04 / Subsystems and Commands
Enums and Finite State Machines
Represent robot mechanism states explicitly instead of scattering booleans.
04 / Subsystems and Commands
Represent robot mechanism states explicitly instead of scattering booleans.
You will
This lesson is about making robot code scale. Architecture should reduce repeated decisions, not bury logic. Students should see why RobotHardware, subsystems, enums, state machines, and commands make the robot easier to test, reuse, and repair at an event.
Names like DOWN, HOVER, DEPOSIT, and SAFE are easier to reason about than raw servo positions. The attached arm subsystem uses enum states to make mechanism targets readable.
A finite state machine defines which states exist and how the robot moves between them. This prevents half-updated mechanisms where one servo thinks the robot is scoring and another thinks it is intaking.
Introduce structure after students have felt the pain of duplication. Move one mechanism into a subsystem, one repeated behavior into a command, and one set of raw positions into named states. The final code should read more like robot intent and less like a wiring diagram.
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.
ArmState.java · Java
public enum ArmState {
DOWN(0.12),
HOVER(0.18),
DEPOSIT(0.46),
UP(0.95);
public final double position;
ArmState(double position) {
this.position = position;
}
}
public void updateState(ArmState nextState) {
armState = nextState;
setProfileTarget(nextState.position);
}Architecture fails when responsibilities are unclear. If hardware is written from several places, commands fight. If states are scattered across booleans, impossible combinations appear. If lifecycle methods run out of order, telemetry and behavior disagree. The debugging path should follow ownership: input, command, subsystem state, hardware write.
Check your understanding
Why prefer enums over several unrelated booleans?
0 of 1 answered
References
Finished reading?
You'll move on to “Command-Based OpModes” next.