Mapping an environment in ROS 2#

Many robots operate in pre-mapped environments. In Nav2 the map of the environment is used both for localization and for generating a costmap for motion planning. This document demonstrates how to create a map of the environment using SLAM toolbox.

Table of Contents

Prerequisites top#

  • SVL Simulator release 2021.2.2 or later
  • Linux operating system (preferably Ubuntu 18.04 or later)

For a guide to setting up and using the simulator see the Getting Started guide. For other robotics guides see Running a basic Robotics Simulation with ROS 2 and Running a ROS2 Robotics Simulation with Nav2.

Installing ROS 2 top#

ROS2 can be installed by following the steps in the official installation guide.

SVL Robot Startup top#

Robots often have many 3D coordinate frames that change in position and orientation over time. tf2 is a transform library used to keep track of the relative transformation between these coordinate frames in ROS/ROS 2. The simulator does not directly broadcast tf transforms. Therefore, an external ROS2 node is needed for this. svl_robot_startup is a ROS2 package that publishes static transforms for the robot sensors and subscribes to the /odom topic published by the simulator to figure out and broadcast the transform between the odom and basefootprint frames.

The SVL Simulator's ROS 2 bridge lgsvl_bridge is included as a dependency and added to the launch file to enable communication with the simulator.

The navigation2 and nav2_bringup packages are also added as dependencies as they will be needed for running Nav2.

Follow these steps to setup the package and install dependencies:

source /opt/ros/foxy/setup.bash
mkdir -p robot_ws/src
cd robot_ws/src
git clone https://github.com/lgsvl/svl_robot_bringup.git
cd ..
rosdep update
rosdep install --from-path src -iy --rosdistro foxy
colcon build --symlink-install

Installing Nav2#

Nav2 be installed by rosdep in the previous step. If, for whatever reason, the installation was skipped and can be manually installed using the following commands.

sudo apt update
sudo apt install ros-foxy-navigation2 ros-foxy-nav2-bringup

Installing SLAM toolbox#

SLAM toolbox provides a set of open-source tools for 2D SLAM which will be used in this tutorial for mapping the environment. It can be built from source (follow instructions on GitHub) or installed using the following command:

sudo apt install ros-foxy-slam-toolbox

Setting up a Simulation top#

Click on the Simulations tab and click Add New to create a new simulation.

  • Enter a Simulation Name and select the Cluster. Click Next.
    • Optional - Enable Interactive Mode to be able to start the simulation at the desired time by pressing a play button in the simulator screen.
  • Select the Random Traffic Runtime Template. Select LGSeocho under Map (if it is not present you will need to go back to the store and add the map to your library). Select the LGCloi ego vehicle and the Nav2 Sensor Configuration. Click Next.
  • Select Other ROS2 Autopilot under Autopilot and enter the Bridge Connection address (by default localhost:9090).
  • Click Publish

Mapping the environment#

  • In a new terminal start the robot_tf_launch:
source /opt/ros/foxy/setup.bash
cd robot_ws
source install/setup.bash
ros2 launch svl_robot_bringup robot_tf_launch.py
  • Start the simulation in the SVL Simulator

  • Run the Simulator binary and click OPEN BROWSER

  • In the web user interface, open the Simulations page, select the recently built simulation and click Run Simulation

robot_at_spawn_point

  • Launch navigation_launch fron Nav2:
source /opt/ros/foxy/setup.bash
ros2 launch nav2_bringup navigation_launch.py
  • Launch slam toolbox:
souce /opt/ros/foxy/setup.bash
ros2 launch slam_toolbox online_async_launch.py
  • Launch rviz:
source /opt/ros/foxy/setup.bash
ros2 launch nav2_bringup rviz_launch.py
  • You can start mapping by either giving destinations using the Navigation2 Goal button in rviz or by manually driving the robot around using the keyboard.

mapping

  • Once satisfied with the map it can be saved using:
ros2 run nav2_map_server map_saver_cli -f ~/map

Note on saving maps: Map saving tends to fail due to timeout. Attempting to save multiple times can result in a successful map save. See this issue on GitHub for more information.