What is SLAM in a Nutshell?

Mobile robots rely heavily on accurate representations of the environment (i.e maps) to fulfill their tasks (autonomous navigation, exploration, ...). Inside buildings, GPS signals are too weak to be used to localize robots. Hence we face a so-called Chicken-and-Egg-Problem, as localization requires a map, and map building (i.e. mapping) requires the current location. One solution consists in doing Simultaneous Localization and Mapping (a.k.a. SLAM) using a SLAM algorithm that typically reaches centimetric precision.

There are many different flavors of SLAM especially regarding the map format. The dominating 2D map format is the occupancy grid, also called grid map. A grid map is a matrix whose cells represents a defined region of the real world; this is the resolution of the grid map (typically a square of 5cm). A cell holds the estimated probability that the space it represents is traversable (free space) or not (obstacle). The simplest format is the 3-state occupancy grid in which a cell has 3 different possible values: 0 (free space), 0.5 (unknown) and 1 (obstacle).

Overview of a SLAM algorithm that produces a 3-state occupancy grid map and the robot pose (i.e. the robot position and its orientation)

Nav2 is the open source de facto standard for autonomous navigation in ROS2 that includes: perception, planning, control, localisation, ...

Map building using slam_toolbox

There are a lot of different SLAM algorithms and some implementations are open source and available on OpenSLAM.

We will use here the slam_toolbox ROS2 implementation in Nav2(documentation is here).

Launch slam_toolbox

ros2 launch tbot_sim challenge-1.launch.py
ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=True
rviz2 --ros-args -p use_sim_time:=True

Question: using all the tools you already know (rviz2, rqt_graph, tf, ...), what are the input and output data of slam_toolbox?

Manual Mapping

Launch a teleop:

# keyboard
ros2 run teleop_twist_keyboard teleop_twist_keyboard

# or xbox
ros2 launch teleop_twist_joy teleop-launch.py joy_config:=xbox

Now, while moving the robot around the simulated environment, you should see the result of slam_toolbox (both the map and robot pose) updated in rviz2.

tf tree

It is possible to save the map by using slam_toolbox service call:

ros2 service call /slam_toolbox/save_map slam_toolbox/srv/SaveMap "name: {data: '/home/bot/path/to/map'}"

Load a map and Localize in it

To load a map and localize in it, you should:

  1. execute:
ros2 launch nav2_bringup localization_launch.py map:=/home/bot/map.yaml autostart:=True
  1. publish an approximate initial pose into the topic /initialpose (using rviz2 for example)

  2. move the robot around (teleop or so) to improve the acuracy of the localization

In simulation, we do the same steps, but the time is simulated:

ros2 launch nav2_bringup localization_launch.py map:=/home/bot/map.yaml autostart:=True use_sim_time:=True

rviz2 --ros-args --remap use_sim_time:=True

Bag files (ros2 bag)

Working in simulation is nice but we can do better and work directly on real data using the ros2 bag command tool. The idea is to record some topics (all data that goes through) into a bag file and play them later on. Bag files are really useful to test algorithms on real data sets that have been recorded in a specific location and with specific sensors. Moreover, there are a lot of public datasets available:

  • [http://radish.sourceforge.net/]
  • [https://vision.in.tum.de/data/datasets/rgbd-dataset/download]
  • [http://www.ipb.uni-bonn.de/datasets/]
  • [http://car.imt-lille-douai.fr/polyslam/]

Use the tutorial on rosbag to learn how to use it.

Work: Launch the challenge 2 simulation, record /tf and /scan into a rosbag and teleop the robot all around the map. You now have a dataset ready to do offline SLAM.

Offline Mapping

Work: Play the rosbag created before and launch slam_toolbox in offline mode to create the map and then save it. You can now load this map to localize the robot into it.

Map a real arena

Re-do exactly the same things to map an existing arena:

  1. Put the robot inside the arena
  2. Record a bag file with all needed data to achieve SLAM
  3. Manually teleoperate the robot everywhere in the arena
  4. Save the bag file
  5. Play the bag file and launch the SLAM to create a map and save it (offline mapping)
  6. Put the robot inside the arena and load the map to localize the robot inside the arena

Hint: slam_toolbox comes with a RViz plugin (Panels->Add New Panel->slam_toolbox->SlamToolboxPlugin) that might help

Tuning map quality

SLAM algorithms usually have a lot of parameters that can be tuned to improve performance or map quality. slam_toolbox is no exception. Some hints here.