View Single Post
  #3   Spotlight this post!  
Unread 13-02-2014, 11:34
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: CommandGroup questions

Given this expectation:
Quote:
Originally Posted by pblankenbaker View Post
  • Run CommandA first and wait for it to complete.
  • Then run CommandB and CommandC at the same time.
  • After CommandB and CommandC complete I then want to run CommandD.
This will not work:
Quote:
Originally Posted by pblankenbaker View Post
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;
}
A command added in parallel to a command group has no effect on when the commands after it run. CommandD will run when CommandC finishes, with no regard for whether or not CommandB is running. To fix this, add this line before CommandD:
Code:
cmd.addSequential(new WaitForChildren());
This will wait until all parallel commands have completed before moving on.

Quote:
Originally Posted by pblankenbaker View Post
My next questions have to do with what happens to a command group when things don't complete nicely:
  • If CommandA from the example command group above was interrupted, would the entire command group be interrupted? For example, let's assume CommandA was waiting for something to move into position and CommandB performed a firing action. If CommandA was interrupted, would CommandB still run, or would the interruption stop CommandB from ever being run?
If you directly cancelled CommandA, the CommandGroup would continue to the next Command in line, per lines 217-230 in CommandGroup.java:
Code:
            if (cmd != null) {
                if (entry.isTimedOut()) {
                    cmd._cancel();
                }
                if (cmd.run()) {
                    break;
                } else {
                    cmd.removed();
                    m_currentCommandIndex++;
                    firstRun = true;
                    cmd = null;
                    continue;
                }
            }
However, if the interruption is due to another command that requires() the same subsystem, the whole CommandGroup will be cancelled. A CommandGroup absorbs the requirements of all Commands within it.

Quote:
Originally Posted by pblankenbaker View Post
  • Secondly, Assume CommandA has used the setTimeout() method or was added to the group with a timeout option to indicate that it has 2 seconds to complete. If the timeout is reached, will the rest of the commands complete in the sequence?
If the timeout is reached, the command will not even terminate, unless you have an isFinished() method like this (from WaitCommand.java):
Code:
    protected boolean isFinished() {
        return isTimedOut();
    }
However, there is a way to add a timeout within a CommandGroup. addSequential and addParallel are both overloaded so that you can specify a timeout after which the Command will be cancelled and the next Command in the group will begin, like so:
Code:
addSequential(new CommandA(),2.0); // times out after 2 seconds
__________________
I code stuff.
Reply With Quote