Command¶
-
class
wpilib.command.
Command
(name=None, timeout=None)[source]¶ Bases:
wpilib.SendableBase
The Command class is at the very core of the entire command framework. Every command can be started with a call to start(). Once a command is started it will call
initialize()
, and then will repeatedly callexecute()
untilisFinished()
returns True. Once it does,end()
will be called.However, if at any point while it is running
cancel()
is called, then the command will be stopped andinterrupted()
will be called.If a command uses a
Subsystem
, then it should specify that it does so by calling therequires()
method in its constructor. Note that a Command may have multiple requirements, andrequires()
should be called for each one.If a command is running and a new command with shared requirements is started, then one of two things will happen. If the active command is interruptible, then
cancel()
will be called and the command will be removed to make way for the new one. If the active command is not interruptible, the other one will not even be started, and the active one will continue functioning.See also
Creates a new command.
Parameters: - name – The name for this command; if unspecified or None, The name of this command will be set to its class name.
- timeout – The time (in seconds) before this command “times out”. Default is no timeout. See isTimedOut().
-
cancel
()[source]¶ This will cancel the current command.
This will cancel the current command eventually. It can be called multiple times. And it can be called when the command is not running. If the command is running though, then the command will be marked as canceled and eventually removed.
Warning
A command can not be canceled if it is a part of a
CommandGroup
, you must cancel the CommandGroup instead.
-
clearRequirements
()[source]¶ Clears list of subsystem requirements. This is only used by
ConditionalCommand
so cancelling the chosen command works properly inCommandGroup
.
-
doesRequire
(system)[source]¶ Checks if the command requires the given
Subsystem
.Parameters: system – the system Returns: whether or not the subsystem is required, or False if given None.
-
end
()[source]¶ Called 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.
-
execute
()[source]¶ The execute method is called repeatedly until this Command either finishes or is canceled.
-
getGroup
()[source]¶ Returns the
CommandGroup
that this command is a part of. Will return None if this Command is not in a group.Returns: the CommandGroup
that this command is a part of (or None if not in group)
-
initialize
()[source]¶ The initialize method is called the first time this Command is run after being started.
-
interrupted
()[source]¶ Called when the command ends because somebody called cancel() 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.
-
isCanceled
()[source]¶ Returns whether or not this has been canceled.
Returns: whether or not this has been canceled
-
isFinished
()[source]¶ Returns whether this command is finished. If it is, then the command will be removed and end() will be called.
It may be useful for a team to reference the isTimedOut() method for time-sensitive commands, or override TimedCommand.
If you do not specify isFinished in your command, the command will only end if interrupted or canceled. If you want a command that executes only once and then ends, override InstantCommand.
Returns: whether this command is finished.
See: See: class: .TimedCommand See: class: .InstantCommand
-
isInterruptible
()[source]¶ Returns whether or not this command can be interrupted.
Returns: whether or not this command can be interrupted
-
isParented
()[source]¶ Returns whether the command has a parent.
Returns: True if the command has a parent.
-
isRunning
()[source]¶ Returns whether or not the command is running. This may return true even if the command has just been canceled, as it may not have yet called
interrupted()
.Returns: whether or not the command is running
-
isTimedOut
()[source]¶ Returns whether or not the
timeSinceInitialized()
method returns a number which is greater than or equal to the timeout for the command. If there is no timeout, this will always return false.Returns: whether the time has expired
-
removed
()[source]¶ Called when the command has been removed. This will call
interrupted()
orend()
.
-
requires
(subsystem)[source]¶ This method specifies that the given Subsystem is used by this command. This method is crucial to the functioning of the Command System in general.
Note that the recommended way to call this method is in the constructor.
Parameters: subsystem – the Subsystem
required
-
run
()[source]¶ The run method is used internally to actually run the commands.
Returns: whether or not the command should stay within the Scheduler.
-
setInterruptible
(interruptible)[source]¶ Sets whether or not this command can be interrupted.
Parameters: interruptible – whether or not this command can be interrupted
-
setParent
(parent)[source]¶ Sets the parent of this command. No actual change is made to the group.
Parameters: parent – the parent
-
setRunWhenDisabled
(run)[source]¶ Sets whether or not this {@link Command} should run when the robot is disabled.
By default a command will not run when the robot is disabled, and will in fact be canceled.
Parameters: run – whether or not this command should run when the robot is disabled
-
setTimeout
(seconds)[source]¶ Sets the timeout of this command.
Parameters: seconds – the timeout (in seconds) See: isTimedOut()
-
start
()[source]¶ Starts up the command. Gets the command ready to start. Note that the command will eventually start, however it will not necessarily do so immediately, and may in fact be canceled before initialize is even called.
-
startRunning
()[source]¶ This is used internally to mark that the command has been started. The lifecycle of a command is:
startRunning()
is called.run()
is called (multiple times potentially)removed()
is called
It is very important that
startRunning()
andremoved()
be called in order or some assumptions of the code will be broken.
-
startTiming
()[source]¶ Called to indicate that the timer should start. This is called right before initialize() is, inside the run() method.