Command

class wpilib.command.Command(name=None, timeout=None)[source]

Bases: wpilib.Sendable

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 call execute() until isFinished() 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 and interrupted() will be called.

If a command uses a Subsystem, then it should specify that it does so by calling the requires() method in its constructor. Note that a Command may have multiple requirements, and requires() 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.

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.

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)
getName()[source]

Returns the name of this command. If no name was specified in the constructor, then the default is the name of the class.

Returns:the name of this command
getRequirements()[source]

Returns the requirements (as a set of Subsystems) of this command

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.

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.

Returns:whether this command is finished.
See:isTimedOut()
isInterruptible()[source]

Returns whether or not this command can be interrupted.

Returns:whether or not this command can be interrupted
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
lockChanges()[source]

Prevents further changes from being made

removed()[source]

Called when the command has been removed. This will call interrupted() or end().

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:

It is very important that startRunning() and removed() 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.

timeSinceInitialized()[source]

Returns the time since this command was initialized (in seconds). This function will work even if there is no specified timeout.

Returns:the time since this command was initialized (in seconds).
willRunWhenDisabled()[source]

Returns whether or not this Command will run when the robot is disabled, or if it will cancel itself.