|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
CommandGroup questions
I have a couple of questions regarding how command groups are processed.
As an example, lets assume that I want to:
I'm assuming the following would generate this sequence: Code:
public static Command buildMyCommand() {
CommandGroup bc = new CommandGroup();
bc.addParallel(new CommandB());
bc.addParallel(new CommandC());
CommandGroup cmd = new CommandGroup();
cmd.addSequential(new CommandA());
cmd.addSequential(bc);
cmd.addSequential(new CommandD());
return cmd;
}
Code:
public static Command buildMyCommand() {
CommandGroup cmd = new CommandGroup();
cmd.addSequential(new CommandA());
// Start of first command to run in parallel
cmd.addParallel(new CommandB());
// End of commands to run in parallel (runs in parallel with CommandB)
cmd.addSequential(new CommandC());
cmd.addSequential(new CommandD());
return cmd;
}
My next questions have to do with what happens to a command group when things don't complete nicely:
Thanks, Paul |
|
#2
|
|||||
|
|||||
|
Re: CommandGroup questions
Quote:
|
|
#3
|
||||
|
||||
|
Re: CommandGroup questions
Given this expectation:
Quote:
Quote:
Code:
cmd.addSequential(new WaitForChildren()); Quote:
Code:
if (cmd != null) {
if (entry.isTimedOut()) {
cmd._cancel();
}
if (cmd.run()) {
break;
} else {
cmd.removed();
m_currentCommandIndex++;
firstRun = true;
cmd = null;
continue;
}
}
Quote:
Code:
protected boolean isFinished() {
return isTimedOut();
}
Code:
addSequential(new CommandA(),2.0); // times out after 2 seconds |
|
#4
|
|||
|
|||
|
Re: CommandGroup questions
Thanks for the explanations. If I understand things correctly, when the goal is to build a command group that has the following pattern:
There are at least two ways to code this: Option A: Don't mix addParallel() and addSequential() (use nested CommandGroup objects instead): Code:
public static Command buildMyCommand() {
CommandGroup bc = new CommandGroup();
bc.addParallel(new CommandB());
bc.addParallel(new CommandC());
CommandGroup cmd = new CommandGroup();
cmd.addSequential(new CommandA());
cmd.addSequential(bc);
cmd.addSequential(new CommandD());
return cmd;
}
Code:
public static Command buildMyCommand() {
CommandGroup cmd = new CommandGroup();
// CommandA is the first command to run
cmd.addSequential(new CommandA());
// Start a command in parallel after CommandA finishes
cmd.addParallel(new CommandB());
// Start a sequential command (also starts after CommandA finishes
// runs at the same time as CommandB)
cmd.addSequential(new CommandC());
// Wait for CommandB and CommandC to finish
cmd.addSequential(new WaitForChildren());
// Now it is OK to run CommandD (both CommandB and CommandC are done)
cmd.addSequential(new CommandD());
return cmd;
}
Option C: Mix addParallel() and addSequential(), similar to Option B, but uses addParallel() for CommandB and CommandC (which might be slightly more intuitive to look at): Code:
public static Command buildMyCommand() {
CommandGroup cmd = new CommandGroup();
// CommandA is the first command to run
cmd.addSequential(new CommandA());
// Start a command in parallel after CommandA finishes
cmd.addParallel(new CommandB());
// Start a parallel command (also starts after CommandA finishes
// runs at the same time as CommandB)
cmd.addParallel(new CommandC());
// Wait for CommandB and CommandC to finish
cmd.addSequential(new WaitForChildren());
// Now it is OK to run CommandD (both CommandB and CommandC are done)
cmd.addSequential(new CommandD());
return cmd;
}
I think I prefer the nested command groups (Option A) for readability (human comprehension). Unless someone tells me this won't work, I will encourage this general rule of thumb when we introduce CommandGroups to new programmers joining the team (at least to get them started). We will also need to review our command groups and think about the different behaviors between timeouts and interrupted situations. I suspect we have some commands that need to be coded a little more defensively so we don't break things on the robot for certain time outs (if CommandA times out, we may not want to run CommandB in the sequence). Thanks again for the information, Paul Last edited by pblankenbaker : 13-02-2014 at 13:54. |
|
#5
|
|||||
|
|||||
|
Re: CommandGroup questions
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|