Subsystem

class commands2.Subsystem[source]

Bases: Sendable

A robot subsystem. Subsystems are the basic unit of robot organization in the Command-based framework; they encapsulate low-level hardware objects (motor controllers, sensors, etc.) and provide methods through which they can be used by Commands. Subsystems are used by the CommandScheduler’s resource management system to ensure multiple robot actions are not “fighting” over the same hardware; Commands that use a subsystem should include that subsystem in their commands2.Command.getRequirements() method, and resources used within a subsystem should generally remain encapsulated and not be shared by other parts of the robot.

Subsystems must be registered with the scheduler with the commands2.CommandScheduler.registerSubsystem() method in order for the periodic() method to be called. It is recommended that this method be called from the constructor of users’ Subsystem implementations.

addChild(name: str, child: Sendable) None[source]

Associates a wpiutil.Sendable with this Subsystem. Also update the child’s name.

Parameters:
  • name – name to give child

  • child – sendable

getCurrentCommand() Command | None[source]

Returns the command currently running on this subsystem. Returns None if no command is currently scheduled that requires this subsystem.

Returns:

the scheduled command currently requiring this subsystem

getDefaultCommand() Command | None[source]

Gets the default command for this subsystem. Returns None if no default command is currently associated with the subsystem.

Returns:

the default command associated with this subsystem

getName() str[source]

Gets the name of this Subsystem.

Returns:

Name

getSubsystem() str[source]

Gets the subsystem name of this Subsystem.

Returns:

Subsystem name

initSendable(builder: wpiutil._wpiutil.SendableBuilder) None[source]

Initializes this Sendable object.

Parameters:

builder – sendable builder

periodic() None[source]

This method is called periodically by the CommandScheduler. Useful for updating subsystem-specific state that you don’t want to offload to a Command. Teams should try to be consistent within their own codebases about which responsibilities will be handled by Commands, and which will be handled here.

register()[source]

Registers this subsystem with the CommandScheduler, allowing its periodic() method to be called when the scheduler runs.

removeDefaultCommand() None[source]

Removes the default command for the subsystem. This will not cancel the default command if it is currently running.

run(action: Callable[[], None]) Command[source]

Constructs a command that runs an action every iteration until interrupted. Requires this subsystem.

Parameters:

action – the action to run

Returns:

the command

runEnd(run: Callable[[], None], end: Callable[[], None]) Command[source]

Constructs a command that runs an action every iteration until interrupted, and then runs a second action. Requires this subsystem.

Parameters:
  • run – the action to run every iteration

  • end – the action to run on interrupt

Returns:

the command

runOnce(action: Callable[[], None]) Command[source]

Constructs a command that runs an action once and finishes. Requires this subsystem.

Parameters:

action – the action to run

Returns:

the command

setDefaultCommand(command: Command) None[source]

Sets the default Command of the subsystem. The default command will be automatically scheduled when no other commands are scheduled that require the subsystem. Default commands should generally not end on their own, i.e. their commands2.Command.isFinished() method should always return false. Will automatically register this subsystem with the CommandScheduler.

Parameters:

defaultCommand – the default command to associate with this subsystem

setName(name: str) None[source]

Set the name of this Subsystem.

setSubsystem(subsystem: str)[source]

Sets the subsystem name of this Subsystem.

Parameters:

subsystem – subsystem name

simulationPeriodic() None[source]

This method is called periodically by the CommandScheduler. Useful for updating subsystem-specific state that needs to be maintained for simulations, such as for updating simulation classes and setting simulated sensor readings.

startEnd(start: Callable[[], None], end: Callable[[], None]) Command[source]

Constructs a command that runs an action once and another action when the command is interrupted. Requires this subsystem.

Parameters:
  • start – the action to run on start

  • end – the action to run on interrupt

Returns:

the command