![]() |
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. |
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.
|
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;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. |
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. |
Re: JAVA Command Group Question
Quote:
|
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(); |
Re: JAVA Command Group Question
Quote:
--- 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. |
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. |
Re: JAVA Command Group Question
Quote:
What's your opinion of CCRE? |
Re: JAVA Command Group Question
Quote:
|
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. |
Re: JAVA Command Group Question
Quote:
|
Re: JAVA Command Group Question
Quote:
|
Re: JAVA Command Group Question
When you decide to publish your code, please loop me in. I'm interested in seeing what you propose.
|
| All times are GMT -5. The time now is 08:20 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi