Lesson 37: Command-Based Programming

Command-based programming is a modern WPILib pattern for organizing robot code. Instead of putting every button check and motor command in one periodic method, you split the robot into subsystems and commands.

The exact class names and templates can change between seasons. Use this lesson for the idea, and use the current WPILib docs when you write the actual project.

Subsystems

A subsystem owns one part of the robot. A drive subsystem owns the drive motors and sensors. An intake subsystem owns the intake motor. Other code should ask a subsystem to do something instead of directly controlling its hardware.

public class Intake extends SubsystemBase {
    private final MotorController motor;

    public Intake(MotorController motor) {
        this.motor = motor;
    }

    public void run(double speed) {
        motor.set(speed);
    }

    public void stop() {
        motor.set(0.0);
    }
}

Commands

A command describes an action. For example, "run the intake while this button is held" or "drive forward until the encoder reaches a target."

Command runIntake = Commands.startEnd(
    () -> intake.run(1.0),
    () -> intake.stop(),
    intake
);

Triggers and Buttons

Buttons schedule commands. This keeps driver controls separate from mechanism hardware.

new JoystickButton(operatorController, 1).whileTrue(runIntake);

Default Commands

A default command is what a subsystem does when nothing else is using it. A drive train usually has a default command that reads joystick input and drives the robot.

Practice

Choose one mechanism and identify its subsystem, two commands, and the driver buttons that should trigger those commands. Then compare your design to the official command-based examples in WPILib.