CodeTheRobot

Ubuntu Linux for Robotics · Lesson 81 of 96

Behavior Trees

Where the BT XML lives, choosing a tree and viewing its state.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain behavior trees in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Nav2 uses behavior trees (XML files) to decide what happens on each goal: plan, follow the path, and recover from failures. The default trees are in the nav2_bt_navigator package under behavior_trees/. You choose a tree with the bt_navigator parameter default_nav_to_pose_bt_xml.

Copy a default tree into your own package, edit it (for example change the replanning rate) and point the parameter at your file. Run ros2 pkg prefix nav2_bt_navigator to find the originals. Tools such as Groot can display the tree while it runs.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the behavior trees commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

ls $(ros2 pkg prefix nav2_bt_navigator)/share/nav2_bt_navigator/behavior_trees
# List the default behavior trees.
cp $(ros2 pkg prefix nav2_bt_navigator)/share/nav2_bt_navigator/behavior_trees/navigate_to_pose_w_replanning_and_recovery.xml ~/my_tree.xml
# Copy a tree to edit.
ros2 param get /bt_navigator default_nav_to_pose_bt_xml
# See which tree is used (empty means the default).
ros2 param set /bt_navigator default_nav_to_pose_bt_xml /home/user/my_tree.xml
# Switch to your own tree (or set it in the YAML file).

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

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.

goalMOBILE ROBOT

Demo for Behavior Trees. Expected result: The list of default behavior tree files

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: list the files in the behavior_trees folder of nav2_bt_navigator (use ros2 pkg prefix)

Expected output

The list of default behavior tree files
Stuck? Show the full solution (try it yourself first)
ls $(ros2 pkg prefix nav2_bt_navigator)/share/nav2_bt_navigator/behavior_trees

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Copy the default tree, change the replanning rate from 1 Hz to 0.2 Hz, load it, and observe how the robot reacts when you put a new obstacle in its path.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.