Using State Machines for Reliable FTC Code

intermediate65 min

Why state machines

Long if chains are hard to follow. A state machine keeps one active step (drive, turn, place) and moves to the next when a condition is met.

What is a state machine?

Your code tracks a currentState enum. Each loop runs the logic for that state and updates the enum when sensors, timers, or buttons say to advance.

See gm0: Finite State Machines.

Simple drive–turn–stop example

@TeleOp
public class StateMachineOpMode extends LinearOpMode {
    private enum State { DRIVE_FORWARD, TURN, STOP }
    private State currentState = State.DRIVE_FORWARD;
    private DcMotor leftMotor, rightMotor;
    @Override
    public void runOpMode() {
        leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
        rightMotor = hardwareMap.get(DcMotor.class, "right_drive");
        waitForStart();
        while (opModeIsActive()) {
            switch (currentState) {
                case DRIVE_FORWARD:
                    leftMotor.setPower(0.5);
                    rightMotor.setPower(0.5);
                    if (leftMotor.getCurrentPosition() > 1000) {
                        currentState = State.TURN;
                    }
                    break;
                case TURN:
                    leftMotor.setPower(0.5);
                    rightMotor.setPower(-0.5);
                    if (rightMotor.getCurrentPosition() < -500) {
                        currentState = State.STOP;
                    }
                    break;
                case STOP:
                    leftMotor.setPower(0);
                    rightMotor.setPower(0);
                    break;
            }
            telemetry.addData("State", currentState);
            telemetry.update();
        }
    }
}

Multiple subsystems

One state machine can sequence lift, pivot, and place steps so mechanisms do not fight each other.

Lift and pivot example

@TeleOp
public class MultiSubsystemStateMachineOpMode extends LinearOpMode {
    private enum State { START, LIFT_UP, PIVOT_OUT, PLACE, RETRACT, STOP }
    private State currentState = State.START;
    private DcMotor liftMotor;
    private Servo pivotServo;
    @Override
    public void runOpMode() {
        liftMotor = hardwareMap.get(DcMotor.class, "lift_motor");
        pivotServo = hardwareMap.get(Servo.class, "pivot_servo");
        waitForStart();
        while (opModeIsActive()) {
            switch (currentState) {
                case START:
                    // Wait for button to start sequence
                    if (gamepad1.a) {
                        liftMotor.setTargetPosition(1000);
                        liftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
                        liftMotor.setPower(0.5);
                        currentState = State.LIFT_UP;
                    }
                    break;
                case LIFT_UP:
                    if (!liftMotor.isBusy()) {
                        pivotServo.setPosition(1.0); // Pivot out
                        currentState = State.PIVOT_OUT;
                    }
                    break;
                case PIVOT_OUT:
                    // Wait for pivot to reach position (simulate with timer or sensor)
                    sleep(500);
                    currentState = State.PLACE;
                    break;
                case PLACE:
                    // Place object (e.g., open claw)
                    // ... add code for placing ...
                    currentState = State.RETRACT;
                    break;
                case RETRACT:
                    pivotServo.setPosition(0.0); // Pivot back
                    liftMotor.setTargetPosition(0);
                    liftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
                    liftMotor.setPower(0.5);
                    if (!liftMotor.isBusy()) {
                        currentState = State.STOP;
                    }
                    break;
                case STOP:
                    liftMotor.setPower(0);
                    // Sequence complete
                    break;
            }
            telemetry.addData("State", currentState);
            telemetry.addData("Lift Position", liftMotor.getCurrentPosition());
            telemetry.addData("Pivot Position", pivotServo.getPosition());
            telemetry.update();
        }
    }
}

Debugging with states

Log currentState on telemetry every loop. When behavior stalls, you know which case to inspect.

State telemetry

telemetry.addData("Current State", currentState);
telemetry.addData("Left Encoder", leftMotor.getCurrentPosition());
telemetry.update();

State machine tips

  • Keep each state simple and focused on one task.
  • Use enums for state names to avoid typos and improve readability.
  • Avoid deeply nested logic inside states.
  • Use telemetry to track state transitions.
  • Break up large state machines into smaller, manageable pieces if needed.

Practice: Build Your Own State Machine

Write an OpMode that uses a state machine to perform a sequence of actions (e.g., drive forward, pause, then reverse). Use telemetry to display the current state and test your transitions.

  • Define at least three states for your robot.
  • Implement transitions based on encoder values or timers.
  • Add telemetry to show the current state and relevant sensor data.

Further Reading Resources