Mapping and SLAM
What you’ll learn
- Maps robots use: occupancy grids
- Localization: where am I on a map?
- SLAM: building the map and localizing at the same time
Occupancy grids
The world is split into small squares (for example 5 × 5 cm). Each cell holds the probability that it’s occupied: free, wall, or unknown. Distance sensors fill it in: cells along a ray are free, the cell where the ray hits is occupied.
Localization
With a map, the robot compares what its sensors see now with what they should see from each possible position. A popular method keeps many guesses (“particles”) and keeps the ones that match best: Monte Carlo localization.
SLAM
Simultaneous Localization and Mapping solves both at once, a chicken-and-egg problem: you need a map to know where you are, and you need to know where you are to build a map.
- Odometry (encoders, IMU) gives a first guess of movement, which drifts.
- Scan matching lines up new sensor data with the map to correct it.
- Loop closure: when the robot recognises a place it’s seen before, it corrects all the drift around the loop.
Sensors for SLAM
- 2D lidar (a spinning laser): the classic choice for indoor robots and robot vacuums.
- Cameras (visual SLAM): cheaper, harder.
- Depth cameras for 3D maps.
In ROS 2, packages such as slam_toolbox build maps from lidar data, and Nav2 plans routes on them.
Example: filling a grid from one ray
The robot is at cell (10, 10) facing east, and the distance sensor reads 40 cm with 10 cm cells:
- Cells (11,10), (12,10), (13,10) → free
- Cell (14,10) → occupied
- Cells beyond → still unknown
Example: why loop closure matters
A robot drives round a square corridor. Odometry says it ended 30 cm away from where it started, but its lidar recognises the starting doorway. SLAM shifts the whole loop’s path to close that 30 cm gap, and the map’s walls line up.
Grid by handEasy
On squared paper, draw a 10 × 10 grid room. Place a robot and “measure” four directions; mark free and occupied cells.
DriftMedium
A robot’s odometry drifts 2% of the distance travelled. After driving 25 m, how far off could it be? 2% of 25 m = 0.5 m.
Explain loop closureHard
Explain loop closure to a friend using the example of walking around your school with your eyes half closed.
Quick check
Finished this lesson?