05 / From Timed Steps to Actions
Autonomous State Machines
Use states to sequence non-blocking autonomous routines.
05 / From Timed Steps to Actions
Use states to sequence non-blocking autonomous routines.
You will
This lesson is about sequencing decisions without a human driver. Autonomous code must know where it starts, what it is trying to accomplish, how each step ends, and what safe behavior happens when a step fails.
A state machine names each part of autonomous. Instead of one long chain of sleeps, the loop checks the current state, does the needed work, and transitions when the step is complete.
When autonomous code avoids long blocking calls, it can keep updating telemetry, checking sensors, and stopping safely if the OpMode ends.
Teach autonomous as a progression from timed steps to encoder checks to state machines and actions. Each layer should preserve the same core habits: explicit state, telemetry for the active step, timeout or finish condition, and a final safe stop.
For this specific lesson, students should first restate the goal in robot terms, then identify the value or behavior they expect to observe, then run the smallest test that proves the idea. The lesson should feel like a guided lab: predict, run, observe, explain, and only then extend.
AutoStateMachine.java · Java
enum AutoState {
DRIVE_TO_SPIKE,
DROP_PIXEL,
PARK,
DONE
}
AutoState state = AutoState.DRIVE_TO_SPIKE;
while (opModeIsActive() && state != AutoState.DONE) {
switch (state) {
case DRIVE_TO_SPIKE:
driveForward();
if (atTarget() || runtime.seconds() > 2.0) state = AutoState.DROP_PIXEL;
break;
case DROP_PIXEL:
openClaw();
state = AutoState.PARK;
break;
case PARK:
strafeToPark();
if (atPark()) state = AutoState.DONE;
break;
}
}Autonomous failures are easier to diagnose when the robot reports why it moved on or stopped. Print the selected branch, active state, elapsed time, target, measurement, and stop reason. If those values are missing, the team is debugging a story with half the pages torn out.
Check your understanding
What does an autonomous state machine make easier to debug?
0 of 1 answered
References
Finished reading?
You'll move on to “Actions and Sequencing” next.