ROS 2 Learning Path  ยท  Illustrated Educational Book

Chapter Twelve: Behavior Tree and Lifecycle Nodes โ€” The Intelligence Behind Nav2

Where Nav2 stops being a linear program and starts thinking
Prerequisite: Chapter 11 (Navigation2)
Ongoing project: ARCHO robot
Concepts: BT, Lifecycle, Recovery
Reading time: 120โ€“140 minutes
What we'll cover in this chapter 12.1The problem with linear programs 12.2What a Behavior Tree is 12.3The three main node types 12.4Status and Tick 12.5Nav2's real Behavior Tree for ARCHO 12.6Lifecycle Nodes: why ROS 2 differs from ROS 1 12.7Four main states of a Lifecycle Node 12.8Lifecycle Manager: Nav2's project manager 12.9Summary, glossary, and exercises

12.1The problem with linear programs

Suppose you give ARCHO a mission: "Go to shelf number 12." Now, along the way, any of these could happen:

If ARCHO's logic is just a simple linear program:

flowchart LR A["Start"] --> B["Move"] --> C["Goal"] style B fill:#fdeeec,stroke:#d64a3c
โš ๏ธ Why this isn't enough

In a linear program, the first unexpected event stops the entire program. If you write Move โ†’ Pick โ†’ Return and the first step fails, the whole chain collapses. But in the real world, an industrial robot needs to be able to make decisions โ€” not just execute.

ROS 2's solution to this problem is a structure called a Behavior Tree.

12.2What a Behavior Tree is

๐Ÿ“– Definition

A Behavior Tree is a tree-shaped decision-making structure that lets a robot choose the best behavior based on the environment's momentary conditions โ€” instead of blindly executing a fixed chain of commands.

๐Ÿง  A simple analogy

When you leave the house in the morning, this happens in your mind: "Is it raining? If yes, take an umbrella; if not, go without one." This is exactly a Behavior Tree โ€” a condition, and depending on its result, a different action.

Because decisions branch, a tree structure makes sense:

flowchart TB R["Mission"] --> A["Go To Goal"] R --> B["Recharge Battery"] R --> C["Wait"] R --> D["Recover"] style R fill:#eef0ff,stroke:#3d4bf5

12.3The three main node types

Almost every real Behavior Tree is built from a combination of these three node types:

Node TypeFunctionExample
ConditionOnly asks a question; the output is only Success or FailureBattery OK? Goal Reached?
ActionPerforms a real taskCompute Path, Follow Path, Rotate, Clear Costmap
ControlDecides which branch executes โ€” the most important part of a BTSequence, Fallback, Parallel

Sequence Node (โ†’)

All steps must succeed one after another; if one fails, the rest never execute.

flowchart LR S["Sequence โ†’"] --> P1["Compute Path"] --> P2["Follow Path"] --> P3["Success"] style S fill:#eef0ff,stroke:#3d4bf5

Fallback Node โ€” or Selector (?)

If the first option fails, the next one is tried.

flowchart TB F["Navigate ?"] --> R1["Main Route"] F --> R2["Secondary Route"] F --> R3["Recovery"] style F fill:#f4effe,stroke:#8b5cf6

Parallel Node

Runs several tasks at once โ€” ARCHO doesn't just move, it simultaneously monitors battery, LiDAR, and the goal.

flowchart TB P["Parallel"] --> M1["Move"] P --> M2["Monitor Battery"] P --> M3["Watch Obstacles"] style P fill:#eafaf3,stroke:#0e9e6e

12.4Status and Tick

At any given moment, every node returns one of these three states:

StatusMeaning
SuccessThe task was completed successfully
FailureThe task was not completed
RunningStill in progress โ€” for example, ARCHO is still moving
๐Ÿ“– What a Tick is

A Behavior Tree doesn't run just once; it's checked repeatedly and continuously (for example, 20 times per second) from the root. Each check is called a Tick. On every Tick, the same questions are asked again: Has the goal changed? Is there a new obstacle? Is the battery low? Is the path still valid?

๐Ÿ”ง Why a Behavior Tree beats a pile of if-else statements

If you tried to write all these conditions with if battery, if obstacle, if planner, if recovery, if localization, if timeout, and if sensor, the code would very quickly turn into unmaintainable chaos. A Behavior Tree organizes this same complexity into a visual, extensible structure.

12.5Nav2's real Behavior Tree for ARCHO

Simplified version:

flowchart LR A["Navigate"] --> B["Compute Path"] --> C["Follow Path"] --> D["Goal Reached?"]

But the real version that Nav2 executes has a more complete shape:

flowchart TB N["Navigate"] --> C1["Compute Path"] N --> C2["Follow Path"] N --> C3["Check Goal"] N --> C4["Check Battery"] N --> C5["Replan"] N --> C6["Recovery"] style N fill:#eef0ff,stroke:#3d4bf5 style C6 fill:#fdeeec,stroke:#d64a3c

Recovery Behavior: when the Planner gets stuck

If the Global Planner can't find a path, the BT tries a recovery chain instead of collapsing:

flowchart TB A["Rotate"] -->|failure| B["Backup"] B -->|failure| C["Try Again"] C -->|failure| D["Mission Failed"] style D fill:#fdeeec,stroke:#d64a3c

Replanning: when a forklift blocks an aisle

If the Planner had previously found a path through an aisle, but that aisle is now blocked, the Behavior Tree decides on its own that the Planner should run again and compute a new path โ€” without a single new line of code being written.

Low battery example

If ARCHO's battery drops to 8%, the BT can decide โ€” without the programmer writing hundreds of if conditions โ€” to cancel the mission and head to the charging station.

๐ŸŒ ARCHO's complete mission with a BT

The mission "go to shelf 12," in BT form, looks something like this: Battery OK? โ†’ Localization OK? โ†’ Compute Path โ†’ Follow Path โ†’ Goal Reached? โ†’ Recovery. If a worker crosses the path midway, the Planner recomputes the route and ARCHO continues; if that doesn't work either, Recovery kicks in โ€” it backs up a bit, tries again, or ultimately cancels the mission.

Nav2's complete architecture with the Behavior Tree at its center Goal Behavior Tree Planner Controller Recovery cmd_vel
The Behavior Tree sits at the center of decision-making, and on every Tick decides whether the Planner, Controller, or Recovery should be active.

12.6Lifecycle Nodes: why ROS 2 differs from ROS 1

Suppose you turn ARCHO on. Does it make sense for the LiDAR to start working immediately, the Planner to run, the Controller to issue commands, and the motors to move โ€” without any guarantee that everything is healthy? Absolutely not.

๐Ÿง  Like an airplane's pre-flight checklist

A pilot doesn't just hit the power button and take off. First the engine, fuel, hydraulic system, and instruments are checked, and only then is takeoff cleared. ROS 2 implements exactly this same logic for critical nodes.

In ROS 1, nodes usually only had two states: Start and Running. If something went wrong, it was hard to manage because there was no controlled intermediate state. ROS 2 solved this with the Lifecycle Node.

๐Ÿ“– Definition

A Lifecycle Node is a node with well-defined states that only becomes fully active once all the necessary conditions are met โ€” not the instant it's powered on.

12.7Four main states of a Lifecycle Node

stateDiagram-v2 [*] --> Unconfigured: Create Unconfigured --> Inactive: Configure() Inactive --> Active: Activate() Active --> Inactive: Deactivate() Inactive --> Unconfigured: Cleanup() Unconfigured --> Finalized: Shutdown() Inactive --> Finalized: Shutdown() Active --> Finalized: Shutdown() Finalized --> [*]

Note: in addition to these four main states (Unconfigured, Inactive, Active, Finalized), each transition between them (like Configure or Activate) actually passes through a temporary Transition State; these transition states are not shown in the simplified diagram above.

StateMeaningLiDAR Driver Example
UnconfiguredThe node exists, but isn't ready to work yetThe serial port hasn't been opened yet
InactiveThe node is ready but isn't doing anything yetThe LiDAR is ready but isn't publishing data yet
ActiveThe node is actually working โ€” publishing topics, responding to servicesLaserScan is published on /scan
FinalizedThe node has been fully and irreversibly shut down โ€” reachable from all three other statesComplete system shutdown
๐Ÿ”ง Why the Inactive state matters so much

Suppose the Planner is ready but Localization isn't ready yet. If the Planner became active right now, it would receive incorrect position data and generate wrong paths. The Inactive state lets the Planner stay "ready but quiet" until it's actually its turn.

โš ๏ธ Why activation order matters

If the Controller becomes active before Localization, ARCHO could move based on incorrect position data โ€” which, in a warehouse full of shelves and workers, is dangerous. The Lifecycle mechanism controls exactly this order: LiDAR โ†’ TF โ†’ Localization โ†’ Costmap โ†’ Planner โ†’ Controller.

12.8Lifecycle Manager: Nav2's project manager

In Navigation2, there's a central node called the Lifecycle Manager that acts like a project manager: it coordinates Configure โ†’ Activate โ†’ Monitor โ†’ Shutdown for all the critical nodes.

flowchart TB LM["Lifecycle Manager"] --> A["Map Server"] LM --> B["AMCL"] LM --> C["Planner Server"] LM --> D["Controller Server"] LM --> E["BT Navigator"] LM --> F["Behavior Server"] style LM fill:#3d4bf5,color:#fff
dev@archo:~$ ros2 lifecycle get /amcl active [3] dev@archo:~$ ros2 lifecycle get /planner_server active [3] dev@archo:~$ ros2 lifecycle set /planner_server deactivate Transitioning successful
๐ŸŒ A real industrial scenario

The night shift starts at a factory. ARCHO powers on; the Lifecycle Manager checks that the LiDAR, IMU, and encoders are healthy. AMCL becomes active and localizes the robot. The Planner and Controller become active and ARCHO starts working. Midway through the shift, the LiDAR temporarily goes offline โ€” the Lifecycle Manager immediately moves the Planner and Controller to Inactive so ARCHO doesn't make a dangerous move. Once the LiDAR is healthy again, the nodes are reactivated in the same logical order. This behavior keeps the system predictable and safe.

๐Ÿง  Behavior Tree and Lifecycle complement each other โ€” they aren't the same thing

The Behavior Tree decides what to do right now. Lifecycle decides which node is even allowed to run. One is the brain of moment-to-moment decision-making; the other is the safety framework for startup and shutdown.

12.9Chapter Twelve summary

Now the complete architecture of Navigation2 is clear to you: Localization, Map, Costmap, Global Planner, and Local Controller (Chapter 11), together with the Behavior Tree that coordinates all of these, and Lifecycle Nodes that guarantee each component only becomes active when it's truly ready. Together, these six pieces let ARCHO cope in a real environment โ€” with moving obstacles, route changes, sensor errors, and unexpected conditions.

โœ… Learning checkpoint
  • I can explain the difference between Sequence, Fallback, and Parallel nodes with an example.
  • I know what a Tick is and why a BT doesn't run just once.
  • I can explain how Recovery Behavior prevents Planner failure.
  • I can name the four main states of a Lifecycle Node in order.
  • I know why the Lifecycle Manager is essential for safely starting up Nav2.
๐ŸŒ Connection to the main project

ARCHO Project now has a complete Nav2 architecture: a BT Navigator that coordinates moment-to-moment decision-making, and a Lifecycle Manager that controls the safe startup order and recovery of all critical nodes.

What the next chapter adds

In Chapter Thirteen we move on to MoveIt 2 โ€” if ARCHO also has a robotic arm, this is where we learn how that arm plans its own motion.

Chapter Twelve glossary

Behavior Tree (BT)
A tree-shaped structure for robot decision-making based on the environment's momentary conditions.
Sequence Node
A control node whose children must all succeed in order for it to succeed.
Fallback (Selector) Node
A control node that picks the first child that succeeds.
Tick
Each repeated, periodic check of the behavior tree from the root.
Recovery Behavior
An alternative behavior (like rotating or backing up) used when the main path fails.
Lifecycle Node
A node with well-defined states that only becomes active in a controlled order.
Lifecycle Manager
A management node that coordinates the Configure/Activate/Deactivate order of all Lifecycle Nodes.

Chapter Twelve common mistakes โ€” summary