Localization & SLAM · Theory · M3
M3-T1 — Occupancy Grid Mapping
Divide the world into cells, and for each cell keep the probability that something is there.
Occupancy grids are the map that most mobile robots actually plan on. Each cell holds a belief that it is free or occupied, updated by every laser beam that passes through or ends in it. This lesson derives the log-odds update, shows how to trace a beam through the grid, and lists the assumptions that quietly break real grid maps.
Learning objectives
After this lesson, you should be able to:
- State the occupancy mapping problem and its assumptions, including known robot poses.
- Derive the log-odds update and apply it to a cell over several observations.
- Trace a beam through a grid with Bresenham’s algorithm and identify free and occupied cells.
- Explain how resolution, memory, pose error and dynamic objects limit a grid map.
The problem and the intuition
Picture a sheet of graph paper laid over the floor plan. A laser beam goes out from the robot. Every square it crosses must have been empty, otherwise the light would have stopped there; the square where it stops probably has something in it. Do this thousands of times and the squares that were often crossed are free, those that were often hit are occupied, and the rest are unknown. That is occupancy grid mapping.
A grid map does not store objects. It stores, for every cell, how strongly the evidence says “empty” or “full”. Unknown is a real state.
The algorithm: occupancy by log-odds
Problem. Given the robot poses x1:t and range scans z1:t, estimate for each cell mi the probability p(mi = occupied | x1:t, z1:t).
Assumptions. The poses are known (this is mapping, not SLAM); the cells are independent of each other; each cell is either free or occupied and does not change over time; the sensor noise is independent between beams and scans.
The log-odds update
Multiplying probabilities from many observations is numerically awkward. In log-odds form, the Bayes update becomes an addition:
- l(mi)
- log-odds of cell i being occupied. 0 means 50% (unknown); positive means more likely occupied; negative more likely free.
- inv_sensor
- the inverse sensor model: the log-odds contributed by one beam to the cell: locc for the cell at the beam end, lfree for cells the beam crossed, 0 for cells not reached.
- l0
- the prior log-odds, 0 when the prior is 50%.
- phit, pmiss
- the probabilities behind the two constants: locc = log(phit/(1−phit)), lfree = log(pmiss/(1−pmiss)).
Pipeline
- Take the pose xt and the scan; convert each beam’s endpoint into world coordinates and then into cell indices.
- Trace the beam from the sensor cell to the endpoint cell with Bresenham’s line algorithm.
- Add lfree to every cell on the ray except the last, and locc to the last cell (only if the beam hit something, that is, the range is below the sensor’s maximum).
- Clamp each l to a range such as [−5, 5] so that the map can still change if the world changes.
- Convert to probabilities when the map is used: p = 1 / (1 + e^(−l)). Typical thresholds: occupied above 0.65, free below 0.35, unknown in between.
Strengths and limitations
| Strength | Limitation / failure mode |
|---|---|
| Simple, fast, no feature extraction; handles any obstacle shape | Memory grows with area: a 100 m × 100 m floor at 5 cm resolution has 4 million cells |
| Free space is explicit, which planners need | Pose error smears walls into thick or doubled lines (the map is only as good as the poses) |
| Probabilistic: noise averages out over repeated beams | Static-world assumption: moving people leave streaks and ghosts |
| Log-odds is cheap: one addition per cell per beam | Glass, mirrors and black surfaces cause missing or false hits; cells are assumed independent |
A worked example: one cell, several beams
Take phit = 0.7 and pmiss = 0.4 (so a hit says “probably occupied” and a pass-through says “probably free”). Then locc = log(0.7/0.3) = +0.847 and lfree = log(0.4/0.6) = −0.405. A fresh cell starts at l = 0 (p = 0.5).
- One hit: l = 0.847, p = 1/(1 + e−0.847) = 0.70.
- Two hits: l = 1.695, p = 0.845. Three hits: l = 2.542, p = 0.927. The evidence accumulates by simple addition.
- A contradicting pass-through (the cell was also crossed by a beam that went through it): after hit, hit, miss l = 0.847 + 0.847 − 0.405 = 1.289, p = 0.784. One contradiction lowers confidence but does not erase two hits.
- Bresenham. A beam from cell (0, 0) to cell (5, 2): the line rises 2 cells over 5 columns, so the cells along it are (0,0), (1,0), (2,1), (3,1), (4,2), (5,2). The first five receive lfree and the last locc.
- Clamping. With limits ±5, a cell hit 20 times sits at 5 (p = 0.993) rather than 17. If the object moves, about 12 contradicting beams (5 / 0.405) are enough to flip the cell to free, instead of 42.
Note what the example does not need: any knowledge of what the object is, or even whether the same object caused each hit.
Engineering implications and limitations
Engineering implications
- Choose the resolution to match the sensor and the task. A cell smaller than the sensor noise adds memory without information; a cell larger than the robot’s clearance hides doorways. 5 cm is common indoors.
- Sparse or tiled storage is necessary for large maps: allocate blocks only where beams have been, and keep submaps (Module 5) so that a global correction does not need rewriting the whole grid.
- Apply the update in the right order. Cells near the end of a beam are affected by the range noise: some systems update a small neighbourhood of the endpoint, or lower lfree near the hit.
- Use good poses. The map blurs by exactly the pose error. A 5 cm drift turns a 5 cm resolution map into one with walls two cells thick. Scan matching (M3-T4) before updating keeps the walls sharp.
- Handle dynamic objects. Clamped log-odds lets the map recover, but a person standing still for ten seconds becomes a wall. Filter dynamic returns (Module 8) or keep a separate short-term layer for the planner.
Limits
Occupancy grids are a good map for planning and a poor one for recognising places or for optimisation: a grid has no compact features to match. They assume independence of cells, which is false for walls (neighbouring cells are strongly related) and a source of over-confidence. 3D versions (voxels, OctoMap) pay a steep memory price.
Common misconceptions
“Unknown is the same as free.”
Unknown cells have never been observed. Planning through them is a gamble, and many planners treat them as obstacles or as exploration targets.
“A cell with p = 0.5 means the robot checked and found it half full.”
It usually means it has not been observed, or the evidence has cancelled out.
“More beams always make the map sharper.”
More beams reduce sensor noise but cannot fix pose error, which affects all beams of a scan together.
“The grid stores objects.”
It stores the evidence of space being free or occupied; an object is a pattern in the grid that a later stage interprets.
Engineering takeaways
- An occupancy grid keeps one log-odds value per cell and updates it by addition: lfree along the beam, locc at the hit.
- Bresenham’s algorithm gives the cells a beam passes through; cells behind the hit are not updated.
- Clamping the log-odds keeps the map adaptable; the thresholds 0.35 and 0.65 give free, unknown and occupied.
- The map is only as sharp as the poses it was built with: pose error turns into wall thickness.
- Resolution, memory and dynamic objects decide whether a grid is practical.
References and further reading
Established fundamentals. Learned and neural map representations are covered in Module 8.
- Moravec, H. and Elfes, A. (1985). High resolution maps from wide angle sonar. IEEE International Conference on Robotics and Automation: the original occupancy grid paper.
- Elfes, A. (1989). Using occupancy grids for mobile robot perception and navigation. Computer 22(6).
- Thrun, S., Burgard, W. and Fox, D. (2005). Probabilistic Robotics. MIT Press, chapter 9: occupancy grid mapping with the log-odds formulation.
- Hornung, A. et al. (2013). OctoMap: An efficient probabilistic 3D mapping framework based on octrees. Autonomous Robots 34(3).
- Bresenham, J. E. (1965). Algorithm for computer control of a digital plotter. IBM Systems Journal 4(1).
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A cell has been hit by five beams and passed through by none. What is its probability with locc = 0.847 and no clamping, and what could clamping at ±5 change?
Explanation
l = 5 × 0.847 = 4.24, so p = 1/(1 + e^−4.24) ≈ 0.986. The clamp at 5 does not act yet (4.24 < 5), but after about six hits it would, keeping the cell flippable by a modest number of contradicting beams.
Question 2
Why must the cell where a beam ends receive locc only when the range is less than the sensor’s maximum range?
Explanation
A maximum-range reading means “nothing detected up to the limit”, not “an obstacle at the limit”. Marking the last cell occupied would create false walls at the edge of the sensor’s reach. In that case only the free-space update applies.
Question 3
A robot’s heading drifts slowly during a long corridor run, and the finished grid shows the corridor walls as thick curved bands. Is the grid algorithm at fault?
Explanation
No. The algorithm places each beam using the pose it is given. Drift in the pose spreads the same wall across neighbouring cells. The remedy is better poses (scan matching, loop closure), not a different grid update.
What’s next
A grid is one kind of map. Next, maps made of explicit landmarks, with a covariance for each.
Related content
Nothing below is required to finish this lesson. The last 2 entries are lessons of other courses on this site that teach the background or go further.
- BeforeM2-T7: Data Association
Match observations to landmarks with nearest neighbour, Mahalanobis gating and joint compatibility, and see how wrong matches break estimates.
- LessonM5-T11: Dense Map Representations
Compare occupancy octrees, signed distance fields and surfels, fuse range measurements into a TSDF, and say what a loop closure does to a dense map.
- LabM3-L1: Build a 2D Occupancy Grid
Turn known-pose range scans into an occupancy grid with a log-odds update and ray tracing.
- ProbabilityOccupancy Grids and the Binary Bayes Filter
The log-odds update derived from the binary Bayes filter.
- ProbabilityInverse Sensor Models
How a range measurement becomes an occupancy update.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.