Skip to content
learnFRC.org
programminglesson30 min

Advanced Command Compositions

Fluent composition patterns: sequence, parallel, race, deadline, conditional branching, and safety timeouts.

What you will learn

  • Master the four composition primitives (sequence, parallel, race, deadline).
  • Apply safety timeouts using withTimeout() to prevent frozen command sequences.
  • Implement dynamic conditional branching using Commands.either() and select().

Prerequisites:Stage 05 - Vision Systems & Field Localization

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.


The Commands factory class provides four fundamental composition wrappers:

  • 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].
  • 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.
  • 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)).

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 intake
Command 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
);

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.

Subsystem Collisions

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.


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 RobotContainer initialization.
  • Cause: Two commands passed to Commands.parallel, race, or deadline share a common subsystem requirement.
  • Fix: Review requirements of all child commands in the group.
  • 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.

  1. Implement the composite scoring sequence in RobotContainer.
  2. Run simulation (F5).
  3. Trigger the sequence and verify in the console that each stage starts only after the previous stage finishes.

Verified Sources & Standards

Evergreen Standard Verified: Sep 25, 2026