Logic & State Machines
Loading simulation…
flagWhat you'll discover
- arrow_forwardDefine states, transitions and events in an FSM
- arrow_forwardTrace the vacuum's behavior through the live state diagram
- arrow_forwardPredict what the battery-low event does in every state
- arrow_forwardArgue why FSMs are easier to debug than tangled if-else code
What is a finite state machine?
A finite state machine (FSM) organizes behavior into a fixed set of states — distinct modes like SEARCH or DOCK — with the rule that the robot is in exactly one state at any moment. Arrows called transitions connect states and fire when specific events occur: a bump, a timer expiring, a battery threshold.
The state diagram in the simulation is the entire program, drawn as a picture. The glowing node tells you precisely what the robot is doing and why.
The vacuum's brain, four states
SEARCH drives forward sweeping the floor. Hitting a wall fires the bump event, transitioning to BACKUP, which reverses for a fixed time, then hands off to TURN, which rotates a random angle before returning to SEARCH. This three-state cycle alone covers a whole room surprisingly well.
The fourth state, DOCK, is special: when battery drops below the threshold, every state yields to DOCK, and the robot steers toward its charger instead of cleaning.
Why not just use if-else?
You could write this as nested if-else statements, but as behaviors grow, the conditions tangle: was I already backing up? For how long? Did the bump happen while turning? Programmers call this spaghetti code, and it breeds bugs that only appear in rare sequences.
An FSM untangles it: each state owns its simple logic, transitions are explicit and listable, and you can test every state in isolation. Adding a new behavior means adding a node, not rewriting the whole tangle.
FSMs across engineering
Traffic lights, ATM menus, elevator logic, video game enemy AI, network protocols and spacecraft fault handling all run on state machines. NASA reviews mission FSM diagrams line by line, because a missing transition in space can end a mission.
Modern robotics layers FSMs into hierarchies and behavior trees, but the principle endures: make every mode explicit, make every transition deliberate, and the machine's behavior becomes something you can reason about, prove and trust.