SLAM Toolbox tutorial: map a robot in ROS 2

Set up SLAM Toolbox in ROS 2: required transforms, launch commands, the parameters that matter, saving maps and the failure modes to expect.

Reference
RE-873
Published
Read
4 min

About the author · Sources, testing and AI policy

RViz window showing a 2D occupancy map built by a mobile robot

SLAM Toolbox turns a lidar, a wheel odometry source and a ROS 2 launch file into a map you can navigate. It is the default mapping package for Nav2, and most “SLAM does not work” problems are missing transforms, not missing algorithms.

This tutorial covers the prerequisites, the launch commands, the parameters that matter, saving and reloading a map, and the failure modes you will actually see on a differential drive robot.

What SLAM Toolbox needs before it runs

  • A lidar publishing sensor_msgs/LaserScan on /scan.
  • Odometry published on /odom and a transform from odom to base_link.
  • A static transform from base_link to the lidar frame.
  • A URDF that defines the frames consistently.
  • Time synchronisation. Simulation time and wall time must not mix.

If ros2 run tf2_tools view_frames does not show a connected tree from map to the lidar frame, SLAM cannot work. Fix the transforms first, always.

Start mapping

sudo apt install ros-$ROS_DISTRO-slam-toolbox
ros2 launch slam_toolbox online_async_launch.py use_sim_time:=true

Use online_async on a normal laptop or Jetson. On a very low-power board, or when you want deterministic ordering, online_sync_launch.py processes scans in order instead of in a separate thread.

ros2 launch slam_toolbox online_sync_launch.py use_sim_time:=false

In RViz, add the /map display and set the fixed frame to map. Drive the robot slowly and completely around the space, closing loops by returning to where you started.

Drive it properly

HabitWhy it matters
Drive slowlyFast rotation blurs scans and breaks matching
Turn in place at cornersGives the matcher clean rotational constraints
Close the loopReturn to the start to trigger loop closure
Keep the lidar clearBags, coats and hands become permanent walls
Light the space consistentlySome lidars struggle with direct sunlight or glass
Do not teleport the robotJumping the base breaks the odometry chain

Parameters worth tuning

ParameterEffectPractical note
minimum_travel_distanceDistance before a new node is addedLower for small indoor spaces
minimum_travel_headingRotation before a new nodeLower if the robot turns a lot
resolutionMap cell size in metres0.05 is typical for indoor robots
max_laser_rangeIgnores returns beyond thisCut glass reflections and outdoor noise
transform_publish_periodHow often map to odom is published0.05 s is a reasonable start
map_update_intervalHow often the map is rebuiltRaise it on slow hardware
loop_search_maximum_distanceHow far to look for loop closuresRaise for large buildings

Change one parameter at a time and drive the same route. Tuning two at once makes it impossible to know what helped.

Save the map

The map saver writes a .pgm image and a .yaml metadata file, which is what Nav2’s map server reads later.

mkdir -p ~/maps
ros2 run nav2_map_server map_saver_cli -f ~/maps/workshop

Also save the pose graph, so you can resume mapping or run in localization mode with the full graph rather than just the image.

ros2 service call /slam_toolbox/serialize_map \
  slam_toolbox/srv/SerializePoseGraph \
  "{filename: '/home/user/maps/workshop'}"

Mapping mode vs localization mode

ModeWhen to useBehaviour
MappingFirst run in a new spaceBuilds and updates the map
LocalizationProduction, known buildingLoads a saved graph and only localises
LifelongChanging environmentsUpdates an existing map over time

For a delivery or cleaning robot in a fixed building, map once and run localization. Continuous remapping in production adds risk for no benefit.

Failure modes and fixes

SymptomCauseFix
Map drifts then snapsBad odometry calibrationFix wheel radius and track width, re-tune
Walls doubledPoor scan matching or fast drivingSlow down, lower travel thresholds
No map appearsMissing map to odom transformCheck TF tree and launch status
Map jumps on rotationLidar mounted off-centre without correct TFCorrect the static transform
Loop closure never triggersNever revisited the startClose the loop deliberately
CPU peggedAsync worker saturated or too many nodesRaise travel distance, lower scan rate

A mapping routine that produces usable maps

  • Run TF checks and confirm /scan rate is stable.
  • Start SLAM, then drive a slow perimeter loop.
  • Cover corridors twice in opposite directions.
  • Stop, save the pose graph, then save the image map.
  • Restart in localization mode and drive the route again to test.
  • Fix anything that fails before adding Nav2.

Related: SLAM for beginners, Nav2 tutorial, TF2 tutorial, lidar vs ultrasonic vs IR, Gazebo ROS 2 simulation.

What does SLAM Toolbox do?

It builds a 2D occupancy map from lidar scans and odometry while tracking the robot's pose, and can later localise the robot on a saved map. It is the standard mapping package used with Nav2.

What topics does SLAM Toolbox need?

A LaserScan on /scan, odometry on /odom, an odom to base_link transform, and a static transform from base_link to the lidar frame. Without a complete TF tree it will not run.

How do I save a map in ROS 2?

Run ros2 run nav2_map_server map_saver_cli -f ~/maps/name to write the .pgm and .yaml, then call the SerializePoseGraph service to save the graph for later localization.

Why is my SLAM map drifting?

Usually wheel odometry is miscalibrated, the robot is driving too fast, or the lidar transform is wrong. Fix odometry first, then slow down and lower the travel thresholds.

Should I run SLAM continuously on a production robot?

No. Map once, then run SLAM Toolbox in localization mode against the saved graph or use AMCL with the saved map. Continuous remapping adds risk without benefit in a fixed building.

When a mapping run goes wrong, a recorded bag lets you replay it without driving again.

Mapping improves when the odometry feeding SLAM is fused rather than raw.

A clean map helps, but costmap inflation decides how the robot drives it.

The prediction and correction steps are the same maths SLAM uses to fuse scans with odometry.

Set the environment up first; our WSL2 setup for ROS 2 gets RViz and the toolchain running before you tune SLAM.

Sources and further reading

Pass it onWhatsAppLinkedIn

Keep reading

Continue learning

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.