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)orgetDistance(). - 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
FireShooterCommandrequires 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.javajoystick.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:
- Input Polling: Read Driver Station Xbox controllers and NetworkTables.
- Subsystem Periodic: Ask every subsystem to report its encoder/sensor values.
- Command Execution: For every currently active Command (like the Shooter spinning up), run its
execute()block. Check if itsisFinished()block is true; if yes, cleanly end it. - Motor Writing: Flush all the newly requested voltage calculations over the CAN bus physically to the motors.
📖 Further Reading & External Resources
- WPILib “Zero to Robot” Guide - The official foundational pipeline for FRC control systems.
- FRC 0 to Auto Youtube Series - Outstanding video tutorials exploring command-based programming for novices.
- WPILib Command-Based Documentation - Deep dive into Subsystem and Command scheduling architecture.