Skip to content
learnFRC.org
programminglesson30 min

Command Lifecycle & Subsystem Requirements

The CommandScheduler loop, subsystem requirement locking, and the initialize, execute, isFinished, and end lifecycle methods.

What you will learn

  • Understand the 20ms CommandScheduler execution cycle.
  • Implement the four Command lifecycle methods (initialize, execute, isFinished, end).
  • Prevent actuator contention using subsystem requirement locking.
  • Apply modern WPILib scheduling patterns, Subsystem.idle(), and Timer.createStarted().

Prerequisites:Java Foundations for FRC Robotics

In WPILib, robot behavior is orchestrated by the CommandScheduler. Instead of writing monolithic loops with complex conditional branches, behaviors are encapsulated into discrete Commands.

The scheduler manages command lifecycle states and subsystem requirements to prevent conflicting commands from controlling the same actuator simultaneously.


Every 20 milliseconds (50 times per second), the roboRIO executes one iteration of the scheduler loop:

1. Poll Triggers & Buttons -> 2. Run Subsystem periodic() -> 3. Run Command execute() -> 4. Check isFinished()
  1. Poll Triggers: Evaluates controller buttons, joystick thresholds, and sensor states.
  2. Subsystem periodic(): Runs sensor updates, logging, and telemetry on every subsystem regardless of whether a command is active.
  3. Command execute(): Runs active commands that have been scheduled.
  4. Evaluate isFinished(): If a command reports true, the scheduler removes it and calls its end(false) method.

Every Command class defines four lifecycle methods:

package frc.robot.commands;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.IntakeSubsystem;
import static edu.wpi.first.units.Units.*;
public class RunIntakeUntilDetectedCommand extends Command {
private final IntakeSubsystem m_intake;
public RunIntakeUntilDetectedCommand(IntakeSubsystem intake) {
m_intake = intake;
// Lock this subsystem: interrupts any conflicting command!
addRequirements(m_intake);
}
@Override
public void initialize() {
// Runs ONCE when command starts
m_intake.setVoltage(Volts.of(8.0));
}
@Override
public void execute() {
// Runs EVERY 20ms while active
// Typically empty if running a fixed voltage or onboard closed-loop
}
@Override
public boolean isFinished() {
// Evaluated EVERY 20ms. Return true to finish command.
return m_intake.hasGamePiece();
}
@Override
public void end(boolean interrupted) {
// Runs ONCE when command finishes or is cancelled
m_intake.setVoltage(Volts.of(0.0));
}
}
  • initialize(): Prepares the mechanism for the action. Used for resetting timers, setting target setpoints, or clearing error accumulators.
  • execute(): Continuously updates outputs. For motors running onboard PID or Motion Magic, this method is often left empty because the motor controller manages the 1 kHz closed loop internally.
  • isFinished(): Defines the completion criteria. Returns false for continuous actions (like teleop driving) or true when a condition is met (sensor tripped, target reached, or timeout expired).
  • end(boolean interrupted): Clean-up method. Must always leave the mechanism in a safe state (e.g. cutting motor power or engaging brake mode). The interrupted flag is true if another command preempted this command before isFinished() returned true.

3. Subsystem Requirements & Mutual Exclusion

Section titled “3. Subsystem Requirements & Mutual Exclusion”

The most important rule in Command-Based programming is the Mutual Exclusion Rule:

A Subsystem can only be required by ONE active Command at any given time.

When you call addRequirements(m_elevator) inside a command constructor:

  1. If the elevator is idle, the new command begins running immediately.
  2. If another command is currently controlling the elevator, the scheduler cancels the old command, calls its end(true) method, and starts the new command.
  3. Two commands can never fight over the same motor controller output simultaneously.
Never Forget addRequirements()

If you forget to call addRequirements(), the scheduler will allow two commands to run simultaneously on the same hardware. For example, a default “Hold Position” command will conflict with an “Elevator Move” command, causing violent motor stuttering.


Subsystems can designate a Default Command that runs automatically whenever no other command is requiring that subsystem:

// Drivetrain default command: drive with joysticks
m_drivetrain.setDefaultCommand(
m_drivetrain.driveCommand(
() -> -m_driverController.getLeftY(),
() -> -m_driverController.getLeftX(),
() -> -m_driverController.getRightX()
)
);
// Arm default command: hold current position
m_arm.setDefaultCommand(m_arm.holdPositionCommand());

When an autonomous routine or temporary button command finishes, the subsystem automatically resumes its default command without requiring manual button presses.


Modern WPILib releases introduce several design refinements to prevent common runtime concurrency mistakes:

Directly calling myCommand.schedule() is deprecated in favor of declarative Trigger bindings or explicit scheduler calls:

// Deprecated:
// myCommand.schedule();
// Preferred declarative approach in RobotContainer:
m_operator.a().onTrue(myCommand);
// Explicit manual scheduling (e.g., when launching autonomous selections):
CommandScheduler.getInstance().schedule(m_autonomousCommand);

Direct calls on command instances bypassed requirement tracking context in multi-threaded callbacks.

Subsystems now provide a built-in idle() method that returns a non-terminating command requiring the subsystem without commanding actuator outputs. This replaces ad-hoc run(() -> {}) lambdas for disabling mechanisms while reserving hardware requirements:

// Holds subsystem requirements in an idle state:
m_shooter.setDefaultCommand(m_shooter.idle());

Clean State Timing (Timer.createStarted())

Section titled “Clean State Timing (Timer.createStarted())”

Instead of instantiating and manually triggering a timer in separate statements, use Timer.createStarted():

// Creates and starts the timer immediately in one step
private final Timer m_watchdogTimer = Timer.createStarted();

1. Motor Keeps Spinning After Button Released

Section titled “1. Motor Keeps Spinning After Button Released”
  • Symptom: Motor continues running indefinitely after releasing controller button.
  • Cause: Command lacked an end() implementation, or motor power was not zeroed when interrupted == true.
  • Fix: Always set motor voltage to zero in end(boolean interrupted).
  • Symptom: Button is pressed, but mechanism barely twitches and stops.
  • Cause: isFinished() returned true on the very first loop cycle (for example, sensor was already tripped or variable initialized improperly).
  • Fix: Check that isFinished() logic properly verifies setpoint arrival or sensor debounce.

  1. Write a test command in simulation with logging inside each lifecycle method:
    System.out.println("Init");
    System.out.println("End: interrupted=" + interrupted);
  2. Schedule the command, interrupt it with a second command, and verify that end(true) executes before the second command’s initialize().

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026