|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
JAVA Command Group Question
We have been using JAVA for a couple of years now and each year have had issues with the command groups. Previously, for the most part, sequential commands have worked fine but parallel commands have had hit and miss success. We've been through the manuals repeatedly without any success in clearing up the issues.
This year it's been a little better but we just ran into an issue that makes me wonder if it isn't what we were seeing before. We have a number of commands added sequentially in a command group. These commands for the most part run fine. The problem we get to is when the output of one command is used in a subsequent command. We might initialize the robot with a robot.drivedistance variable initialized to 10". The first command in the command group may set the robot.drivedistance to 100". Then a command further down in the command group is passed this distance such as driveRobotForDistance(robot.drivedistance, fullspeed). This command seems to execute with the initial 10" instead of the 100". Can anyone explain why? We can hardcode the 100" into driveRobotForDistance(100,fullsspeed) and it works just fine. |
|
#2
|
|||||
|
|||||
|
Re: JAVA Command Group Question
It's impossible to be sure without seeing the code, but it sounds like problems in variable scope, that is, a confusion between global and local variables which have the same name.
|
|
#3
|
||||
|
||||
|
Re: JAVA Command Group Question
This is a really annoying aspect of CommandGroup. Basically, all the code that you write for a CommandGroup is in the constructor. This means that the code itself is going to run instantaneously, probably when you first turn on the robot. What addSequential() and addParallel() do is pretty much just add the commands you requested into a queue. Those commands are then iterated through later when the CommandGroup is actually started.
What this means is that if you have a 3 line command group, like such: Code:
Robot.drivedistance = 10; addSequential(new Command1()); addSequestial(new Command2(Robot.drivedistance)); Code:
Robot.drivedistance = 100; There are a few different ways to fix this, although I don't consider any of them pretty. The simplest way is probably just to create a new command that will drive the distance specified by Robot.drivedistance, and have it be separate from your normal driveRobotToDistance command. In this new command you should check what the value of Robot.drivedistance is in the initialize method, not the constructor. |
|
#4
|
|||
|
|||
|
Re: JAVA Command Group Question
Yes. I don't see it as being a scope issue. I just caught yesterday that the commands are actually in the command group constructor. Had never noticed that before. Explains why we couldn't run if structures in it.
We came to the same conclusion. Use the initialize method to check the global. This could very well explain the behavior that we'd seen the previous years as well. It was kind of fuzzy and only seen at certain times. We were coming to the conclusion that all of the commands in the command group were being instantiated early. Our previous train of thought is that they would be instantiated when we instantiated the command group. A few system.outs for the poor man's trace seemed to be showing us otherwise. this link: http://www.chiefdelphi.com/forums/sh...ommand+gro up has some interesting concepts to get around it as well. But it was a little past my experience and I don't want to tackle that at t-3 days. |
|
#5
|
|||||
|
|||||
|
Re: JAVA Command Group Question
It is obvious that you have no understanding of how the CommandGroups work. Please stick to answering questions about topics you know and avoid just trying to answer every single question that is posed.
|
|
#6
|
||||
|
||||
|
Re: JAVA Command Group Question
Let's not be harsh here. @GeeTwo is correct that we'd need the code to fully understand what's going on.
--- For complicated command logic, the CommandGroup often isn't enough. What we did by NECMP this year was write our own CommandGroup-like class to run our automatic shooting logic (it was a lot prettier than the mess of CommandGroups in CommandGroups we had before). When you have full control over how sub-commands are run, it's easy to relay the values you need. And, in terms of good programming practice, it's the best solution. |
|
#7
|
|||
|
|||
|
Re: JAVA Command Group Question
Quote:
As an aside, I think Pault's criticism of the CommandGroup is more about the subtleties of Java's pass-by-value semantics. A statement like: Code:
addSequestial(new Command2(Robot.drivedistance)); Here is an easy way to use CommandGroups to achieve what Pault is desiring, without a lot of extra code. Suppose you have two Commands, CalculateDistanceToShootingPosition and DriveForward. You want to execute them in sequence in a CommandGroup. You could say something like this in the CommandGroup's constructor: Code:
DriveForward df = new DriveForward(); CalculateDistanceToShootingPosition calc = new CalculateDistanceToShootingPosition(df); addSequential(calc); addSequential(df); |
|
#8
|
||||
|
||||
|
Re: JAVA Command Group Question
Quote:
Right now the code is has a couple of bugs in certain edge cases, but I hope to release it publicly soon. |
|
#10
|
||||
|
||||
|
Re: JAVA Command Group Question
Quote:
246's programming department has grown immensely over the past 2 years, and looks to be continuing on an upward trend. I would rather take a few years to build up a coding foundation that is tailored exactly to what we want, we can trust, know the limitations of, and know how to fix if something goes wrong. |
|
#11
|
|||
|
|||
|
Quote:
|
|
#12
|
|||
|
|||
|
Re: JAVA Command Group Question
Quote:
|
|
#13
|
||||
|
||||
|
Re: JAVA Command Group Question
Again, I don't see this as a problem with the way Java works, I see this as a problem with the way CommandGroups use Java. There are plenty of workarounds to make CommandGroup work if you understand why it is so limited, but every suggestion that I have seen is code that I consider fairly ugly and overly complicated. I would much rather use a framework that allows for real time decision making. That means more than just being able to pass variables into Commands, it also includes having if statements and while loops, and the capacity to be more specific about when a Command should start other than just addSequential and addParallel. The 2 best ways I can think of doing this are a state machine and a separate thread. State machines are hard to create a CommandGroup like framework for, and overall take a significantly longer time to code. But it is possible to have something very similar to CommandGroup on a separate thread, with 1 liners that will run a Command once certain parameters are met.
|
|
#14
|
|||
|
|||
|
Re: JAVA Command Group Question
When you decide to publish your code, please loop me in. I'm interested in seeing what you propose.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|