Overview
SLAM (Simultaneous Localization and Mapping) builds a map while simultaneously tracking robot position within that map. SLAM Toolbox is the recommended SLAM solution for ROS2.Why SLAM? Corrects odometry drift using map landmarks. Essential for long-term autonomous operation.
SLAM Toolbox Features
Mapping Modes
- Synchronous: Real-time mapping
- Asynchronous: Offline processing
- Localization: Localize in existing map
Loop Closure
Detects when robot returns to known location, corrects accumulated drift.
Lifelong Mapping
Update map over multiple sessions, handle dynamic environments.
Map Serialization
Save/load maps in multiple formats (PGM, serialized pose graph).
Installation
Configuration
Configuration File
File:config/slam_toolbox.yaml
Launch File
File:launch/slam.launch.py
Complete System Launch
File:launch/robot_slam.launch.py
Mapping Procedure
1
Launch System
2
Launch RViz
- Fixed Frame:
map - Add displays:
- Map: Topic
/map - LaserScan: Topic
/scan - RobotModel: Shows robot
- TF: Shows coordinate frames
- Odometry: Topics
/odometry/filtered(green),/mecanum_drive_controller/odom(red)
- Map: Topic
3
Drive Robot
Terminal:Mapping tips:
- Slow speed: 0.3 m/s max (more accurate scans)
- Overlap scans: Revisit areas for loop closure
- Cover all areas: Drive through entire environment
- Avoid open spaces: LiDAR needs features (walls, objects)
4
Monitor Map Quality
In RViz, watch for:
- Map building in real-time (gray = unknown, white = free, black = obstacle)
- Loop closure events (map suddenly adjusts when revisiting area)
- No major drift (robot model stays aligned with map)
- Walls are straight, continuous lines
- Rooms are closed shapes
- Features align when revisited
5
Save Map
Once satisfied with map:Method 1: RViz PanelAlso save occupancy grid (for Nav2):Creates:
- Panels → SlamToolboxPlugin
- Click “Serialize Map”
- Save to:
~/maps/my_map.posegraph
my_map.pgm(image)my_map.yaml(metadata)
RViz Configuration
Save RViz config for mapping:- Configure all displays as above
- Add SlamToolboxPlugin panel:
- Panels → Add New Panel → SlamToolboxPlugin
- Save config: File → Save Config As →
rviz/slam.rviz
Understanding SLAM Toolbox
TF Frames
Before SLAM:map → odom: Corrects long-term drift (published by SLAM Toolbox)odom → base_link: Local smooth motion (published by EKF)
Loop Closure
What is loop closure?- Robot returns to previously mapped area
- SLAM recognizes location (scan matching)
- Corrects accumulated drift by adjusting
map → odomtransform
- Robot drives in circle (odometry drifts 30cm)
- Returns to start
- SLAM recognizes starting area (loop closure!)
- Adjusts map to close the loop (drift corrected)
Occupancy Grid
Map representation:- Grid: 2D array of cells
- Cell value:
- 0: Free space (white)
- 100: Occupied (black)
- -1: Unknown (gray)
Tuning SLAM Toolbox
Scan Matching Sensitivity
- More scans added → denser map
- Slower processing, larger map file
- Fewer scans → faster, smaller map
- Might miss small features
Loop Closure Threshold
- Start with 0.45 (default)
- If false loop closures (map distorts) → Increase to 0.5-0.55
- If no loop closures (drift accumulates) → Decrease to 0.4
Max Laser Range
- RPLIDAR A1M8 max: 12m
- If noisy at max range → Reduce to 10m
- Shorter range = more reliable scans
Mapping Strategies
Grid Exploration
Pattern:
- Start at one corner
- Drive straight rows (like mowing lawn)
- Turn at ends, overlap previous rows
- Revisit starting area for loop closure
Perimeter First
- Drive around perimeter (walls)
- Then fill interior
- Creates outer boundary first (good reference)
Spiral Out
- Start at center
- Spiral outward
- Natural loop closure as spiral completes
Troubleshooting
No map appearing in RViz
No map appearing in RViz
Debug:
-
Check
/maptopic published: -
Check SLAM Toolbox node running:
-
Check LiDAR data:
-
Check Fixed Frame in RViz: Must be
map
Map drifts / distorts
Map drifts / distorts
Causes:
- Poor odometry (calibrate wheels)
- Too fast motion (scans blur)
- False loop closures
- Improve odometry calibration
- Reduce robot speed during mapping
- Increase
loop_match_minimum_response_fine
No loop closure detected
No loop closure detected
Symptoms: Drift accumulates, map doesn’t correctCauses:
- Threshold too high
- Environment too similar (featureless)
- Not enough overlap when revisiting
- Decrease
loop_match_minimum_response_fineto 0.4 - Drive slower for better scans
- Revisit areas directly (exact path overlap)
SLAM node crashes
SLAM node crashes
Causes:
- Insufficient memory
- Large map size
- Corrupted scan data
- Increase
stack_size_to_use - Use async mode instead of sync
- Check LiDAR for bad scans (range errors)
Map quality poor (fuzzy walls)
Map quality poor (fuzzy walls)
Causes:
- Robot moving too fast
- Vibration during scanning
- LiDAR mounting unstable
- Reduce mapping speed (<0.3 m/s)
- Ensure LiDAR rigidly mounted
- Check for wheel wobble
Localization in Existing Map
Once map created, switch to localization mode:Localization Config
File:config/slam_toolbox_localization.yaml
Localization Launch
- Map is loaded (not built)
- Robot localizes itself within map
map → odomupdated for continuous correction
Next Steps
Map Server
Save and load maps for navigation
Cartographer
Alternative SLAM solution (Google Cartographer)
Navigation
Use map for autonomous path planning
LiDAR Setup
Configure RPLIDAR for optimal performance