The robot keeps a chance of "occupied" for every cell, and drives to the edge of what it knows. On this toy floor, that rule maps every reachable cell in 89 moves. A random walk knows 79% after 400.
Pick a way to explore and watch the grey fog turn into a map. Then we build, piece by piece, the grid, the belief in one cell, what one beam says, the log-odds sum, the scan, the frontier and the choice of where to go.
You need what a probability is and a little Python. Lesson 1 showed one map cell adding log-odds; we start from there and go deeper.
Lesson 12 of 15 in our follow-along of Stanford AA 274A, Principles of Robot Autonomy I. Every idea is re-taught here in our own words and devices. Poses are known here; Lesson 11, SLAM, drops that.
Illustrative toy: a 28 by 18 cell floor, 72 beams of 6 cells, +0.85 per hit and −0.40 per miss. A cell counts as known below p = 0.3 or above 0.7. The robot never sees the pink truth; it plans on its purple belief.
Chapter 0
Drive a robot by hand through a floor it cannot see, and feel what it needs
A delivery robot rolls into a building with no floor plan. It must reach a room somewhere down a hall. It has one sensor: a spinning laser that, ten times a second, reports the distance to the nearest surface in each direction.
Before it plans one move, it must answer one question: which patches of floor are safe, and which are walls? The answer is a map: a record, for a machine, of free space and occupied space.
The robot scans where it stands. Click any clear (known free) cell to drive there; it scans again on arrival. Try to know the whole floor in as few drives as you can.
Illustrative toy, the hero's floor. You may only drive onto cells the robot already believes are free, the same rule a real planner follows.
Two problems show up at once. The sensor is noisy: a glossy floor can send one beam back short, so no single reading can be the truth. And somebody must choose where to look next. Here that was you.
This lesson solves both. Each cell stores a chance of being occupied, and every beam adds evidence. Then the frontier, the line where known free space meets the unknown, tells the robot where to drive next.
The symptom is a map that flickers: a cell reads "wall", then "free", then "wall" on the next sweep. To measure it, count how often each cell changes its label over ten sweeps of a still room. The fix is to add up evidence per cell, which Chapters 2 and 4 build.
Chapter 1
Turn a cell size up and down and watch a doorway open and close
Lay a square grid over the floor. Each square is a cell. Each cell stores one number: the chance that something solid fills it. That 2D array of chances is an occupancy grid.
The robot reads each cell as one of three labels:
The robot's position is in metres; the grid is in whole cells. Call the robot's x position in metres x, the x position of the grid's corner x0, and the cell size r (the resolution). The column of the cell is i.
i equals the floor of x minus x-zero, over r.
Subtract the corner x-zero from the point x. Divide by the cell size r. Round down. That is the column i. Do the same with y for the row.
With made-up numbers: x = 3.42 m, x-zero = 0, r = 0.05 m. 3.42 / 0.05 = 68.4, so i = 68. Any x from 3.40 m to just under 3.45 m lands in the same cell.
Pink lines are the true walls, with a 0.6 m doorway. A cell turns purple if any wall touches it. Drag the cell size and watch whether a free path through the doorway survives.
Illustrative room, 6 m by 4 m, walls 0.1 m thick. Memory is for a 20 m by 20 m floor at 4 bytes per cell.
| Cell size r | Cells on a 20 m side | Cells | Memory at 4 B |
|---|---|---|---|
| 0.50 m | 40 | 1,600 | 6.4 KB |
| 0.10 m | 200 | 40,000 | 160 KB |
| 0.05 m | 400 | 160,000 | 640 KB |
Halve the cell size and the count goes up four times, because cells fill an area. A small indoor robot often uses 5 to 10 cm: fine enough for a doorway, small enough to update fast.
The symptom is a planner that says "no path" in front of an open door. To measure it, count free cells across the doorway in the grid. If there are none, the cells are too big. Halve r until free cells span the gap, with room for the robot's width.
Chapter 2
Feed the same noisy readings to a yes-or-no cell and to a belief, and see which one lies
Suppose each cell stores yes or no. A beam glances off a shiny floor and comes back short. The free cell where it stopped flips to "wall". The planner now drives around nothing.
The fix treats each cell as its own small estimation problem. The cell is truly occupied or truly free. The robot only sees it through noisy beams, so it keeps a belief: the chance P that the cell is occupied. Each reading updates P with Bayes' rule. This is the Bayes filter with two states, called the binary Bayes filter.
Take one reading: a beam stopped in this cell, a hit. Call the event "the cell is occupied" o. Call the belief before the reading P(o). Two numbers describe the sensor. p(hit | o) is how often an occupied cell gives a hit. p(hit | not o) is how often a free cell gives a false hit.
P of o given hit equals p of hit given o, times P of o, over that same product plus p of hit given not o, times one minus P of o.
Multiply the old belief P of o by how well "occupied" explains the hit. Do the same for "free". Divide the first by the sum of both. The new belief replaces the old one, ready for the next reading.
With made-up sensor numbers: P(o) = 0.5. P(o | hit) = 0.35 / (0.35 + 0.15) = 0.70. A second hit: 0.49 / (0.49 + 0.09) = 0.845. The belief climbs, but each step needs a multiply and a divide.
The cell is truly free. Each reading is a miss, except a few false hits. The top row trusts the last reading. The bottom row keeps a belief. Raise the error rate and press Run.
Illustrative: a hit multiplies the odds by 7/3, a miss by 2/3 (the numbers of Chapter 4). A cell reads "wall" above 0.7. Fixed random seed per error rate.
One more choice makes a whole map cheap. The robot treats every cell as independent: the chance of a whole map is the product of the chances of its cells. So it runs one small filter per cell, not one giant filter over 2160,000 possible maps.
Independence is wrong: walls are long, and a cell cannot know its neighbour is a wall. The symptom is a speckled wall with one-cell gaps. To measure it, count free cells with occupied cells on both sides along a known wall. The fix is more scans from more places, so each wall cell gets more hits.
Chapter 3
Slide a wall along a beam and watch each cell get its vote
A beam returns one number: the range z to the first surface. The map needs a statement about every cell along the beam. The rule that turns z into those statements is the inverse sensor model. "Inverse" because it runs from reading to map; a forward model runs from map to reading.
The beam splits its cells into three zones. Call a cell's distance from the robot along the beam r. Real walls have depth, so the robot marks a band of thickness α (alpha) around the hit as occupied.
p of m given z and r equals p-free if r is less than z minus alpha over two; p-occ if r is within alpha over two of z; and p-zero if r is beyond z plus alpha over two.
Walk along the beam. Before the band around z, say "free". Inside the band, say "occupied". Past the band, say nothing. If z is the sensor's maximum range, no wall was seen, so every cell out to that range says "free".
With made-up numbers: z = 3.0 m, alpha = 0.2 m. A cell at r = 2.85 m gets 0.4 (free). Cells at 2.95 m and 3.05 m get 0.7 (occupied). A cell at 3.15 m gets 0.5: no update.
The top plot is p(m | z, r) along the beam. The row of cells under it shows each vote. Drag the range to the far end to see a max-range reading.
Illustrative: 20 cells of 0.2 m, a maximum range of 4.0 m. A cell's vote uses the distance r of its centre.
One beam does not mark one cell. It certifies a whole corridor of free cells and one occupied band. That is why maps fill in fast.
The votes are gentle on purpose: 0.4 and 0.7, not 0.05 and 0.95. One beam is a hint. Certainty must come from many beams that agree. A noisy sonar wants even gentler votes and a thicker α than a sharp laser.
The symptom is a robot that will not move: everything it has not hit looks unsafe. To measure it, count free cells after a scan of an open room; it stays near zero. The cause is a model that votes only at the hit. The fix is the free zone: every cell before the hit gets pfree.
Chapter 4
Open a door where a wall stood and count how long the map takes to notice
Lesson 1 showed the short form: store the log-odds ℓ = ln(p / (1 − p)) and add +0.85 per hit, −0.40 per miss. Here is where those numbers come from, and the two terms that keep a real map honest.
Call the cell's log-odds after reading t ℓt. The beam's vote from Chapter 3 is p(m | zt). The prior, the belief before any reading, has log-odds ℓ0.
ell-t equals ell-t-minus-one, plus the natural log of p of m given z-t over one minus p of m given z-t, minus ell-zero.
Turn the beam's vote into log-odds. Add it to the cell's running total. Subtract the prior ell-zero once, because the vote already contains the prior. Turn the total back into a chance with p = 1 / (1 + e−ℓ) when the planner asks.
Votes 0.7 and 0.4: ln(0.7 / 0.3) = 0.85 and ln(0.4 / 0.6) = −0.41, which we round to −0.40. Prior 0.5, so ell-zero = 0. Hit, hit, hit, a stray miss, hit: 0.85, 1.70, 2.55, 2.15, 3.00. As chances: 0.70, 0.85, 0.93, 0.90, 0.95.
The stray miss moves the cell from 0.93 to 0.90. It cannot undo three hits. That is the robustness Chapter 2 asked for, at the cost of one addition per cell.
Take a warehouse where most cells are free, so the prior is p0 = 0.2 and ℓ0 = −1.39. A beam that says nothing returns the prior, 0.2. With the subtraction, it adds −1.39 + 1.39 = 0. Without it, each empty beam adds −1.39 again: after 5 such beams the cell reads p = 0.0002, free with no evidence at all.
A wall hit 500 times reaches ℓ = 425. Then a door opens there. Each miss subtracts 0.40, so the cell needs 1,065 misses to read free: about 106 s at one miss per scan at 10 Hz. A clamp keeps ℓ between −3.5 and +3.5 (p from 0.03 to 0.97). From +3.5 the cell reads free after 11 misses, about 1 s.
The cell gets hits while the wall stands, then misses after the door opens. Set how long the wall stood and where the clamp sits. Count the misses until the cell reads free.
Illustrative: +0.85 per hit, −0.40 per miss, one reading per scan at 10 Hz. "Free" means p below 0.3, which is ℓ below −0.85.
The symptom is a ghost wall: a door opened a minute ago and the planner still routes around it. To measure it, log ℓ of a cell you know changed and count scans until it flips. The fix is the clamp. A lower clamp forgets faster but trusts a wall less.
Chapter 5
Fire a fan of beams and watch each one walk its line of cells
A LiDAR scan is a list of beams, each an angle and a range. For each beam the robot needs two things: the cell where it stopped, and every cell it crossed on the way.
First, the hit point. Call the robot's pose (x, y, θ): its position in metres and its heading. Call the beam's angle in the robot's own frame φ (phi) and its range z.
x-hit equals x plus z times the cosine of theta plus phi; y-hit equals y plus z times the sine of theta plus phi.
Add the beam angle phi to the heading theta to get the beam's world direction. Walk z metres that way from the robot's position x, y. Then turn the point into a cell with Chapter 1's floor rule.
With made-up numbers: the robot is at (2.0, 1.0) facing 90°, the beam is at −30°, z = 3.0 m. World direction 60°. x-hit = 2.0 + 3.0 × 0.5 = 3.50; y-hit = 1.0 + 3.0 × 0.866 = 3.60. At 5 cm cells: robot cell (40, 20), hit cell (70, 71).
Second, the cells between. Bresenham's line algorithm walks a straight line across a grid with whole-number additions only. It steps one cell along the longer axis each time, and an error counter says when to step the other axis too.
| Step | Cell (x, y) | Vote |
|---|---|---|
| 0 | (0, 0) | free: the robot's own cell |
| 1 | (1, 0) | free |
| 2 | (2, 1) | free: the error counter steps y |
| 3 | (3, 1) | free |
| 4 | (4, 2) | occupied: the hit |
One beam from (0, 0) to (4, 2): four free votes and one occupied vote. A scan of a few hundred beams updates thousands of cells, and cells near the robot get votes from many beams at once.
Press Fire to cast every beam from the teal robot. Each beam walks its Bresenham cells: free votes along the way, a hit vote at the wall. Fire again to add evidence; drag the beam count down to see gaps.
Illustrative room of 24 by 16 cells, beams of 9 cells, +0.85 per hit and −0.40 per miss, clamp ±3.5. A cell crossed by two beams gets two votes.
Now build it. The lab below hands you the room, the beams and the Bresenham walker; you write the votes.
The symptom is a phantom wall across an open doorway. To measure it, find occupied cells whose beams were all at maximum range. The cause is code that always marks the last cell as a hit. The fix: a max-range beam votes free all the way out, with no hit.
Chapter 6
Scan one wall from two places, tilt one heading, and watch the wall split
One scan maps what one spot can see. To map a building, the robot drives, scans from each new pose, and adds every scan into the same grid. In log-odds this is only addition, so the order of the scans does not matter.
The chapter's quiet assumption does a lot of work. To place a beam in the right cells, the robot must know its pose at the moment of the scan. Occupancy grid mapping assumes the poses are known and correct. SLAM drops that assumption and estimates the poses and the map together; inside, it often runs a mapper like this one.
What if the heading is off? Call the heading error Δθ (delta-theta) and the beam range z. The hit point slides sideways by e.
e equals z times the sine of delta-theta.
The beam pivots about the robot by the heading error delta-theta. Its tip moves z times the sine of that angle. Divide e by the cell size to get the error in cells.
With made-up numbers: z = 3.0 m, delta-theta = 5°. e = 3.0 × 0.087 = 0.26 m. At 5 cm cells, that is 5 cells off: one wall turns into two.
The robot scans a straight pink wall from pose A, then from pose B. Add a heading error to pose B and watch its purple wall split away from A's.
Illustrative: cells of 0.1 m, the wall 2.0 m ahead, 31 beams per scan. The worked number above uses 3.0 m and 5 cm cells.
| Error source | What it does to the map | What helps |
|---|---|---|
| Pose error | a wall drawn twice, or thick | SLAM, Lesson 11 |
| Cell size | a slanted wall becomes a staircase | smaller r, at 4 times the memory |
| Glass, mirrors | beams vanish or return wrong ranges | gentle votes, many beams |
| People walking | a trail of short-lived walls | the clamp, Chapter 4 |
The symptom is a crisp, confident, wrong map: every wall doubled. More scans only make it more confident. To measure it, map a straight corridor and measure the wall's thickness in cells. The fix is a better pose, from SLAM or scan matching, not more mapping.
Chapter 7
Find every free cell that touches the fog, then group them into goals
The robot has a partial map: islands of free space and walls in a sea of grey. Where should it go next? If a person must point, the robot is not autonomous. It needs to find the edge of its own knowledge.
Brian Yamauchi named that edge in 1997. A frontier cell is a cell that is known free and has at least one unknown neighbour. Call a cell c, its four side neighbours N(c), and the set of frontier cells F.
F is the set of cells c such that c is free and some n in N of c is unknown.
Visit every cell c. Skip it unless it is free. Look at its four neighbours in N of c. If any one is unknown, put c in F.
In the 5 by 5 map below, 7 cells are free. Cell (2, 3) has three unknown neighbours: it is in F. Cell (2, 2) has none: it is not. F holds 3 cells.
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | X | X | X | U | U |
| 1 | X | F | F | U | U |
| 2 | X | F | F | F | U |
| 3 | X | F | F | U | U |
| 4 | X | X | X | U | U |
The frontier cells, as (row, column), are (1, 2), (2, 3) and (3, 2). They touch at their corners, so they form one frontier region. The robot groups touching frontier cells (all eight neighbours count here) with a flood fill. It keeps each region's size and its centroid, the average position: here size 3, centroid (2, 2.33).
Pale yellow cells are frontier cells; each ring is a region's centroid. Click a cell to cycle it: free, wall, unknown. Watch regions split and merge.
Illustrative belief map of 18 by 11 cells. Detection uses the four side neighbours; grouping uses all eight.
The symptom is a robot that drives into grey space and stops at a wall it could not see. To measure it, check the label of each goal cell; some are unknown. The cause is targeting the unknown side of the edge. The fix: a frontier cell is always free, so the robot can stand on it.
Chapter 8
Weigh what a frontier will reveal against the trip, and turn the dial that sets the robot's curiosity
Frontier-based exploration is one loop: map, find frontiers, choose one, plan a path with A*, drive, repeat. It stops when no frontier is left. Only the choice is new.
The simplest rule is nearest frontier: go to the region with the shortest path. The path length counts, not the straight-line distance, because a wall can turn a near frontier into a long walk.
A smarter rule weighs gain against cost. Call a frontier f. Its gain(f) is how many unknown cells it should reveal; its region size is a fair guess. Its cost(f) is the path length to it. A weight λ (lambda) sets how much one new cell is worth in cells of travel.
U of f equals lambda times gain of f, minus cost of f.
For each frontier f, multiply its gain by lambda. Subtract its cost. Keep the frontier with the biggest U. If no frontier is reachable, exploration is done.
With made-up numbers and lambda = 1: A (gain 8, cost 5) scores +3, B (20, 12) scores +8, C (30, 28) scores +2. B wins. With lambda = 2: A +11, B +28, C +32. C wins.
Three frontiers wait at different distances. Slide lambda and watch the bars of U. The yellow ring marks the pick. Switch to Nearest to see a rule that ignores gain.
Illustrative numbers from the worked example. The pick changes at lambda = 0.58 (A to B) and lambda = 1.6 (B to C).
Where do the switches come from? A and B tie when 8λ − 5 = 20λ − 12, so at λ = 7/12, about 0.58. B and C tie when 20λ − 12 = 30λ − 28, so at λ = 1.6. A small λ mops up close edges; a large λ crosses the building for a big room.
When does the loop end? If the free space the robot can reach is closed by walls, any unknown part of it must border a free cell, and that cell is a frontier. So "no reachable frontier" means every reachable cell is known.
The symptom is a robot that shuttles between two far rooms, never finishing either. To measure it, log the chosen frontier each cycle and count switches between regions. The cause is a gain estimate that jumps between scans. The fix is to lower λ, or to add a small bonus for the current target.
Chapter 9
Run the whole loop phase by phase, then wall the robot in and watch it cope
Every piece now runs together on the hero's floor. Each cycle has four phases:
Press Run, or Step to go one phase at a time. Click a cell to add or remove a true wall, even mid-run. Show the truth to compare the pink world with the purple belief.
Illustrative toy, the hero's floor and sensor. Lambda only matters under Best U. A frontier the robot reaches but cannot resolve is marked and skipped.
Try three things. Step through one cycle and read the phase chips. Wall off the door ahead of the robot: the next Select finds no path there and picks another frontier, or stops if it is sealed in. Then punch a hole into a mapped wall: new frontiers appear behind it and the robot goes back.
Nothing in the loop needs a prior map or a human waypoint. It asks one local question, "where does my map end?", and answers it again after every scan.
Chapter 10
Keep the terms, the numbers, the code and the links in one place
An occupancy grid stores one log-odds number per cell. Each beam votes free along its line and occupied at its hit, and the votes add. With known poses, all scans add into one grid. Frontiers, free cells next to unknown ones, tell the robot where to look next, and the loop ends when none is left.
| Term | Meaning | Number to remember |
|---|---|---|
| Labels | free below 0.3, occupied above 0.7 | every cell starts at 0.5 |
| Resolution | i = floor((x − x0) / r) | 20 m at 5 cm: 160,000 cells, 640 KB |
| Binary Bayes | one filter per cell | two hits: 0.50, 0.70, 0.845 |
| Inverse model | free before z − α/2, occupied within α/2 | votes 0.4 and 0.7 |
| Log-odds | ℓ += ln(vote odds) − ℓ0 | +0.85 per hit, −0.40 per miss |
| Clamp | keep ℓ in [−3.5, +3.5] | 11 misses to forget, not 1,065 |
| Heading error | e = z sin(Δθ) | 3.0 m at 5°: 0.26 m |
| Frontier | free, with an unknown side neighbour | 5 by 5 example: 3 cells, 1 region |
| Utility | U = λ gain − cost | λ = 1: B (+8); λ = 2: C (+32) |
python import numpy as np L_HIT, L_MISS, L_MAX = 0.85, -0.40, 3.5 def integrate_scan(L, robot_cell, beams): for end_cell, hit in beams: # one beam = one Bresenham line ray = bresenham(robot_cell, end_cell) for (cx, cy) in ray[:-1]: L[cy, cx] += L_MISS # the beam flew through: free ex, ey = ray[-1] L[ey, ex] += L_HIT if hit else L_MISS # max range: free, no hit return np.clip(L, -L_MAX, L_MAX) # the clamp def explore(robot, lidar, lam=1.0): L = np.zeros((H, W)) # prior 0.5 everywhere while True: L = integrate_scan(L, robot.cell, lidar.read(robot.pose)) grid = label(L) # FREE below 0.3, OCC above 0.7 regions = cluster(frontier_cells(grid)) target = choose_frontier(regions, robot.cell, lam) # max lam*gain - cost if target is None: return L # no reachable frontier: done robot.follow(astar(grid, robot.cell, target))
| Topic | Lesson | How it connects |
|---|---|---|
| Maps and the stack | Lesson 1 | the grid is one of four maps; one cell's log-odds |
| A* planning | Lesson 3 | plans the path to each chosen frontier on this grid |
| LiDAR and ICP | Lesson 7 | the beams this lesson ray-casts, and scan matching |
| SLAM | Lesson 11 | drops the known-pose assumption |
| Kalman filters | Lesson 13 | estimates the poses this lesson took as given |
Based on Stanford AA 274A, Principles of Robot Autonomy I. This page re-teaches the course's lesson on occupancy mapping and frontier exploration in our own words, devices and toy numbers. The frontier idea is from Brian Yamauchi (1997).
Now press Present or Teach and explain from memory: the three zones of a beam, the log-odds update with its prior and clamp, a Bresenham line, the frontier test and U. Then go back to the fog, pick Random walk, and say why it never finishes.