Class Command
- All Implemented Interfaces:
Sendable
- Direct Known Subclasses:
ConditionalCommand
,DeferredCommand
,FunctionalCommand
,MecanumControllerCommand
,NotifierCommand
,ParallelCommandGroup
,ParallelDeadlineGroup
,ParallelRaceGroup
,PIDCommand
,ProfiledPIDCommand
,ProxyCommand
,RamseteCommand
,RepeatCommand
,ScheduleCommand
,SelectCommand
,SequentialCommandGroup
,SwerveControllerCommand
,TrapezoidProfileCommand
,WaitCommand
,WaitUntilCommand
,WrapperCommand
CommandScheduler
, and can be composed into CommandGroups to allow users to build
complicated multistep actions without the need to roll the state machine logic themselves.
Commands are run synchronously from the main robot loop; no multithreading is used, unless specified explicitly from the command implementation.
This class is provided by the NewCommands VendorDep
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
An enum describing the command's behavior when another command with a shared requirement is scheduled. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal void
addRequirements
(Subsystem... requirements) Adds the specified subsystems to the requirements of the command.final void
addRequirements
(Collection<Subsystem> requirements) Adds the specified subsystems to the requirements of the command.Decorates this command with a set of commands to run parallel to it, ending when the last command ends.Decorates this command with a set of commands to run after it in sequence.Decorates this command with a runnable to run after the command finishes.asProxy()
Decorates this command to run "by proxy" by wrapping it in aProxyCommand
.beforeStarting
(Command before) Decorates this command with another command to run before this command starts.beforeStarting
(Runnable toRun, Subsystem... requirements) Decorates this command with a runnable to run before this command starts.void
cancel()
Cancels this command.deadlineFor
(Command... parallel) Decorates this command with a set of commands to run parallel to it, ending when the calling command ends and interrupting all the others.deadlineWith
(Command... parallel) Deprecated, for removal: This API element is subject to removal in a future version.void
end
(boolean interrupted) The action to take when the command ends.void
execute()
The main body of a command.finallyDo
(BooleanConsumer end) Decorates this command with a lambda to call on interrupt or end, following the command's inherentend(boolean)
method.Decorates this command with a lambda to call on interrupt or end, following the command's inherentend(boolean)
method.How the command behaves when another command with a shared requirement is scheduled.getName()
Gets the name of this Command.Specifies the set of subsystems used by this command.Gets the subsystem name of this Command.handleInterrupt
(Runnable handler) Decorates this command with a lambda to call on interrupt, following the command's inherentend(boolean)
method.boolean
hasRequirement
(Subsystem requirement) Whether the command requires a given subsystem.ignoringDisable
(boolean doesRunWhenDisabled) Decorates this command to run or stop when disabled.void
The initial subroutine of a command.void
initSendable
(SendableBuilder builder) Initializes thisSendable
object.boolean
Whether the command has finished.boolean
Whether the command is currently scheduled.onlyIf
(BooleanSupplier condition) Decorates this command to only run if this condition is met.onlyWhile
(BooleanSupplier condition) Decorates this command with a run condition.Decorates this command with a set of commands to run parallel to it, ending when the first command ends.Decorates this command to run repeatedly, restarting it when it ends, until this command is interrupted.boolean
Whether the given command should run when the robot is disabled.void
schedule()
Schedules this command.void
Sets the name of this Command.void
setSubsystem
(String subsystem) Sets the subsystem name of this Command.unless
(BooleanSupplier condition) Decorates this command to only run if this condition is not met.until
(BooleanSupplier condition) Decorates this command with an interrupt condition.withDeadline
(Command deadline) Creates a new command that runs this command and the deadline in parallel, finishing (and interrupting this command) when the deadline finishes.withInterruptBehavior
(Command.InterruptionBehavior interruptBehavior) Decorates this command to have a differentinterruption behavior
.Decorates this Command with a name.withTimeout
(double seconds) Decorates this command with a timeout.withTimeout
(Time time) Decorates this command with a timeout.
-
Constructor Details
-
Command
protected Command()Default constructor.
-
-
Method Details
-
initialize
The initial subroutine of a command. Called once when the command is initially scheduled. -
execute
The main body of a command. Called repeatedly while the command is scheduled. -
end
The action to take when the command ends. Called when either the command finishes normally, or when it interrupted/canceled.Do not schedule commands here that share requirements with this command. Use
andThen(Command...)
instead.- Parameters:
interrupted
- whether the command was interrupted/canceled
-
isFinished
Whether the command has finished. Once a command finishes, the scheduler will call its end() method and un-schedule it.- Returns:
- whether the command has finished.
-
getRequirements
Specifies the set of subsystems used by this command. Two commands cannot use the same subsystem at the same time. If another command is scheduled that shares a requirement,getInterruptionBehavior()
will be checked and followed. If no subsystems are required, return an empty set.Note: it is recommended that user implementations contain the requirements as a field, and return that field here, rather than allocating a new set every time this is called.
- Returns:
- the set of subsystems that are required
- See Also:
-
addRequirements
Adds the specified subsystems to the requirements of the command. The scheduler will prevent two commands that require the same subsystem from being scheduled simultaneously.Note that the scheduler determines the requirements of a command when it is scheduled, so this method should normally be called from the command's constructor.
- Parameters:
requirements
- the requirements to add
-
addRequirements
Adds the specified subsystems to the requirements of the command. The scheduler will prevent two commands that require the same subsystem from being scheduled simultaneously.Note that the scheduler determines the requirements of a command when it is scheduled, so this method should normally be called from the command's constructor.
- Parameters:
requirements
- the requirements to add
-
getName
Gets the name of this Command.By default, the simple class name is used. This can be changed with
setName(String)
.- Returns:
- The display name of the Command
-
setName
Sets the name of this Command.- Parameters:
name
- The display name of the Command.
-
getSubsystem
Gets the subsystem name of this Command.- Returns:
- Subsystem name
-
setSubsystem
Sets the subsystem name of this Command.- Parameters:
subsystem
- subsystem name
-
withTimeout
Decorates this command with a timeout. If the specified timeout is exceeded before the command finishes normally, the command will be interrupted and un-scheduled.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
seconds
- the timeout duration- Returns:
- the command with the timeout added
-
withTimeout
Decorates this command with a timeout. If the specified timeout is exceeded before the command finishes normally, the command will be interrupted and un-scheduled.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
time
- the timeout duration- Returns:
- the command with the timeout added
-
until
Decorates this command with an interrupt condition. If the specified condition becomes true before the command finishes normally, the command will be interrupted and un-scheduled.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
condition
- the interrupt condition- Returns:
- the command with the interrupt condition added
- See Also:
-
onlyWhile
Decorates this command with a run condition. If the specified condition becomes false before the command finishes normally, the command will be interrupted and un-scheduled.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
condition
- the run condition- Returns:
- the command with the run condition added
- See Also:
-
beforeStarting
Decorates this command with a runnable to run before this command starts.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
toRun
- the Runnable to runrequirements
- the required subsystems- Returns:
- the decorated command
-
beforeStarting
Decorates this command with another command to run before this command starts.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
before
- the command to run before this one- Returns:
- the decorated command
-
andThen
Decorates this command with a runnable to run after the command finishes.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
toRun
- the Runnable to runrequirements
- the required subsystems- Returns:
- the decorated command
-
andThen
Decorates this command with a set of commands to run after it in sequence. Often more convenient/less-verbose than constructing a newSequentialCommandGroup
explicitly.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
next
- the commands to run next- Returns:
- the decorated command
-
withDeadline
Creates a new command that runs this command and the deadline in parallel, finishing (and interrupting this command) when the deadline finishes.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
deadline
- the deadline of the command group- Returns:
- the decorated command
- See Also:
-
deadlineWith
@Deprecated(since="2025", forRemoval=true) public ParallelDeadlineGroup deadlineWith(Command... parallel) Deprecated, for removal: This API element is subject to removal in a future version.Decorates this command with a set of commands to run parallel to it, ending when the calling command ends and interrupting all the others. Often more convenient/less-verbose than constructing a newParallelDeadlineGroup
explicitly.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
parallel
- the commands to run in parallel- Returns:
- the decorated command
-
deadlineFor
Decorates this command with a set of commands to run parallel to it, ending when the calling command ends and interrupting all the others. Often more convenient/less-verbose than constructing a newParallelDeadlineGroup
explicitly.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
parallel
- the commands to run in parallel. Note the parallel commands will be interrupted when the deadline command ends- Returns:
- the decorated command
- See Also:
-
alongWith
Decorates this command with a set of commands to run parallel to it, ending when the last command ends. Often more convenient/less-verbose than constructing a newParallelCommandGroup
explicitly.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
parallel
- the commands to run in parallel- Returns:
- the decorated command
-
raceWith
Decorates this command with a set of commands to run parallel to it, ending when the first command ends. Often more convenient/less-verbose than constructing a newParallelRaceGroup
explicitly.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
parallel
- the commands to run in parallel- Returns:
- the decorated command
-
repeatedly
Decorates this command to run repeatedly, restarting it when it ends, until this command is interrupted. The decorated command can still be canceled.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Returns:
- the decorated command
-
asProxy
Decorates this command to run "by proxy" by wrapping it in aProxyCommand
. Use this for "forking off" from command compositions when the user does not wish to extend the command's requirements to the entire command composition. ProxyCommand has unique implications and semantics, see the WPILib docs for a full explanation.- Returns:
- the decorated command
- See Also:
-
unless
Decorates this command to only run if this condition is not met. If the command is already running and the condition changes to true, the command will not stop running. The requirements of this command will be kept for the new conditional command.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
condition
- the condition that will prevent the command from running- Returns:
- the decorated command
- See Also:
-
onlyIf
Decorates this command to only run if this condition is met. If the command is already running and the condition changes to false, the command will not stop running. The requirements of this command will be kept for the new conditional command.Note: This decorator works by adding this command to a composition. The command the decorator was called on cannot be scheduled independently or be added to a different composition (namely, decorators), unless it is manually cleared from the list of composed commands with
CommandScheduler.removeComposedCommand(Command)
. The command composition returned from this method can be further decorated without issue.- Parameters:
condition
- the condition that will allow the command to run- Returns:
- the decorated command
- See Also:
-
ignoringDisable
Decorates this command to run or stop when disabled.- Parameters:
doesRunWhenDisabled
- true to run when disabled.- Returns:
- the decorated command
-
withInterruptBehavior
Decorates this command to have a differentinterruption behavior
.- Parameters:
interruptBehavior
- the desired interrupt behavior- Returns:
- the decorated command
-
finallyDo
Decorates this command with a lambda to call on interrupt or end, following the command's inherentend(boolean)
method.- Parameters:
end
- a lambda accepting a boolean parameter specifying whether the command was interrupted.- Returns:
- the decorated command
-
finallyDo
Decorates this command with a lambda to call on interrupt or end, following the command's inherentend(boolean)
method. The provided lambda will run identically in both interrupt and end cases.- Parameters:
end
- a lambda to run when the command ends, whether or not it was interrupted.- Returns:
- the decorated command
-
handleInterrupt
Decorates this command with a lambda to call on interrupt, following the command's inherentend(boolean)
method.- Parameters:
handler
- a lambda to run when the command is interrupted- Returns:
- the decorated command
-
schedule
Schedules this command. -
cancel
Cancels this command. Will callend(true)
. Commands will be canceled regardless ofinterruption behavior
.- See Also:
-
isScheduled
Whether the command is currently scheduled. Note that this does not detect whether the command is in a composition, only whether it is directly being run by the scheduler.- Returns:
- Whether the command is scheduled.
-
hasRequirement
Whether the command requires a given subsystem.- Parameters:
requirement
- the subsystem to inquire about- Returns:
- whether the subsystem is required
-
getInterruptionBehavior
How the command behaves when another command with a shared requirement is scheduled.- Returns:
- a variant of
Command.InterruptionBehavior
, defaulting tokCancelSelf
.
-
runsWhenDisabled
Whether the given command should run when the robot is disabled. Override to return true if the command should run when disabled.- Returns:
- whether the command should run when the robot is disabled
-
withName
Decorates this Command with a name.- Parameters:
name
- name- Returns:
- the decorated Command
-
initSendable
Description copied from interface:Sendable
Initializes thisSendable
object.- Specified by:
initSendable
in interfaceSendable
- Parameters:
builder
- sendable builder
-
deadlineFor(edu.wpi.first.wpilibj2.command.Command...)
instead.