SLAM: Simultaneous Localization and Mapping

SLAM is one of the most elegant problems in robotics: a robot enters an unknown environment and must figure out where it is while simultaneously building a map of the environment — and it needs to do both at the same time, because each task requires the other. It sounds circular, and in a way it is. Understanding how SLAM breaks that circle is key to building autonomous mobile robots.

The chicken-and-egg problem

To build an accurate map, you need to know exactly where you were standing when you took each sensor reading. But to know where you are, you need a map to localize yourself against. SLAM solves both simultaneously, using probabilistic reasoning to handle the uncertainty in both the robot's position and the map's features.

Why can't we just use GPS?

GPS works outdoors with open sky — accuracy of 1–5 meters. Inside a building, GPS signal is blocked. Even outdoors, 1 meter accuracy is insufficient for a robot navigating around furniture or a car driving between lanes. SLAM provides centimeter-level localization in any environment, without external infrastructure.

Odometry and drift

Odometry tracks position by integrating wheel encoder counts: "I moved forward 1m, turned 90° left, moved 0.5m." This works in the short term, but small errors accumulate — the robot thinks it's at [5.3, 2.1], but it's actually at [5.8, 1.7]. SLAM corrects this drift by recognizing previously-seen landmarks and adjusting the entire trajectory to be consistent.

How SLAM works — the core concepts

Feature extraction and landmarks

The robot extracts distinctive features from sensor data — corners from LiDAR scans, visual keypoints from cameras. These become "landmarks" on the map. Each time the robot sees a landmark, it gets information about its own position relative to that landmark. Accumulate enough of these observations and you can triangulate position accurately.

Loop closure — the magic moment

When the robot recognizes a place it's been before (a "loop closure"), it has a strong constraint: it knows its current position relative to that earlier position. This lets it correct accumulated drift across the entire trajectory at once. Loop closure detection is what separates good SLAM systems from mediocre ones — it's the key to long-term accuracy.

The factor graph

Modern SLAM represents the problem as a factor graph: nodes are robot poses (at different times) and landmark positions; edges are sensor observations that constrain relationships between nodes. SLAM becomes a graph optimization problem — find the pose/landmark values that best satisfy all the constraints. Libraries like GTSAM and g2o solve this efficiently.

Types of SLAM

LiDAR SLAM

Uses a 2D or 3D LiDAR to build maps. Most reliable and accurate — LiDAR gives direct, precise distance measurements. Popular implementations: Cartographer (Google, 2D/3D, ROS), LOAM and its variants (3D LiDAR, autonomous vehicles), SLAM Toolbox (ROS 2, beginner-friendly). If your robot has LiDAR, start with SLAM Toolbox.

Visual SLAM

Uses cameras instead of LiDAR. Cheaper hardware, but more computationally demanding and more sensitive to lighting. ORB-SLAM3 is the gold standard for monocular, stereo, and RGB-D cameras. RTAB-Map (Real-Time Appearance-Based Mapping) supports LiDAR, RGB-D, and stereo, and has excellent ROS 2 support with loop closure and 3D map building.

Visual-Inertial SLAM

Combines camera data with an IMU. The IMU provides high-frequency orientation and motion data between slower camera frames — dramatically improving robustness when the robot moves quickly or the scene lacks visual features. Used in drones, AR headsets, and mobile robots that need reliable localization in dynamic environments.

SLAM in ROS 2

SLAM Toolbox (recommended for beginners)

The official SLAM package for ROS 2. Works with 2D LiDAR. Install: sudo apt install ros-humble-slam-toolbox. Launch: ros2 launch slam_toolbox online_async_launch.py. Subscribe to your LiDAR topic, drive your robot around, and watch the map build in RViz2 in real time. Full navigation integration with Nav2.

Nav2 integration

Once you have a SLAM map, ROS 2 Navigation (Nav2) can use it for autonomous navigation. SLAM provides the map and robot pose; Nav2 provides path planning, obstacle avoidance, and motor commands. The two work together seamlessly — SLAM feeds Nav2, Nav2 drives the robot, which generates new sensor data for SLAM to update the map.

Frequently Asked Questions

What sensor do I need for SLAM?

The minimum is a 2D LiDAR (like RPLidar A1, ~$100) plus wheel encoders. This gives you 2D SLAM for flat-floor environments. For 3D SLAM (stairs, ramps, outdoor), you need a 3D LiDAR or depth camera. For visual-only SLAM, a stereo camera or RGB-D camera works, but requires more CPU/GPU.

What's the difference between mapping and localization?

Mapping: Building a map from scratch (unknown environment). Localization: Figuring out where you are on an already-known map. In production deployments, robots often run SLAM once to build the map, then switch to localization-only (AMCL in ROS) for day-to-day operation — it's more efficient and more stable.

What is AMCL?

Adaptive Monte Carlo Localization — the standard algorithm for localizing a robot on a known map. It maintains a "particle cloud" of possible robot positions. Each particle is weighted by how well the current sensor reading matches the map at that position. Over time, the cloud collapses to the true position. Used in ROS 2 Nav2 for robot localization in production environments.

How does a robot know it's seen a place before (loop closure)?

For LiDAR SLAM, scan matching algorithms compare the current laser scan to stored scans and look for high correlation. For visual SLAM, appearance-based methods (like Bag of Words) encode the visual appearance of each keyframe as a compact descriptor and compare against all previous keyframes. A high similarity score triggers a loop closure candidate, which is then geometrically verified.

Frequently Asked Questions

What will I learn here?

This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.

How should I use this page?

Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.

What should I read next?

Use the navigation below to continue to the next lesson or explore related topics.