Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 38 ยท ROS and Software Architecture

Robot Software Architecture and Behavior Design

Compare sense-plan-act, subsumption and hybrid architectures, then build a finite state machine and a behavior tree in runnable Python for a patrol robot.

Writing the individual pieces of a robot (a planner, a filter, a controller) is only half of the job. The other half is deciding how they fit together: what runs when, what takes priority, and what happens when something fails. A robot with excellent algorithms and a poor architecture will freeze, oscillate between two behaviors, or ignore its battery until it dies in the hallway.

This lesson covers the classic architecture styles, then two tools for organizing behavior that you can run: finite state machines and behavior trees.

Architecture styles

Sense, Plan, Act was the first approach: read the sensors, build a world model, plan, execute, repeat. It is clean, but slow: if the plan takes two seconds the robot is blind and helpless for two seconds.

Reactive control goes the other way. In Brooks' subsumption architecture, behaviors are stacked in layers ("avoid obstacles" at the bottom, "wander" above it, "explore" above that); higher layers can suppress or override lower ones. Reflexes are fast and robust, but long-term goals are hard to express.

Hybrid (three-layer) architectures combine them, and are the most common design today:

Layer Time scale Job Example
Deliberative Seconds to minutes Task planning, route to the goal "Go to room 12, then the dock"
Sequencing About a second Choose and switch behaviors, handle failures State machine, behavior tree
Reactive / control Milliseconds Obstacle avoidance, motor loops, safety PID, local planner, emergency stop

Three layers

Deliberative Slow. Plans routes and tasks.
Sequencing Picks the current behavior.
Reactive Fast. Avoids, brakes, controls.

Lower layers must keep working when upper layers are slow.

1 / 2

A good rule: the faster a loop must be, the lower it lives, and the less it depends on others. A motor controller at 1 kHz must not wait on a planner that takes 200 ms.

Finite state machines

A finite state machine (FSM) has a fixed set of states (what the robot is doing) and transitions (events that move it from one state to another). It is easy to draw, test, and reason about, which is why it is the default tool for simple robot behavior.

A patrol robot: it patrols waypoints; if the battery is low it goes to charge; while charging it waits; when full it resumes. If an obstacle is detected, it stops and waits.

stateDiagram-v2
  [*] --> Patrol
  Patrol --> GoCharge: battery < 20
  GoCharge --> Charging: at dock
  Charging --> Patrol: battery >= 95
  Patrol --> Blocked: obstacle
  Blocked --> Patrol: path clear

A patrol robot as a state machine.

Python A finite state machine for a patrol robot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every transition is written down and loggable, which makes debugging easy. The weakness shows as the robot grows: states multiply (state explosion), and shared behavior, such as "from any state, if the emergency stop is pressed, stop," has to be added to every state. Hierarchical state machines (used by SMACH and other libraries) help; behavior trees take a different route.

Behavior trees

A behavior tree (BT) is a tree that is ticked (evaluated) from the root, many times per second. Each node returns SUCCESS, FAILURE, or RUNNING. Leaves are actions ("move to door") and conditions ("battery OK?"). Inner nodes control the flow:

Node Behavior
Sequence (->) Tick children in order; stop and return FAILURE at the first failure; SUCCESS if all succeed
Fallback / Selector (?) Tick children in order; stop and return SUCCESS at the first success; FAILURE if all fail
Decorator Wraps one child (invert result, retry N times, timeout)
Parallel Ticks several children at once

BTs are modular: subtrees can be reused, priority is explicit (earlier children win), and the whole thing re-evaluates every tick, so a high-priority condition such as a low battery naturally interrupts lower-priority work. Nav2 and many game engines use them (the BehaviorTree.CPP library is the common choice in ROS 2).

Python A tiny behavior tree: Fallback, Sequence, conditions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the tree top-down: "If the battery is fine, then (if the path is clear, patrol, else wait); otherwise charge." Every tick starts at the root, so when the battery drops below 20 the robot immediately switches to charging, with no extra transitions written. In a real BT library, actions that take time return RUNNING and are resumed on later ticks.

FSM or behavior tree?

State machine Behavior tree
Good for Few states, clear modes (boot, run, error) Many tasks with priorities, reusable parts
Scaling Transitions grow quickly Subtrees compose
Interrupts Add a transition from every state Higher-priority branch preempts automatically
Tools SMACH, YASMIN, transitions BehaviorTree.CPP, py_trees, Nav2

Many systems use both: a small FSM for the robot's lifecycle (boot, ready, error, shutdown) and BTs for its tasks.

Engineering concerns

  • Real-time loops. Motor control runs at fixed rates. On Linux that may mean a real-time kernel, or a microcontroller (a Teensy, an STM32) running the fast loop while a computer runs the high-level code over a serial or CAN link.
  • Timeouts and watchdogs. Every command needs an expiry. If the controller stops hearing /cmd_vel for 0.5 s, it must stop the robot.
  • Fault handling. Define what happens when a sensor stops, localization is lost, or a planner fails: retry, replan, ask for help, or stop safely. See Robot Safety and Ethics.
  • Logging and replay. Record sensor data and decisions so every failure can be reproduced offline.
  • Simulation first. Test the same architecture against a simulator, as in the mini-ROS examples, before the real robot moves.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does sense-plan-act alone perform poorly for fast-moving robots?

    If planning takes seconds, the robot acts on stale information. Reactive layers fill the gap.
  2. 2. In a behavior tree, what does a Sequence return if its second child fails?

    A Sequence stops at the first child that does not succeed and returns that result.
  3. 3. What is a main advantage of behavior trees over a simple state machine?

    BTs re-evaluate from the root each tick, so higher-priority branches interrupt naturally, and subtrees can be reused.
  4. 4. A controller stops receiving velocity commands. What should a safe design do?

    A watchdog timeout turns a lost command stream into a safe stop instead of a runaway robot.

Summary

  • Hybrid three-layer architectures put slow planning above a sequencing layer and fast reactive control at the bottom.
  • Finite state machines are simple and explicit but suffer state explosion.
  • Behavior trees tick from the root with Sequence, Fallback and decorators, giving priorities and modularity.
  • Use watchdogs, timeouts, logging and simulation to make behavior safe and debuggable.

Next: Robot Applications