ROS 2 Lyrical Luth · Lesson 59 of 86
SLAM Toolbox: mapping
Online async mapping, parameters, driving to map and saving the result.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain slam toolbox: mapping in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
SLAM (simultaneous localization and mapping) builds a map while working out where the robot is in it. SLAM Toolbox is Nav2's standard 2D SLAM: it takes /scan and odometry, publishes /map and the map → odom transform, and can save and reload maps (also serialized pose graphs for continuing later).
Run online_async_launch.py for live mapping, drive slowly with teleop (fast turns and featureless corridors break scan matching), close loops by returning to a known place, then save with the slam_toolbox RViz2 plugin or map_saver_cli. Key parameters: resolution, max_laser_range, minimum_travel_distance and loop closure settings.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the slam toolbox: mapping example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
ros2 launch slam_toolbox online_async_launch.py use_sim_time:=true
# Start live mapping using simulated time.
ros2 run teleop_twist_keyboard teleop_twist_keyboard
# Drive the robot around slowly to reveal the room.
ros2 run rviz2 rviz2
# Watch the map grow on /map.
ros2 run nav2_map_server map_saver_cli -f ~/maps/office
# Save the finished map to office.yaml and office.pgm.
ros2 service call /slam_toolbox/serialize_map slam_toolbox/srv/SerializePoseGraph "{filename: '/home/user/maps/office_graph'}"
# Save the pose graph so mapping can continue later.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
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.
Demo for SLAM Toolbox: mapping. Expected result: office.yaml and office.pgm are written
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
# TODO: save the current /map to ~/maps/office using nav2_map_server's map_saver_cliExpected output
office.yaml and office.pgm are writtenStuck? Show the full solution (try it yourself first)
ros2 run nav2_map_server map_saver_cli -f ~/maps/officeExpected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Map a simulated office with SLAM Toolbox, deliberately drive fast and turn sharply to see the map distort, repeat slowly, close a loop, and save both the map and the pose graph.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.