# Module 5.5: Simultaneous Localization and Mapping (SLAM)

- Course: [Course 5: Sensors, State Estimation & Localization (SLAM)](https://roboticsconcepts.dev/courses/sensors-state-estimation-slam.md)
- HTML Page: https://roboticsconcepts.dev/courses/sensors-state-estimation-slam/simultaneous-localization-and-mapping/
- Key Concepts: Joint pose & landmark estimation, Loop closure detection, Pose graph optimization

Every filter we've looked at so far cheated a tiny bit: it assumed someone already handed the robot a clean map of beacons or corridor walls. In the real world — exploring a collapsed tunnel, a new apartment, or an orchard — you don't get a blueprint ahead of time. You have to build the map while figuring out where you are inside it.

## The chicken-and-egg problem of SLAM

Simultaneous Localization and Mapping (SLAM) looks impossible at first glance: to localize accurately, you need a reliable map; to place walls and landmarks accurately onto a map, you need to know where the robot was standing when it saw them.

If your odometry slowly drifts 3° while driving down a long loop, every wall you paint onto the map gets slightly rotated. By the time you loop back to where you started, your map shows two slightly offset copies of the same front door instead of one clean room.

## Loop closure and pose graph relaxation

Modern SLAM breaks that dead-end with two big ideas. First, instead of throwing away old poses, the robot builds a pose graph: nodes are past robot positions along its drive, and edges are odometry links ('step 4 to step 5 felt like 0.4 m forward') plus landmark sightings.

Second, when the robot loops back and its camera or LiDAR recognizes a corner it visited minutes ago — a loop closure — it adds a new, high-confidence constraint tying the end of the trajectory directly back to the start. Now the graph is over-constrained, and a fast optimizer gently relaxes all the nodes along the loop, spreading the tiny accumulated drift evenly across the whole path until the two doorways snap cleanly into one.

- Front-end SLAM extracts features, matches consecutive scans, and proposes candidate loop closures.
- Back-end SLAM solves the nonlinear least-squares pose graph to align every past pose and landmark consistently.
- Bundle adjustment and factor graphs generalize this exact idea to 3D visual SLAM and LiDAR-inertial mapping.

## Hands-On Lab

There is no separate web widget for this module — instead, open the companion notebook (`05_slam_pose_graph_loop_closure.py`) to step through a drifting robot loop, trigger a loop closure constraint, and watch least-squares pose graph relaxation snap the broken map shut.

## From maps to persistent spatial memory

Seeing a warped, double-walled map snap into crisp alignment after one loop closure is one of the most satisfying moments in robotics. Suddenly, the robot isn't just surviving moment to moment — it has a consistent geometric memory of the building.

Across Course 5, we've gone all the way from raw noisy wheel clicks, to tracking uncertainty with Bayes rule and Kalman ellipses, to handling ambiguous hallways with particles, and finally to building maps from scratch with SLAM. Next, in Course 6, we look at how modern robot learning and embodied AI connect perception directly to generalizable control.

## Pose graph optimization objective

```text
x* = argmin_x Σ || e_odom(x_i, x_{i+1}) ||²_Ω + Σ || e_loop(x_i, x_j) ||²_Ω_loop
```

SLAM finds the full chain of poses x that minimizes disagreement across both sequential odometry edges and long-range loop-closure links.

## Check Your Intuition

**Question:** A mapping robot drives all the way around a large square courtyard and draws a map whose start and end hallways fail to meet by 1.2 meters. What happens the instant SLAM detects a valid loop closure?

- [ ] Only the final pose jumps by 1.2 meters, leaving a sharp tear in the middle of the map
- [✓] Pose graph optimization distributes the 1.2-meter correction smoothly across the entire loop of poses and mapped walls
- [ ] The robot deletes the entire map and starts over from scratch

**Explanation:** Adding the loop-closure edge lets the optimizer relax all interconnected poses along the trajectory, smoothly aligning both the path and mapped geometry.

## Where You'll See This in Modern Robotics

Open-source back-ends like GTSAM, g2o, Cartographer, and ORB-SLAM3 power everything from autonomous drones and home vacuums to AR headsets — turning noisy sensor streams into metric 3D maps in real time.

## Companion Python Notebook: 05 · Pose Graph SLAM & Loop Closure Relaxation

Build a 2D pose graph from drifting odometry constraints, add a loop-closure edge, and relax the entire trajectory via Gauss-Newton.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-5/05_slam_pose_graph_loop_closure.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-5/05_slam_pose_graph_loop_closure.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-5/05_slam_pose_graph_loop_closure.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-5/05_slam_pose_graph_loop_closure.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-5/05_slam_pose_graph_loop_closure.py`
