Robotics From Zero
Module: React To Change

State Machines

Finite state machines are the foundation of robot decision-making. Learn states, transitions, guards, and hierarchical FSMs.

10 min read

State Machines

A finite state machine (FSM) is a model of computation with a fixed set of states and rules for transitioning between them. At any moment, the robot is in exactly one state.

The Basics

Think of a traffic light:

Traffic light FSM — three states (RED, YELLOW, GREEN) with timer-driven transitions between them
The simplest state machine: a traffic light with three states and timer-based transitions. At any moment, the light is in exactly one state. This same concept scales to complex robot behaviors.

At any moment, the light is in exactly one state. When a condition is met (a timer expires), it transitions to a new state.

Anatomy of a State Machine

  1. States — discrete modes of operation (e.g., Idle, Navigating, Charging, Error)
  2. Transitions — arrows between states that define when to switch
  3. Guards — conditions that must be true for a transition to fire (e.g., battery < 20%)
  4. Actions — code that runs when entering/exiting a state or during a transition

Example: Delivery Robot

Let's design a simple delivery robot with four states:

State machine definition
class DeliveryRobot:
    def __init__(self):
        self.state = "IDLE"
        self.battery = 100
        self.mission_complete = False
 
    def update(self):
        # State: IDLE
        if self.state == "IDLE":
            if self.mission_received():
                self.transition_to("NAVIGATING")
 
        # State: NAVIGATING
        elif self.state == "NAVIGATING":
            self.move_toward_goal()
            if self.battery < 20:
                self.transition_to("CHARGING")
            elif self.reached_goal():
                self.mission_complete = True
                self.transition_to("IDLE")
 
        # State: CHARGING
        elif self.state == "CHARGING":
            self.dock_at_charger()
            self.battery += 1  # charge rate
            if self.battery >= 100:
                if self.mission_complete:
                    self.transition_to("IDLE")
                else:
                    self.transition_to("NAVIGATING")
 
    def transition_to(self, new_state):
        print(f"Transition: {self.state}{new_state}")
        self.on_exit(self.state)
        self.state = new_state
        self.on_enter(new_state)
 
    def on_enter(self, state):
        if state == "NAVIGATING":
            print("Starting navigation...")
        elif state == "CHARGING":
            print("Docking at charger...")
 
    def on_exit(self, state):
        if state == "NAVIGATING":
            print("Stopping navigation.")
 
Delivery robot FSM — IDLE, NAVIGATING, and CHARGING states with guard conditions on each transition
A delivery robot FSM with three states. Guard conditions on transitions prevent invalid state changes: the robot only charges when battery is low, and only navigates when a mission is received.

Guards and Transitions

A guard is a boolean condition that must be true for a transition to fire. In the example above:

  • battery < 20 is a guard on the transition from NAVIGATINGCHARGING
  • battery >= 100 and mission_complete is a guard on CHARGINGIDLE

Guards prevent invalid transitions. Without them, you'd need messy nested ifs to check every condition.

Hierarchical State Machines

Real robots need more structure. A hierarchical FSM lets you nest states within states.

Hierarchical state machine — parent states containing child states, with transitions at different levels
A hierarchical FSM nests states within states. The OPERATING parent contains NAVIGATING and CHARGING sub-states. A transition from OPERATING → ERROR applies to all children, eliminating duplicated transitions.

When you're in FOLLOWING_PATH, you're also in NAVIGATING and OPERATING. This means:

  • You can define a transition from any OPERATING state to ERROR (e.g., on sensor failure)
  • You don't need to duplicate "on sensor failure, go to error" in every sub-state
Tip

Hierarchical FSMs are also called Harel statecharts (after their inventor, David Harel). They're used in automotive, aerospace, and industrial robotics because they scale to thousands of states.

Entry and Exit Actions

You can run code when entering or exiting a state:

Entry and exit actions
def on_enter_NAVIGATING(self):
    self.planner.set_goal(self.current_mission.target)
    self.start_time = time.now()
 
def on_exit_NAVIGATING(self):
    self.planner.cancel()
    elapsed = time.now() - self.start_time
    self.log(f"Navigated for {elapsed}s")
 

Entry actions are perfect for initialization (start a timer, load a map, publish a status message).

Exit actions are perfect for cleanup (stop motors, log telemetry, release resources).

When State Machines Excel

State machines are ideal when:

  • Your robot has distinct modes (idle, moving, charging, error)
  • Transitions are event-driven (user presses button, sensor detects obstacle)
  • You need guaranteed behavior in each state (safety-critical systems)
State explosion — showing how adding states causes transitions to grow quadratically, making large FSMs unmanageable
The state explosion problem: with N states, you can have up to N² transitions. At 5 states it's manageable; at 20+ states the diagram becomes an unreadable web of arrows. This is the main limitation of flat state machines.

They're less ideal when:

  • You have many small, independent behaviors running in parallel
  • Behavior needs to be composed from reusable pieces
  • Transitions depend on complex logic rather than simple guards

That's where behavior trees come in (next lesson).

What's Next?

State machines give you clear, testable, structured behavior. But what if you have many small behaviors that need to run in parallel or be composed? That's where behavior trees shine. In the next lesson, we'll explore behavior trees and how they differ from state machines.

Got questions? Join the community

Discuss this lesson, get help, and connect with other learners on r/softwarerobotics.

Join r/softwarerobotics

Further Reading

Related Lessons

Discussion

Sign in to join the discussion.