ROS 2 Learning Path  ยท  Visual Educational Book

Chapter 10: SLAM โ€” ARCHO Builds the Warehouse Map

When no map exists and the robot must build one itself
Prerequisite: Chapter 9 (Odometry)
Running project: ARCHO robot
Tool: SLAM Toolbox
Reading time: 100โ€“120 minutes
What we cover in this chapter 10.1A fundamental question: where does the map come from? 10.2What SLAM is: localization and mapping at once 10.3Two modes of SLAM Toolbox: Mapping and Localization 10.4Installation and configuration 10.5Building the warehouse map 10.6Saving the map 10.7Why maps go bad 10.8Summary, glossary, and exercises

10.1A fundamental question: where does the map come from?

In Chapter 9 we assumed a map already existed and we just needed to estimate ARCHO's position on it. But a real day one doesn't work that way: you unbox ARCHO and roll it into a new warehouse that nobody has ever mapped. Now what?

This simple question gave rise to one of the most important branches of all of robotics: SLAM (Simultaneous Localization and Mapping) โ€” discover the environment, build a map, and find your own position on that same map you're building, all at the same time.

๐Ÿง  Why this is a hard problem

To find your position, you need a map; but to build an accurate map, you need to know where you are. This appears to be an unsolvable two-way dependency โ€” a classic chicken-and-egg problem. SLAM breaks this loop by cleverly combining odometry (which we built in the previous chapter) with successive comparisons of LiDAR scans.

10.2What SLAM is: localization and mapping at once

In this book, we use the most widely-used LiDAR SLAM package in ROS 2: SLAM Toolbox.

๐Ÿ“– What SLAM Toolbox exactly does

It uses LiDAR data, simultaneously estimates the robot's position, builds a map, detects when it returns to a previously visited place (Loop Closure), keeps this entire history in a structure called a Pose Graph, and finally saves the map so it can be reloaded later.

flowchart TB A["LiDAR
/scan"] --> B["SLAM Toolbox"] C["Odometry
/odometry/filtered"] --> B B --> D["/map"] B --> E["Transform: map โ†’ odom"] B --> F["Pose Graph"] D --> G["Navigation2"] style B fill:#eef0ff,stroke:#3d4bf5,color:#211f1a,font-weight:bold style G fill:#eafaf3,stroke:#0e9e6e,color:#211f1a
๐ŸŒ SLAM and robot_localization are not competitors

A point that confuses many people: SLAM Toolbox does not replace the work from Chapter 9 โ€” it uses its output. Filtered odometry estimates moment-to-moment motion; SLAM Toolbox uses this same estimate to improve the accuracy of comparing successive scans (Scan Matching). The two are complementary, not competing.

10.3Two modes of SLAM Toolbox: Mapping and Localization

ModeWhen it's usedInputOutput
MappingDay one, the environment is completely unknownLiDAR + OdometryNew map (Occupancy Map)
LocalizationMap already built and savedSaved map + LiDARJust the robot's position on the map

The workflow for ARCHO's first two days in a new warehouse branch usually looks like this:

flowchart LR subgraph D1["Day One"] A1["LiDAR"] --> B1["SLAM Toolbox
Mapping"] --> C1["Save Map"] end subgraph D2["Day Two Onward"] A2["Load Map"] --> B2["AMCL
Localization"] --> C2["Navigation2"] end style B1 fill:#eef0ff,stroke:#3d4bf5 style B2 fill:#eafaf3,stroke:#0e9e6e
โš ๏ธ The biggest beginner mistake

Many people think SLAM should always stay on. In most industrial projects, the map is built only once, saved, and from then on only Localization (with AMCL, next chapter) is performed โ€” it's both lighter and more stable. The exception is environments that constantly change, like a warehouse under renovation; there, SLAM might stay active permanently.

10.4Installation and configuration

sudo apt install ros-jazzy-slam-toolbox
Main inputsOutputs
/scan/map
Transform: odom โ†’ base_linkTransform: map โ†’ odom
Transform: base_link โ†’ laser_linkPose Graph

Configuration file for ARCHO:

# archo_slam/config/mapper_params.yaml
slam_toolbox:
  ros__parameters:
    use_sim_time: true
    mode: mapping
    map_frame: map
    odom_frame: odom
    base_frame: base_link
    scan_topic: /scan
    resolution: 0.05
    max_laser_range: 12.0
    minimum_time_interval: 0.2
    transform_publish_period: 0.05
    map_update_interval: 2.0
    minimum_travel_distance: 0.15
    minimum_travel_heading: 0.15
    use_scan_matching: true
    do_loop_closing: true
ParameterMeaning
resolutionSize of each map cell in meters โ€” 0.05 means each map pixel is 5 centimeters
minimum_travel_distanceThe robot must travel at least this far before a new scan is recorded into the map
do_loop_closingWhether the map corrects itself when you return to a previous location

10.5Building the warehouse map

To build the map, you need four separate terminals:

TerminalTask
1Gazebo + ARCHO (Chapters 7 and 8)
2SLAM Toolbox
3Teleoperation (manual drive command)
4RViz โ€” with Fixed Frame set to map and Displays for Map, LaserScan, TF, RobotModel, Odometry
๐Ÿ”ง How to build a good map
  • Move slowly โ€” fast turns break Scan Matching.
  • Cover every aisle in the warehouse, not just the direct path.
  • Return to the starting point so Loop Closure can occur and correct accumulated error.
  • Avoid getting too close to shelving or glass surfaces (which give LiDAR poor reflections).

10.6Saving the map

Once you're confident you've covered the whole warehouse:

ros2 run nav2_map_server map_saver_cli \
  -f ~/archo_ws/src/archo_navigation/maps/warehouse

This command creates two files:

FileContent
warehouse.pgmThe map image itself โ€” a grayscale image showing walls and open space
warehouse.yamlMap metadata: cell size, origin, and the PGM file's path
# warehouse.yaml
image: warehouse.pgm
mode: trinary
resolution: 0.05
origin: [-5.0, -5.0, 0.0]
negate: 0
occupied_thresh: 0.65
free_thresh: 0.25

The next day, these same two files are loaded by the Map Server and fed to AMCL (next chapter) to find ARCHO's position without needing to rebuild the map.

10.7Why maps go bad

SymptomLikely causes
Walls appear doubled or blurredPoor odometry, excessive speed, wheel slip, incorrect TF, low scan rate
The map rotates or is wavyIncorrect IMU yaw axis, invalid timestamps, poor scan matching
Extra stray points remain on the mapPeople or moving objects during scanning, an overly dynamic environment
๐Ÿ“– This chapter's golden rule

A good map isn't just the product of a good algorithm; it's the result of proper odometry (Chapter 9), correct TF (Chapter 5), gentle motion, and adequate coverage of the environment. If you didn't work carefully through the earlier chapters, no amount of SLAM Toolbox tuning can make up for that shortfall.

Harder exercise

Suppose that after building the map, you notice the walls of one aisle appear doubled and slightly offset from each other. Using the table in this section, rank three likely causes and, for each, propose an experiment that would identify the actual cause.

10.8Chapter 10 Summary

ARCHO can now enter a completely unknown warehouse and, without any pre-built map, simultaneously discover the environment, build a map, and find its own position on that same map as it's being built. This saved map is the launching pad that the next chapter โ€” Navigation2 โ€” builds upon.

โœ… Learning checkpoint
  • I can explain why SLAM is a "chicken-and-egg" problem.
  • I know the difference between Mapping mode and Localization mode in SLAM Toolbox.
  • I can explain why SLAM Toolbox and robot_localization are complementary, not competing.
  • I know which two files make up a saved map and what role each one plays.
  • I can name at least three common causes of map degradation.
๐ŸŒ Connection to the main project

ARCHO Project now has an archo_slam package with a complete Mapping configuration, and has built and saved its first real warehouse map (warehouse.pgm and warehouse.yaml).

What the next chapter adds

In Chapter 11, we'll load this saved map and step into the full Navigation2 architecture: Map Server, AMCL, Costmap, Global Planner, and Local Controller โ€” where ARCHO plans its own path through the warehouse fully autonomously for the first time.

Chapter 10 Glossary

SLAM
Simultaneous Localization and Mapping; building a map and estimating position simultaneously in an unknown environment.
SLAM Toolbox
The most widely-used LiDAR SLAM package in ROS 2; it produces a map, pose, and TF.
Loop Closure
Detecting that the robot has returned to a previously seen location, and correcting the map's accumulated error accordingly.
Pose Graph
A data structure that keeps the history of poses and the geometric relationships between successive scans.
Scan Matching
Comparing two successive LiDAR scans to estimate the robot's actual displacement between them.
Occupancy Map
A map in which each cell indicates whether a point in the environment is occupied, free, or unknown.
map_saver_cli
A command-line tool for saving the current map to a PGM file and a YAML file.

Common Chapter 10 mistakes โ€” summary