Autonomous State Machines
Represent autonomous as named nonblocking steps.
Autonomous FoundationsAutonomous
Represent autonomous as named nonblocking steps.
In this lesson, you will:
A state machine turns autonomous into named steps that run over many loops. It avoids long blocking code and makes it obvious where the routine is stuck.
The active state is the robot’s current promise. It should have one responsibility and one clear reason to transition.
Write the enum, initialize the first state, switch on state in the loop, and transition only when a condition or timeout is met.
AutoState.javaJava
switch (state) {
case DRIVE:
driveForward();
if (atTarget() || runtime.seconds() > 2.0) state = AutoState.DROP;
break;
case DONE:
stopDrive();
break;
}If auto gets stuck, print state, timer, target, measurement, and transition condition. If a state does many jobs, split it.
Rewrite a timed auto as DRIVE, DROP, PARK, DONE states.
Check your understanding before moving on.
0 of 2 answered