Complex robot actions (such as acquiring a game piece, indexing it, spinning up a shooter, and firing) require precise coordination across multiple subsystems.
Instead of writing monolithic state machines with dozens of mutable boolean flags, modern WPILib builds multi-mechanism routines using Command Compositions.
1. The Four Composition Primitives
Section titled “1. The Four Composition Primitives”The Commands factory class provides four fundamental composition wrappers:
1. Commands.sequence(Command... commands)
Section titled “1. Commands.sequence(Command... commands)”- Behavior: Runs commands one after another in order.
- Finished When: The final command finishes.
- Interruption: If cancelled, whichever child command is currently running is cancelled.
- Use Case: Multi-step routines:
[DeployIntake] -> [RunFeeders] -> [Stow].
2. Commands.parallel(Command... commands)
Section titled “2. Commands.parallel(Command... commands)”- Behavior: Runs all child commands concurrently.
- Finished When: All child commands have finished.
- Use Case: Simultaneous actions: Move arm while elevating carriage.
3. Commands.deadline(Command deadline, Command... followers)
Section titled “3. Commands.deadline(Command deadline, Command... followers)”- Behavior: Runs all commands concurrently.
- Finished When: The designated deadline command finishes. When the deadline ends, all other follower commands are immediately interrupted.
- Use Case: Drive somewhere for a fixed distance while spinning intake rollers: when the drive arrives, stop rollers immediately.
4. Commands.race(Command... commands)
Section titled “4. Commands.race(Command... commands)”- Behavior: Runs all commands concurrently.
- Finished When: Any child command finishes. Whichever finishes first interrupts all other children.
- Use Case: Race a task against a timeout:
Commands.race(findGamePieceCommand, Commands.waitSeconds(3.0)).
2. Complete Scoring Routine Example
Section titled “2. Complete Scoring Routine Example”Here is a production sequence coordinating an intake, elevator, arm, and shooter:
public Command automatedScoreSequence() { return Commands.sequence( // Step 1: Concurrently bring elevator and arm to scoring positions Commands.parallel( m_elevator.moveToHeightCommand(Inches.of(36.0)), m_arm.moveToAngleCommand(Degrees.of(75.0)), m_shooter.spinUpCommand(RotationsPerSecond.of(70.0)) ),
// Step 2: Wait until flywheel is at speed AND arm is at angle Commands.waitUntil(() -> m_shooter.isAtSpeed() && m_arm.isAtAngle()),
// Step 3: Run feeder wheels for 0.5 seconds to launch game piece m_feeder.feedCommand().withTimeout(Seconds.of(0.5)),
// Step 4: Concurrently stow all mechanisms Commands.parallel( m_shooter.stopCommand(), m_arm.moveToAngleCommand(Degrees.of(0.0)), m_elevator.moveToHeightCommand(Inches.of(0.0)) ) );}3. Conditional Branching (Commands.either & select)
Section titled “3. Conditional Branching (Commands.either & select)”Robot logic often needs to take different actions based on sensor state:
// If game piece is detected, score immediately; otherwise run intakeCommand smartAction = Commands.either( scoreSequenceCommand(), intakeCommand(), m_feeder::hasGamePiece);For multiple branches (e.g., scoring at low, mid, or high heights based on an operator knob):
Command heightBranch = Commands.select( Map.of( ScoringLevel.LOW, m_elevator.moveToHeightCommand(Inches.of(12.0)), ScoringLevel.MID, m_elevator.moveToHeightCommand(Inches.of(28.0)), ScoringLevel.HIGH, m_elevator.moveToHeightCommand(Inches.of(44.0)) ), m_operator::getSelectedLevel);4. The Requirement Conflict Rule
Section titled “4. The Requirement Conflict Rule”A command composition cannot contain two parallel commands that require the same subsystem:
// ILLEGAL: Throws IllegalArgumentException on robot boot!Command broken = Commands.parallel( m_arm.moveToAngleCommand(Degrees.of(45)), m_arm.manualJogCommand(() -> 0.5));Both commands require m_arm. Because a subsystem can only be commanded by one entity at a time, parallel composition of conflicting requirements is prohibited.
If you must read sensors from a subsystem while another command controls its motors, do not require the subsystem in the sensor reading command. Only commands that apply motor voltage should require the subsystem.
Common Failure Modes
Section titled “Common Failure Modes”1. IllegalArgumentException: Multiple commands in a parallel group require the same subsystem
Section titled “1. IllegalArgumentException: Multiple commands in a parallel group require the same subsystem”- Symptom: Code crashes on startup during
RobotContainerinitialization. - Cause: Two commands passed to
Commands.parallel,race, ordeadlineshare a common subsystem requirement. - Fix: Review requirements of all child commands in the group.
2. Routine Stalls Indefinitely at Step 2
Section titled “2. Routine Stalls Indefinitely at Step 2”- Symptom: The sequence starts, but never fires the game piece.
- Cause:
m_shooter.isAtSpeed()was never satisfied because target RPM was set higher than the battery voltage could sustain. - Fix: Wrap
waitUntil()with.withTimeout(Seconds.of(2.0))to ensure the routine proceeds even if recovery is marginally slower than expected.
Check Your Work
Section titled “Check Your Work”- Implement the composite scoring sequence in
RobotContainer. - Run simulation (F5).
- Trigger the sequence and verify in the console that each stage starts only after the previous stage finishes.
Verified Sources & Standards
- WPILib Standard