public class CommandGroup extends Command
CommandGroup is a list of commands which are executed in sequence.
Commands in a CommandGroup are added using the
addSequential(...) method and are
called sequentially. CommandGroups are themselves
commands and can be given to other CommandGroups.
CommandGroups will carry all of the requirements of
their subcommands. Additional requirements can be specified
by calling requires(...) normally in
the constructor.
CommandGroups can also execute commands in parallel, simply by adding them
using addParallel(...).
Command,
Subsystem,
IllegalUseOfCommandException| Constructor and Description |
|---|
CommandGroup()
Creates a new
CommandGroup. |
CommandGroup(java.lang.String name)
Creates a new
CommandGroup with the given name. |
| Modifier and Type | Method and Description |
|---|---|
void |
addParallel(Command command)
Adds a new child
Command to the group. |
void |
addParallel(Command command,
double timeout)
Adds a new child
Command to the group with the given timeout. |
void |
addSequential(Command command)
Adds a new
Command to the group. |
void |
addSequential(Command command,
double timeout)
Adds a new
Command to the group with a given timeout. |
protected void |
end()
Called when the command ended peacefully.
|
protected void |
execute()
The execute method is called repeatedly until this Command either finishes
or is canceled.
|
protected void |
initialize()
The initialize method is called the first time this Command is run after
being started.
|
protected void |
interrupted()
Called when the command ends because somebody called
cancel() or another command shared the same
requirements as this one, and booted it out. |
protected boolean |
isFinished()
Returns true if all the
Commands in this group have been
started and have finished. |
boolean |
isInterruptible()
Returns whether or not this group is interruptible.
|
cancel, doesRequire, getGroup, getName, getSmartDashboardType, getTable, initTable, isCanceled, isRunning, isTimedOut, requires, setInterruptible, setRunWhenDisabled, setTimeout, start, timeSinceInitialized, toString, willRunWhenDisabledpublic CommandGroup()
CommandGroup. The name of this command
will be set to its class name.public CommandGroup(java.lang.String name)
CommandGroup with the given name.
$name - the name for this command groupjava.lang.IllegalArgumentException - if name is nullpublic final void addSequential(Command command)
Command to the group. The Command will be started after all the previously added Commands.
Note that any requirements the given Command has will be
added to the group. For this reason, a Command's
requirements can not be changed after being added to a group.
It is recommended that this method be called in the constructor.
command - The Command to be addedIllegalUseOfCommandException - if the group has been started before
or been given to another groupjava.lang.IllegalArgumentException - if command is nullpublic final void addSequential(Command command, double timeout)
Command to the group with a given timeout. The
Command will be started after all the previously added
commands.
Once the Command is started, it will be run until it
finishes or the time expires, whichever is sooner. Note that the given
Command will have no knowledge that it is on a timer.
Note that any requirements the given Command has will be
added to the group. For this reason, a Command's
requirements can not be changed after being added to a group.
It is recommended that this method be called in the constructor.
command - The Command to be addedtimeout - The timeout (in seconds)IllegalUseOfCommandException - if the group has been started before
or been given to another group or if the Command
has been started before or been given to another groupjava.lang.IllegalArgumentException - if command is null or timeout is negativepublic final void addParallel(Command command)
Command to the group. The Command will be
started after all the previously added Commands.
Instead of waiting for the child to finish, a CommandGroup will
have it run at the same time as the subsequent Commands.
The child will run until either it finishes, a new child with conflicting
requirements is started, or the main sequence runs a Command with
conflicting requirements. In the latter two cases, the child will be
canceled even if it says it can't be interrupted.
Note that any requirements the given Command has will be
added to the group. For this reason, a Command's
requirements can not be changed after being added to a group.
It is recommended that this method be called in the constructor.
command - The command to be addedIllegalUseOfCommandException - if the group has been started before
or been given to another command groupjava.lang.IllegalArgumentException - if command is nullpublic final void addParallel(Command command, double timeout)
Command to the group with the given timeout. The
Command will be started after all the previously added
Commands.
Once the Command is started, it will run until it finishes,
is interrupted, or the time expires, whichever is sooner. Note that the
given Command will have no knowledge that it is on a timer.
Instead of waiting for the child to finish, a CommandGroup will
have it run at the same time as the subsequent Commands.
The child will run until either it finishes, the timeout expires, a new
child with conflicting requirements is started, or the main sequence runs a
Command with conflicting requirements. In the latter two cases, the
child will be canceled even if it says it can't be interrupted.
Note that any requirements the given Command has will be
added to the group. For this reason, a Command's
requirements can not be changed after being added to a group.
It is recommended that this method be called in the constructor.
command - The command to be addedtimeout - The timeout (in seconds)IllegalUseOfCommandException - if the group has been started before
or been given to another command groupjava.lang.IllegalArgumentException - if command is nullprotected boolean isFinished()
Commands in this group have been
started and have finished.
Teams may override this method, although they should probably reference super.isFinished() if they do.
isFinished in class CommandCommandGroup is finishedisTimedOut()protected void initialize()
Commandinitialize in class Commandprotected void execute()
Commandprotected void end()
Commandprotected void interrupted()
Commandcancel() or another command shared the same
requirements as this one, and booted it out.
This is where you may want to wrap up loose ends, like shutting off a motor that was being used in the command.
Generally, it is useful to simply call the end()
method within this method
interrupted in class Commandpublic boolean isInterruptible()
setInterruptable(false) was called or if it is currently running an
uninterruptible command or child.isInterruptible in class CommandCommandGroup is interruptible.