Class CommandGroup
- java.lang.Object
-
- edu.wpi.first.wpilibj.command.Command
-
- edu.wpi.first.wpilibj.command.CommandGroup
-
- All Implemented Interfaces:
Sendable,AutoCloseable
public class CommandGroup extends Command
ACommandGroupis a list of commands which are executed in sequence.Commands in a
CommandGroupare added using theaddSequential(...)method and are called sequentially.CommandGroupsare themselvescommandsand can be given to otherCommandGroups.CommandGroupswill carry all of the requirements of theirsubcommands. Additional requirements can be specified by callingrequires(...)normally in the constructor.CommandGroups can also execute commands in parallel, simply by adding them using
addParallel(...).- See Also:
Command,Subsystem,IllegalUseOfCommandException
-
-
Constructor Summary
Constructors Constructor Description CommandGroup()Creates a newCommandGroup.CommandGroup(String name)Creates a newCommandGroupwith the given name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddParallel(Command command)Adds a new childCommandto the group.voidaddParallel(Command command, double timeout)Adds a new childCommandto the group with the given timeout.voidaddSequential(Command command)Adds a newCommandto the group.voidaddSequential(Command command, double timeout)Adds a newCommandto the group with a given timeout.protected voidend()Called when the command ended peacefully.protected voidexecute()The execute method is called repeatedly until this Command either finishes or is canceled.protected voidinitialize()The initialize method is called the first time this Command is run after being started.protected voidinterrupted()Called when the command ends because somebody calledcancel()or another command shared the same requirements as this one, and booted it out.protected booleanisFinished()Returns true if all theCommandsin this group have been started and have finished.booleanisInterruptible()Returns whether or not this group is interruptible.-
Methods inherited from class edu.wpi.first.wpilibj.command.Command
cancel, clearRequirements, close, doesRequire, getGroup, getName, getSubsystem, initSendable, isCanceled, isCompleted, isRunning, isTimedOut, requires, setInterruptible, setName, setRunWhenDisabled, setSubsystem, setTimeout, start, timeSinceInitialized, toString, willRunWhenDisabled
-
-
-
-
Constructor Detail
-
CommandGroup
public CommandGroup()
Creates a newCommandGroup. The name of this command will be set to its class name.
-
CommandGroup
public CommandGroup(String name)
Creates a newCommandGroupwith the given name.- Parameters:
name- the name for this command group- Throws:
IllegalArgumentException- if name is null
-
-
Method Detail
-
addSequential
public final void addSequential(Command command)
Adds a newCommandto the group. TheCommandwill be started after all the previously addedCommands.Note that any requirements the given
Commandhas will be added to the group. For this reason, aCommand'srequirements can not be changed after being added to a group.It is recommended that this method be called in the constructor.
- Parameters:
command- TheCommandto be added- Throws:
IllegalUseOfCommandException- if the group has been started before or been given to another groupIllegalArgumentException- if command is null
-
addSequential
public final void addSequential(Command command, double timeout)
Adds a newCommandto the group with a given timeout. TheCommandwill be started after all the previously added commands.Once the
Commandis started, it will be run until it finishes or the time expires, whichever is sooner. Note that the givenCommandwill have no knowledge that it is on a timer.Note that any requirements the given
Commandhas will be added to the group. For this reason, aCommand'srequirements can not be changed after being added to a group.It is recommended that this method be called in the constructor.
- Parameters:
command- TheCommandto be addedtimeout- The timeout (in seconds)- Throws:
IllegalUseOfCommandException- if the group has been started before or been given to another group or if theCommandhas been started before or been given to another groupIllegalArgumentException- if command is null or timeout is negative
-
addParallel
public final void addParallel(Command command)
Adds a new childCommandto the group. TheCommandwill be started after all the previously addedCommands.Instead of waiting for the child to finish, a
CommandGroupwill have it run at the same time as the subsequentCommands. The child will run until either it finishes, a new child with conflicting requirements is started, or the main sequence runs aCommandwith 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
Commandhas will be added to the group. For this reason, aCommand'srequirements can not be changed after being added to a group.It is recommended that this method be called in the constructor.
- Parameters:
command- The command to be added- Throws:
IllegalUseOfCommandException- if the group has been started before or been given to another command groupIllegalArgumentException- if command is null
-
addParallel
public final void addParallel(Command command, double timeout)
Adds a new childCommandto the group with the given timeout. TheCommandwill be started after all the previously addedCommands.Once the
Commandis started, it will run until it finishes, is interrupted, or the time expires, whichever is sooner. Note that the givenCommandwill have no knowledge that it is on a timer.Instead of waiting for the child to finish, a
CommandGroupwill have it run at the same time as the subsequentCommands. The child will run until either it finishes, the timeout expires, a new child with conflicting requirements is started, or the main sequence runs aCommandwith 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
Commandhas will be added to the group. For this reason, aCommand'srequirements can not be changed after being added to a group.It is recommended that this method be called in the constructor.
- Parameters:
command- The command to be addedtimeout- The timeout (in seconds)- Throws:
IllegalUseOfCommandException- if the group has been started before or been given to another command groupIllegalArgumentException- if command is null
-
isFinished
protected boolean isFinished()
Returns true if all theCommandsin this group have been started and have finished.Teams may override this method, although they should probably reference super.isFinished() if they do.
- Specified by:
isFinishedin classCommand- Returns:
- whether this
CommandGroupis finished - See Also:
isTimedOut()
-
initialize
protected void initialize()
Description copied from class:CommandThe initialize method is called the first time this Command is run after being started.- Overrides:
initializein classCommand
-
execute
protected void execute()
Description copied from class:CommandThe execute method is called repeatedly until this Command either finishes or is canceled.
-
end
protected void end()
Description copied from class:CommandCalled when the command ended peacefully. This is where you may want to wrap up loose ends, like shutting off a motor that was being used in the command.
-
interrupted
protected void interrupted()
Description copied from class:CommandCalled when the command ends because somebody calledcancel()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, as done here.- Overrides:
interruptedin classCommand
-
isInterruptible
public boolean isInterruptible()
Returns whether or not this group is interruptible. A command group will be uninterruptible ifsetInterruptable(false)was called or if it is currently running an uninterruptible command or child.- Overrides:
isInterruptiblein classCommand- Returns:
- whether or not this
CommandGroupis interruptible.
-
-