ROS 2 Lyrical Luth · Lesson 63 of 86
Behavior Trees: BT Navigator
Behavior tree nodes, the navigate_to_pose tree and customizing behaviour.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain behavior trees: bt navigator in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Nav2 decides what to do with behavior trees (BTs): XML files that combine action nodes (ComputePathToPose, FollowPath, Spin, Wait, ClearEntireCostmap), control nodes (Sequence, Fallback, RecoveryNode, PipelineSequence) and decorators (RateController). The bt_navigator loads a tree and runs it for each navigation goal.
A Sequence runs children in order and stops at the first failure; a Fallback tries children until one succeeds. This makes the navigation logic visible and easy to change without recompiling. Start from Nav2's default tree, copy it, and edit small parts.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the behavior trees: bt navigator example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
<root main_tree_to_execute="MainTree">
<!-- Which tree to run. -->
<BehaviorTree ID="MainTree">
<!-- The tree definition. -->
<PipelineSequence name="NavigateWithReplanning">
<!-- Run children so the first keeps running while later ones execute. -->
<RateController hz="1.0"><ComputePathToPose goal="{goal}" path="{path}" planner_id="GridBased"/></RateController>
<!-- Replan once per second. -->
<FollowPath path="{path}" controller_id="FollowPath"/>
<!-- Follow the latest path. -->
</PipelineSequence>
<!-- End of the sequence. -->
</BehaviorTree>
<!-- End of the tree. -->
</root>
<!-- End of the file. -->What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Mobile robot
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Behavior Trees: BT Navigator. Expected result: The path is recomputed once per second
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
<root main_tree_to_execute="MainTree">
<BehaviorTree ID="MainTree">
<PipelineSequence name="NavigateWithReplanning">
<!-- TODO: replan once per second: wrap ComputePathToPose in a RateController with hz 1.0 -->
<FollowPath path="{path}" controller_id="FollowPath"/>
</PipelineSequence>
</BehaviorTree>
</root>Expected output
The path is recomputed once per secondStuck? Show the full solution (try it yourself first)
<root main_tree_to_execute="MainTree">
<BehaviorTree ID="MainTree">
<PipelineSequence name="NavigateWithReplanning">
<RateController hz="1.0"><ComputePathToPose goal="{goal}" path="{path}" planner_id="GridBased"/></RateController>
<FollowPath path="{path}" controller_id="FollowPath"/>
</PipelineSequence>
</BehaviorTree>
</root>Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Copy Nav2's navigate_to_pose tree, change the replanning rate from 1 Hz to 0.2 Hz, set default_nav_to_pose_bt_xml to your file, and observe how the robot reacts to a new obstacle.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.