Lesson 20 of 221 hour

Maps, Occupancy Grids, and SLAM

Start with the lesson question, connect the representations, and test the model with evidence.

occupancy gridslampose graphloop closuremapping

Learning objectives

  • Explain odometry, drift, landmarks, and state estimation.
  • Interpret an occupancy map and the purpose of SLAM.
  • Diagnose failures across localization, planning, and control.
Lesson flowHook, model, explanationShow guidance

Inspect the opening phenomenon

Predict what changes, then name the evidence.

Apply in the lab

Name the evidence before reading the answer.

Read only what helps

Then use the lab and recall check.

More when needed

Transcript and resources stay available below.

Course progress

AI & Robotics Foundations · Localization, Mapping, and Navigation · Lesson 20

Maps, Occupancy Grids, and SLAM

In progress

Decision challenge

Observe the phenomenon. Then connect the representations.

Use the opening example to make a prediction, identify evidence, and explain which model supports it.

How Does SLAM Loop Closure Fix a Broken Robot Map?

SLAM loop closure corrects a broken map only after geometric verification adds a defensible pose-graph constraint.

SLAM loop closure corrects a broken map only after geometric verification adds a defensible pose-graph constraint.

Reference drawerTranscript, source notes, scripts, and package status stay tucked away until you need them.6 files

Lesson reading

live

1 hr

Video script

draft

Transcript fallback

available

courses/ai-robotics/modules/07-localization-mapping-and-navigation/lessons/02-maps-occupancy-grids-and-slam/video-transcript.md

Make Drift Visible, Then Close the Loop

published

30 min

Mastery check

live

5 questions / 10 min

Transcript for accessibility and fallback

# Video transcript The robot returned home, but its map says the start moved. Follow one scan to learn why. Lidar marks crossed cells free and its endpoint occupied. Unobserved cells remain unknown. A scan has no map position alone. The estimated pose transforms it into map cells. Wheel slip creates small pose errors. As the robot travels, odometry drift accumulates. The scan can be correct, yet a wrong pose places walls in the wrong cells. Recognizing a visited place proposes a loop closure: two poses may be one location. Geometry must agree. Verification rejects mirrored or inconsistent rooms before they corrupt the map. A verified loop constrains the pose graph. Optimization redistributes error across the earlier trajectory. Now the path closes and the map agrees. Why can a correct scan still map a wall incorrectly? ## Non-video equivalent An occupancy grid stores sensor evidence as unknown, free, or occupied cells. A measurement does not identify its own map location; SLAM places it using the robot's estimated pose. Odometry error can therefore insert correct measurements into incorrect cells. A geometrically verified revisit establishes a loop-closure constraint. Pose-graph optimization distributes the accumulated error across earlier poses, producing a more consistent trajectory and map. ### Annotated-keyframe reading order 1. **Occupancy evidence:** follow the lidar ray from the robot through free cells to its occupied endpoint; cells outside the observation remain unknown. 2. **Pose drift:** compare the fixed green physical wall with the amber wall placement. Their separation represents pose error, not a different range measurement. 3. **Loop correction:** compare the open amber trajectory with the closed green trajectory. The correction affects connected earlier poses, so their associated scan placements change too.

Reading lab

Core explanation

Connect the lesson's words, diagrams, graphs, evidence, and equations.

Is a robot map a picture of the world—or a record of evidence?

It is a structured estimate. A two-dimensional occupancy grid divides space into cells and records what the mapping system currently believes about each cell. Unknown means the robot lacks sufficient evidence; it is not the same as free space. Free means sensor evidence supports traversal under the sensor model. Occupied means evidence supports an obstacle at that resolution.

A grid needs geometry

The map metadata defines resolution, width, height, and origin. For zero-based integer cell coordinates (x, y) in a width-W row-major grid:

index = x + yW

For a world point (X, Y), axis-aligned grid origin (X₀, Y₀), and resolution r meters per cell:

x = floor((X - X₀) / r) and y = floor((Y - Y₀) / r)

Always check bounds and the map origin's pose before indexing. A rotated origin requires a frame transform, not only subtraction.

Why mapping depends on localization

A laser scan does not arrive already placed in the map. The system transforms each beam from the sensor frame through the robot pose into the map frame. If the pose is wrong, walls smear, duplicate, or bend. But the map is also used to correct the pose. This mutual dependence is the core SLAM problem: estimate the trajectory and map together.

Pose graph and loop closure

A pose graph stores robot poses as nodes and relative-motion or scan-matching constraints as edges. When the robot recognizes a previously visited place, a loop-closure constraint says that two distant points in the estimated trajectory represent the same physical neighborhood. Optimization distributes the correction across the graph. Earlier poses can move, so the occupancy grid rendered from their scans changes too.

Loop closure is evidence, not magic. A false match can deform a good map. Systems therefore need similarity checks, geometric verification, robust loss functions, and post-optimization residual monitoring.

Misconception check

  • “Unknown means empty.” No. Unknown means the mapper lacks enough observation evidence; a planner needs an explicit policy for whether it may enter that space.
  • “A clean scan guarantees a clean map.” No. Correct range measurements can still be inserted into the wrong cells when the associated robot pose or frame transform is wrong.
  • “Loop closure edits pixels until the map looks right.” No. A verified loop adds a graph constraint; optimization updates pose estimates, and the map is then reconstructed from scans placed at those corrected poses.

Retrieval pause

A cell is -1 and lies directly ahead of the robot. Is it safe to treat it as free? What evidence and policy would you require before planning through it?

A lidar scan accurately measures a wall, yet the wall appears twice in the map. Which estimate should you inspect first, and why? Start with the robot pose associated with each scan: correct range data transformed through inconsistent poses will be inserted into different map cells.

Summary

An occupancy grid is an uncertainty-aware spatial representation with explicit geometry and indexing. SLAM places sensor evidence by estimating the robot trajectory at the same time. Loop closure can correct accumulated drift by adding a revisited-place constraint, but only when the match is valid.

Further learning

Practice labMake Drift Visible, Then Close the LoopOpen this when you are ready to apply the model, collect evidence, and check your explanation.30 min

Lab: Make drift visible, then close the loop

Objective

Build a small occupancy grid from simulated range observations and compare the map before and after a loop-closure correction.

Materials

  • Graph paper or spreadsheet software
  • A 12×12 grid
  • The supplied pose-scan-placement-table.csv
  • Pencil or annotation tool; a text-only table is an accessible fallback

Setup

Use graph paper or a spreadsheet with a 12×12 grid. Mark cells as ? unknown, . observed free, or # likely occupied. The simulated room is rectangular; the robot follows a loop and returns near its start.

Steps

  1. Set resolution to 0.5 m/cell and origin to (0, 0).
  2. Convert world point (2.2 m, 3.1 m) to a cell. Record (x, y) and row-major index for width 12.
  3. Run the same-scan test. Use a forward range of 2.0 m at bearing . First associate it with pose A (1.0 m, 1.0 m, 0°); then associate the identical scan with drifted pose A′ (1.5 m, 1.0 m, 0°). Mark each endpoint in the grid.
  4. Explain why the sensor data stayed correct while the occupied endpoint moved by one cell: only the estimated pose changed.
  5. Draw four robot poses around a rectangular route. Add a small position error to every new pose so the final pose misses the start by one cell.
  6. From each pose, mark two free cells along each simulated beam and an occupied endpoint.
  7. Describe the visible artifact caused by drift: doubled wall, smeared corner, or open loop.
  8. Propose a loop closure connecting the final pose to the start, but do not accept it yet. Compare a consistent doorway with a mirrored candidate and record which geometric relationship rejects the mirror.
  9. Accept only the consistent match. Redistribute the one-cell error gradually across the four poses and redraw the observations.
  10. Compare the two grids. Identify which cells changed, which constraint justified the change, and whether any unknown cells were incorrectly converted to free.

Expected Result

  • Cell (4, 6) and index 76 for the worked conversion.
  • In the same-scan test, the endpoint moves from world (3.0 m, 1.0 m) / cell (6, 2) to world (3.5 m, 1.0 m) / cell (7, 2) even though range and bearing are unchanged.
  • Unknown cells remain distinct from observed-free cells.
  • The corrected trajectory closes more consistently, and scan endpoints align better.
  • The learner states that a loop closure must be verified; proximity alone is insufficient.

Evidence table

Record each transformation in assets/pose-scan-placement-table.csv. Preserve the original estimate and corrected estimate as separate rows rather than overwriting the drifted result. A complete submission includes the table, a before/after grid, and a two-sentence causal explanation.

Troubleshooting

  • If the index is wrong, calculate x + y × width, not y + x × height.
  • If every blank cell became free, restore it to unknown until a beam observes it.
  • If correction moves only the last pose, distribute the constraint through the trajectory to model graph optimization.
  • If the same-scan endpoints are identical, confirm that pose A′ is shifted by 0.5 m, exactly one grid cell at this resolution.
  • If the mirrored candidate is accepted, identify at least two corresponding geometric features and verify that their relative orientation agrees.

Accessibility fallback

Complete the same activity as a text table listing pose ID, estimated coordinates, observed cells, constraint, and corrected coordinates. No color distinction is required.

Reflection Questions

What additional evidence would you inspect before accepting a loop closure on a real robot?

Why is a visually cleaner map after optimization not, by itself, proof that the corrected map is metrically accurate?

Extension Challenge

Repeat the same-scan test with a 10-degree heading error instead of a position offset. Compare the displaced endpoints, explain why the error grows with range, and state what geometric evidence would be needed before accepting a correcting loop constraint.