This repository is for learning how to create and use behavior trees in ROS 2 with the BT.CPP
library. It's meant to be used alongside the blog post I wrote about it: https://sentientbeing.bearblog.dev/why-and-how-to-use-behavior-trees-in-your-robot-application/
The whole setup is dockerized, so you don't have to worry about installing ROS 2 or any other dependencies on your machine. Just follow the steps below.
Once you're done, you'll have a simulated robot that uses a behavior tree to navigate around and find objects of a certain color.
These instructions are for a Linux computer (tested on Ubuntu 22.04) since we need to display a graphical user interface from the Docker container. Using VS Code with the Dev Containers extension is the easiest way to get this running.
-
Docker Engine: Follow the installation guide here. After you install it, it's very important to do the Linux post-install steps. This lets you use Docker without typing
sudo
every time. Remember to log out and log back in for the changes to take effect. -
NVIDIA GPU Drivers (if you have an NVIDIA card): If your computer has an NVIDIA graphics card, make sure you're using the official drivers and have the NVIDIA Container Toolkit installed. You can check if it's working by running:
sudo docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi
-
VS Code + Dev Containers Extension: You'll need VS Code and the official Dev Containers extension to open the project.
- Clone this repository to your machine.
- Open the
Behavior_trees
folder with VS Code. - VS Code should suggest reopening the folder in a container. If not, open the command palette (
Ctrl+Shift+P
) and runDev Containers: Rebuild and Reopen in Container
. - Wait for the container to build. This will take a few minutes the first time.
Now you're inside the development environment with everything you need.
All the commands below should be run in the terminal inside VS Code.
First, you need to install Groot2, which is a tool to see your behavior tree graphically as it runs.
The installer is already in your home directory inside the container.
cd ~
./Groot2Installer
An installer window should pop up. Just click 'Next' all the way through. For the installation directory, use the default path: /home/ros2-dev/Groot2
.
After it's installed, run this command to create a shortcut so our ROS nodes can find it:
ln -s /home/ros2-dev/Groot2/bin/groot2 ~/groot2
Next, start the Gazebo world and the navigation system.
ros2 launch simulation simulation_bringup.launch.py
A Gazebo window will appear showing a room and the robot.
Finally, start the behavior tree to get the robot moving.
ros2 launch behavior_tree tree.launch.py target_color:=green
You can change green
to another color if you want to see the robot search for something else. Groot2 should also pop up and show you the tree's state in real-time.
The code for this project was adopted from this repo: https://github.com/sea-bass/turtlebot3_behavior_demos. A great work done by Sebastian Castro. Head to https://roboticseabass.com/ to learn more about robotics related blogs.