When an intake, elevator, and pivoting arm share physical space, uncoordinated movements can collide mechanisms into the chassis or each other.
This lesson teaches how to coordinate complex superstructures using State Machines and Software Interlocks.
1. Defining the Superstructure States
Section titled “1. Defining the Superstructure States”Rather than commanding individual motor setpoints across multiple buttons, define high-level robot states using a Java enum:
package frc.robot.subsystems;
public enum SuperstructureState { STOW(Inches.of(0.0), Degrees.of(0.0)), GROUND_INTAKE(Inches.of(0.0), Degrees.of(-15.0)), HANDOFF(Inches.of(4.0), Degrees.of(10.0)), LOW_SCORE(Inches.of(18.0), Degrees.of(45.0)), HIGH_SCORE(Inches.of(42.0), Degrees.of(80.0));
public final Distance elevatorHeight; public final Angle armAngle;
SuperstructureState(Distance height, Angle angle) { this.elevatorHeight = height; this.armAngle = angle; }}2. Forbidden Zones and Transit Interlocks
Section titled “2. Forbidden Zones and Transit Interlocks”In physical reality, the robot cannot always move directly in a straight line from State A to State B.
Example Mechanical Invariant:
Section titled “Example Mechanical Invariant:”The arm cannot rotate past 30° unless the elevator is above 12 inches.
If the robot is at GROUND_INTAKE (Elevator 0”, Arm -15°) and the driver presses HIGH_SCORE (Elevator 42”, Arm 80°):
- Naive approach: Move both simultaneously. The arm swings up, collides with the elevator crossbeam at 5 inches, and strips gear teeth.
- Interlocked approach: Ascend elevator past 12 inches first, then allow the arm to pivot.
public class SuperstructureSubsystem extends SubsystemBase { private final ElevatorSubsystem m_elevator; private final ArmSubsystem m_arm;
public Command setDesiredStateCommand(SuperstructureState target) { return Commands.defer(() -> { boolean requiresIntermediateTransit = (m_elevator.getHeight().in(Inches) < 12.0) && (target.armAngle.in(Degrees) > 30.0);
if (requiresIntermediateTransit) { // Transit path: Elevate first, then pivot arm return Commands.sequence( m_elevator.moveToHeightCommand(Inches.of(14.0)), Commands.parallel( m_elevator.moveToHeightCommand(target.elevatorHeight), m_arm.moveToAngleCommand(target.armAngle) ) ); } else { // Safe to move directly in parallel return Commands.parallel( m_elevator.moveToHeightCommand(target.elevatorHeight), m_arm.moveToAngleCommand(target.armAngle) ); } }, Set.of(m_elevator, m_arm)); }}Commands.defer() evaluates the lambda dynamically when the command is scheduled, rather than when the robot boots up. This ensures the safe path is chosen based on the actual physical position of the mechanisms at the moment the driver presses the button.
3. Emergency Software Stops (E-Stop Safe Mode)
Section titled “3. Emergency Software Stops (E-Stop Safe Mode)”If a sensor cable breaks or a CANcoder loses magnet communication, closed-loop state machines can behave unpredictably. Implement a manual override trigger:
// Driver presses Back + Start simultaneously to kill automatic state machinesm_driver.back().and(m_driver.start()).onTrue( Commands.runOnce(() -> { CommandScheduler.getInstance().cancelAll(); m_elevator.stop(); m_arm.stop(); }));Common Failure Modes
Section titled “Common Failure Modes”1. State Machine Jams in Transit
Section titled “1. State Machine Jams in Transit”- Symptom: The elevator moves up, but the arm never pivots.
- Cause: Elevator tolerance check (
isAtHeight) was set too strictly, so the sequence never advanced to step 2. - Fix: Use generous tolerances on intermediate transit waypoints (e.g.,
±1.0 inch).
2. Mechanism Drops Under Gravity During State Change
Section titled “2. Mechanism Drops Under Gravity During State Change”- Symptom: As soon as the command advances to step 2, the elevator drops 2 inches before recovering.
- Cause: Default command on the elevator was not configured, or holding voltage was dropped during command handoff.
- Fix: Verify elevator default command holds current height.
Check Your Work
Section titled “Check Your Work”- Test transitions between all state pairs in desktop simulation.
- In the Mechanism2d view, verify the carriage always clears the crossbeam boundary before the arm rotates outward.
Verified Sources & Standards
- WPILib State Machine Patterns & Subsystem Coordinationdocs.wpilib.orgWPILib Standard