View Single Post
  #4   Spotlight this post!  
Unread 13-02-2014, 13:50
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 103
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
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:
  • Runs CommandA first and wait for it to complete.
  • Then runs CommandB and CommandC at the same time.
  • After CommandB and CommandC complete CommandD is run.

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;
}
Option B: Mix addParallel() and addSequential(), but only if you fully understand how CommandGroups work:

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;
}
Please let me know if the three above fragments won't yield the same results as I'm going to try to explain this to our programming leads today (I suspect we will be reviewing and refactoring a lot of command groups).

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.
Reply With Quote