What is SLAM?
SLAM algorithms process sensor data (laser scans, odometry) to:- Localize: Determine the robot’s position and orientation
- Map: Build a 2D occupancy grid of the environment
- Update: Continuously refine both the map and position estimate
Starting SLAM
Using the alias
The quickest way to start SLAM with Cartographer:Using the full command
The
use_sim_time:=True parameter is critical for simulation. It tells ROS2 to use Gazebo’s simulated time instead of wall clock time.Complete SLAM workflow
Launch simulation world
Start a Gazebo world with obstacles to map:Wait for the simulation to fully load.
Start SLAM
In a second terminal, launch Cartographer:RViz2 will open automatically in the VNC desktop.
Drive to explore
Use keyboard controls to drive the robot:
- Press
ito move forward - Use
jandlto rotate - Drive slowly through all areas
- Ensure complete environment coverage
Monitor in RViz2
Access the VNC desktop at
http://localhost:6080 and watch:- The map being built in real-time (gray = unknown, white = free space, black = obstacles)
- The robot’s position and laser scan data
- The particle cloud showing position uncertainty
Mapping tips
Movement strategy
Move slowly: Rapid movements reduce map quality- Use brief taps of movement keys instead of holding
- Allow the SLAM algorithm time to process sensor data
- Pause occasionally at key locations
- Use
uandokeys for curved paths - Avoid rapid rotation with
jandl
- Get close to walls and obstacles for detail
- Cover open spaces thoroughly
- Don’t skip any areas
- This helps the SLAM algorithm reduce drift
- Improves overall map accuracy
- Essential for large environments
Choosing the right world
For practice: Start withtb3_world
- Medium complexity
- Clear obstacles
- Good for learning SLAM basics
tb3_house
- Multiple rooms
- Realistic indoor environment
- Tests advanced SLAM capabilities
tb3_empty for SLAM practice
- No features to map
- SLAM needs distinct landmarks
- Use only for testing SLAM startup
Understanding RViz2 visualization
When you launch SLAM, RViz2 displays several important elements:Map display
- White areas: Free space where the robot can travel
- Black areas: Obstacles and walls detected by sensors
- Gray areas: Unknown/unexplored regions
- Map updates: Watch gray areas turn white or black as you explore
Robot position
- Red arrow: Current robot pose (position and orientation)
- Green/blue particles: Position estimate uncertainty
- Laser scan: Red dots showing current sensor readings
Quality indicators
- Clear boundaries: Sharp transitions indicate good map quality
- Fuzzy edges: Blurry boundaries suggest you moved too fast
- Gaps in walls: Missing data means you need to explore that area more
Saving your map
Once you’ve finished exploring and the map looks complete, save it for future use.Create maps directory
Save the map
Generated files
The map saver creates two files: my_map.pgm- Portable graymap image file
- Visual representation of the map
- Can be viewed with image viewers
- Map metadata and configuration
- Contains resolution, origin, and thresholds
- Required by Navigation2
Both files are needed for navigation. Keep them together and don’t rename them separately.
Workflow examples
Mapping TurtleBot3 world
Terminal 1:Mapping house environment
Terminal 1:Troubleshooting
RViz2 doesn’t open
Map quality is poor
Move slower- Reduce linear velocity by pressing
xseveral times - Use brief key taps instead of holding keys down
- Avoid sharp rotations
- Use curved paths with
uandokeys - Don’t make sudden direction changes
- Drive closer to walls and obstacles
- Explore all areas thoroughly
- Return to starting position to close the loop
No map appears in RViz2
Map save fails
Advanced SLAM options
Different SLAM algorithms
While this environment uses Cartographer by default, TurtleBot3 also supports:- SLAM Toolbox
- Gmapping (ROS1, requires bridging)
Parameter tuning
For advanced users, Cartographer parameters can be adjusted in the launch file to optimize for:- Different sensor configurations
- Varying environment types
- Speed vs. accuracy trade-offs
Real-time map quality
Monitor these indicators in RViz2:- Particle spread (localization confidence)
- Loop closure events (drift correction)
- Edge clarity (measurement quality)