Skip to content

3. Command-Based Paradigm

If you’ve built simple robots before, you might have written all your code linearly: “Wait 2 seconds, drive forward, spin the intake, stop.” In FRC, this is lethal. Blocking the main thread for even a single second violates the FRC networking envelope and crashes the robot.

To ensure code is evaluated safely every 20ms, WPILib enforces the Command-Based Paradigm.

1. Subsystems (The Nouns)

  • Subsystems own the physical motors and CAN IDs.
  • They provide methods like setVoltage(12.0) or getDistance().
  • They have a native periodic() method that fires exactly once every 20 milliseconds, usually dedicated to updating telemetry and logs.

2. Commands (The Verbs)

Commands are discrete actions (e.g., FireShooterCommand, ScoreAmpCommand).

  • Commands are ephemeral. They are instantiated, executed, and then die.
  • Every Command must Require a Subsystem. If FireShooterCommand requires the Shooter Subsystem, it locks it. If another routine tries to touch the Shooter, the original Command is forcefully interrupted!

3. The RobotContainer (The Glue)

You cannot blindly command motors in FRC directly from driver inputs. We use the RobotContainer.java class as a strict wiring harness.

// In RobotContainer.java
joystick.a().onTrue(new FireShooterCommand(shooterSubsystem));

When the driver presses the “A” button, the runtime scheduler grabs FireShooterCommand, injects the permanent shooterSubsystem into it, and schedules the execution cleanly alongside the rest of the robot loops.

4. The 20ms Control Loop

Here is what the RoboRIO is doing 50 times every single second during a match:

  1. Input Polling: Read Driver Station Xbox controllers and NetworkTables.
  2. Subsystem Periodic: Ask every subsystem to report its encoder/sensor values.
  3. Command Execution: For every currently active Command (like the Shooter spinning up), run its execute() block. Check if its isFinished() block is true; if yes, cleanly end it.
  4. Motor Writing: Flush all the newly requested voltage calculations over the CAN bus physically to the motors.



📖 Further Reading & External Resources