Command Based programming - frc-7603/VespaRobotics2024-25 GitHub Wiki

What is Command Based programming?

It's used to simplify and abstract the code by splitting the code into scheduled chunks (called Commands). The commands are run by the CommandScheduler class, and ensure that no commands use the same resources at the same time.

See here for more details.

Subsystems, Commands, and the CommandScheduler

These are the most important parts of command based programming.

Subsystems

Subsystems are used to organise hardware components and the methods to use them.
For example, a subsystem would be used for:

  • The swerve drive base
  • The elevator
  • Game piece pickup
  • Vision Subsystem

Subsystems can have a periodic method and a default command that are called by the CommandScheduler.

See here for more details

Commands

Commands group together code to run and the subsystems it uses. The subsystems requirements are need to ensure that only one command uses a subsystem at a time. Commands have three important methods:

  1. initialize() which runs when the command is scheduled, and is used to set anything up for the next part;
  2. execute() which runs as the command is scheduled (i.e. gets called repeatedly until there is a reason to stop the command), and;
  3. end(boolean interrupted) which runs after the command is scheduled and takes the boolean, if false it ended according to the command (using the isFinished() method).

The Command class has many subclasses that allow for unique ways group and run commands, some of the more useful being accessible through the Commands factory methods.

See more about commands.
See more about grouping commands.

Binding commands to user input

Commands can be automatically called on user input using the Trigger class and its method, such as onTrue(), whileTrue(), and toggleOnTrue(). Triggers can also be chained to allow for more complex input conditions. See here.

CommandScheduler

The CommandScheduler class runs all the commands, and needs the CommandScheduler.getInstance().run() method to be called in robot periodic to do so.
The run() method will:

  1. Call each subsystems periodic() method;
  2. Checks the triggers (buttons and such) and prepares any commands that should be run;
  3. Runs the execute() method of each command currently scheduled, then calls end() if any command is finished or being cancelled;
  4. The default commands of any UNUSED subsystems are scheduled and run like in step 3.

See here for more details.

Project setup

When making a new project pick the "Command Robot" or "Command Robot Skeleton" template, or the equivalent example and proceed as normal. There will be the RobotContainer.java file which will be setting up the subsystems, trigger bindings, any other commands, and the auto command in the configureBindings() method. An instance of RobotContainer must be declared in the Robot.java file so the auto command can be called and scheduled. The Robot.java file will be mostly empty while using command base programming.