In this tutorial, we will explore Navigation2 (Nav2), which is a collection of tools for ROS 2 that enable a robot to go from point A to point B safely. We will also take a look at a SLAM demo with a robot named Turtlebot 3. Here will be our final output:
Real-World Applications
Navigation is one of the most important tasks for a mobile robot. Navigation is about enabling a mobile robot to move from one location to another without running into any obstacles.
In order to navigate properly, a robot needs to have a map (mapping), know where it is located (localization), and have a plan for getting from point A to point B (path planning).
Prerequisites
- ROS 2 Foxy Fitzroy installed on Ubuntu Linux 20.04 (if you are using another distribution, you will need to replace ‘foxy’ with the name of your distribution).
- You have already created a ROS 2 workspace.
Install and Build Nav2
***Note: The official instructions for installing Nav2 are here. Please check out that link to get the latest instructions. The steps below are valid as of the date of this blog post and will likely be different by the time you read this.***
Once you’re done with this tutorial, you can head over to my Ultimate Guide to the ROS 2 Navigation Stack.
To install Nav2, open a new terminal window, and type the following commands:
sudo apt install ros-foxy-navigation2
Type Y and then Enter to continue.
sudo apt install ros-foxy-nav2-bringup
Type Y and then Enter to continue.
Install the Turtlebot 3 example.
Open a new terminal window, and type:
sudo apt install ros-foxy-turtlebot3*
sudo apt install ros-foxy-nav2-simple-commander
If you want to build from the source (i.e. get the ROS2 navigation packages directly from GitHub), open a new terminal window, and type the following commands. One right after the other.
mkdir -p ~/nav2_ws/src
cd ~/nav2_ws/src
git clone https://github.com/ros-planning/navigation2.git --branch foxy-devel
cd ~/nav2_ws
rosdep install -y -r -q --from-paths src --ignore-src --rosdistro foxy
Your computer might say something like “executing command [sudo ……”. That is fine. Just wait, and let your system finish doing what it is doing.
colcon build --symlink-install
It took a while to install Nav2 on my machine. Just be patient.
You noticed that if we install Nav2 from the source, we also needed to install it using the Ubuntu package manager first. The reason we had to do both is that the Ubuntu package manager installs some non-ROS dependencies that are necessary for Nav2 to build from the source.
Building Nav2 from the source (using the Github clone command we did above) enables us to customize the packages in Nav2 to our liking (e.g. add new plugins, messages, etc.) that won’t get overwritten during a system upgrade (i.e. sudo apt-get upgrade)
When Nav2 is finished installing, open your bash file.
gedit ~/.bashrc
Add these lines to the bottom of the file. You can get the latest information on what to add on this link.
source ~/nav2_ws/install/setup.bash
export TURTLEBOT3_MODEL=waffle
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/opt/ros/foxy/share/turtlebot3_gazebo/models
source /usr/share/gazebo/setup.sh
Save the file, and close it.
cd ~/nav2_ws
Build it again, just to make sure everything is in order
colcon build
Test Your Installation
Now test your installation.
Open a new terminal window.
cd ~/nav2_ws
ros2 launch nav2_bringup tb3_simulation_launch.py
rviz2 will open.
Gazebo will also open, but it may take a while.
Move the Robot From Point A to Point B
Now go to the rviz2 screen.
Set the initial pose of the robot by clicking the “2D Pose Estimate” on top of the rviz2 screen. Then click on the map in the estimated position where the robot is in Gazebo.
Set a goal for the robot to move to. Click “Navigation2 Goal” button and choose a destination. The wheeled robot will move to the goal destination.
In the bottom left of the screen, you can Pause and Reset.
Press CTRL + C on all terminal windows to close down the programs.
Install the SLAM Toolbox
Now that we know how to navigate the robot from point A to point B with a prebuilt map, let’s see how we can navigate the robot while mapping. This process is known as Simultaneous localization and mapping (SLAM).
Open a new terminal window. Type this command:
sudo apt install ros-foxy-slam-toolbox
Launch the SLAM launch file. Open a new terminal window, and type:
cd ~/nav2_ws
ros2 launch nav2_bringup slam_launch.py
Now launch the robot.
ros2 launch nav2_bringup tb3_simulation_launch.py
Click the 2D Pose Estimate button and click on the rviz screen an estimate position where the robot is in Gazebo.
Then click the Navigation2 Goal button and click on an area of rviz where you want the robot to go.
Press CTRL+C in all terminals to shut everything down.
Here is another command you can run. This command launches Turtlebot3 and the SLAM package in a single command.
ros2 launch nav2_bringup tb3_simulation_launch.py slam:=True
That’s it. Keep building!