Skip to the lesson
FixTheRobot Explore Robotics / Localization & SLAM / M3 Mapping and State Estimation / M3-T1

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.

  • Lesson 1 of 8
  • Module 3: Mapping and State Estimation
  • Theory
  • About 8 minutes to read

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.

A laser beam through a gridA grid of cells with a beam from the sensor in the bottom-left cell to a hit in a cell two rows up and five columns across. The cells along the beam are marked free (blue) and the last cell is marked occupied (orange).sensor free cells: l ← l + l_freehit cell: l ← l + l_occ cells beyond the hit: unchangedl = log( p / (1 − p) ) Cells on the ray come from Bresenham’salgorithm: (0,0) (1,0) (2,1) (3,1) (4,2) (5,2).
One beam updates many cells: the cells it passes through become more free, the cell where it ends more occupied, and cells behind the hit are not touched.

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:

Log-odds occupancy update
l(mi)=log⁡(p(mi)1−p(mi))l(m_{i})=\log (\frac{p(m_{i})}{1-p(m_{i})})
p=11+e(−l)p=\frac{1}{1+e^{(-l)}}
lt(mi)=lt−1(mi)+invsensor(mi, xt, zt)−l0l_{t}(m_{i})=l_{t-1}(m_{i})+\mathrm{inv}_{\text{sensor}}(m_{i},\,x_{t},\,z_{t})-l_{0}
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

  1. Take the pose xt and the scan; convert each beam’s endpoint into world coordinates and then into cell indices.
  2. Trace the beam from the sensor cell to the endpoint cell with Bresenham’s line algorithm.
  3. 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).
  4. Clamp each l to a range such as [−5, 5] so that the map can still change if the world changes.
  5. 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

Strengths and limitations of occupancy grids
StrengthLimitation / failure mode
Simple, fast, no feature extraction; handles any obstacle shapeMemory grows with area: a 100 m × 100 m floor at 5 cm resolution has 4 million cells
Free space is explicit, which planners needPose error smears walls into thick or doubled lines (the map is only as good as the poses)
Probabilistic: noise averages out over repeated beamsStatic-world assumption: moving people leave streaks and ghosts
Log-odds is cheap: one addition per cell per beamGlass, 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).

  1. One hit: l = 0.847, p = 1/(1 + e−0.847) = 0.70.
  2. Two hits: l = 1.695, p = 0.845. Three hits: l = 2.542, p = 0.927. The evidence accumulates by simple addition.
  3. 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.
  4. 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.
  5. 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

  1. An occupancy grid keeps one log-odds value per cell and updates it by addition: lfree along the beam, locc at the hit.
  2. Bresenham’s algorithm gives the cells a beam passes through; cells behind the hit are not updated.
  3. Clamping the log-odds keeps the map adaptable; the thresholds 0.35 and 0.65 give free, unknown and occupied.
  4. The map is only as sharp as the poses it was built with: pose error turns into wall thickness.
  5. 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.

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.

Practice this lesson

Build a 2D Occupancy Grid

Turn known-pose range scans into an occupancy grid with a log-odds update and ray tracing.

Next lesson

M3-T2 — Landmark-Based Mapping

Represent a map as landmark positions with covariances and update them from known poses.

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.