Skip to the lesson
FixTheRobot Explore Robotics / Robot Foundations / Module 6 / Lesson 2

Robot Foundations · Module 6: Building a Map

Occupancy Grids: Free, Occupied, Unknown

Every beam says two things: "nothing here, nothing here, nothing here" all the way along, and "something here" at the end.

  • Lesson 2 of 3
  • Beginner
  • No code
  • About 8 minutes

A single laser reading is one number: the distance to the first thing the beam met. It still says a great deal. If the beam travelled 2.2 m before it stopped, then the 2.2 m before that point are clear, the end point is solid, and what lies beyond is a mystery. A grid map is nothing but that statement, repeated for thousands of beams and written into squares. This lesson does the writing by hand for one beam, then shows how a room fills in.

What you will be able to do

  • Update a row of cells from one range reading.
  • Say which cells become free, occupied or stay unknown, and why.
  • Explain why a free mark must not overwrite an occupied one.
  • Describe how a room fills in as the robot scans from more places.

The idea

One beam, cell by cell

Start with a map where every cell is unknown. The robot measures one distance along one direction. The rule is short.

  1. Every cell the beam passes through before it stops becomes free. The light got through, so nothing is there.
  2. The cell where the beam stops becomes occupied, if it stopped because it hit something (not because it ran out of range).
  3. Every cell beyond that stays unknown. The beam never got there.
One range reading written into a row of cellsA row of ten cells, each half a metre wide, numbered 0 to 9. The robot is at the start of cell 2 and measures 2.2 metres straight ahead. Cell 2 to cell 5 are free. Cell 6 is occupied because the beam stopped in it. The cells after it are unknown, and so are the cells behind the robot.0123456789robotreading 2.2 mcell number
One reading of 2.2 m from the robot at x = 1 m, written into 0.5 m cells. Light blue: free. Dark: occupied. Grey: unknown.

Why is the beam's end cell occupied, but cell 7 unknown? The beam stopped in cell 6, so it hit something there. It did not travel any further, so it said nothing about cell 7. The thing it hit could be a thin sheet or a thick wall. All that is known is that its front surface is in that cell.

Cells before the end of the beam: free. The cell at the end: occupied. Everything after: unknown.

A beam that hits nothing

If the beam runs out of range without meeting anything, every cell along it is free, and no cell becomes occupied. The sensor has reached its limit, and the cells past the limit stay unknown. The end of a beam is a wall only if the beam hit something.

Free must not erase occupied

A beam grazing past the corner of a wall may skim through a cell that another beam has already marked occupied. Without a rule, the "free" from the skimming beam would wipe out the wall, and the next beam would write it back: the wall would flicker. So the lab's rule, and the usual one, is: a free mark never overwrites an occupied one. The wall stays until stronger evidence removes it. (Real systems store a probability in each cell, and many beams vote. That is the proper fix for noisy sensors, and it is the subject of the Localization and SLAM and Probability tracks.)

A whole room

A scanner fires many beams around the robot, and the update above is done for each. After one scan, everything in view is known. Everything hidden is still unknown.

A grid map of a room filling in as the robot scansThree grid maps of the same six by four metre room. The first is all grey, unknown. The second, after one scan from the middle of the left half, shows the walls and the box's left face as dark cells, free space in light blue, and a few grey unknown cells behind the box and the pillar. The third, after scans from three places, shows no grey cells left.Before any scanAfter one scanAfter three scans
A 6 by 4 m room with a box and a pillar, in 0.5 m cells. The orange dots are where the robot stood to scan. Known cells: 0% before, 89% after one scan, 100% after three.

The 11 grey cells after one scan are the shadows of the box and the pillar. To fill them in, the robot has to move and look from somewhere else. This is why lab 4 asks you to explore: a map grows only where the robot goes and looks.

One more requirement is hidden in all of this. To write a beam into the grid, the robot must know where it was standing and which way it faced when the scan was taken. That is the subject of the next lesson.

Words to know

Free cell
A cell a beam passed through. Nothing is there. On Pip: cells 2 to 5 in the figure.
Occupied cell
A cell where a beam stopped because it hit something. On Pip: cell 6 in the figure.
Unknown cell
A cell no beam has reached. On Pip: the cells behind the box after one scan.
Range reading
The distance along one beam to the first thing it met, or the maximum range if it met nothing. On Pip: one number per beam, 72 beams per scan.
Coverage
The share of the cells that are no longer unknown. On Pip: 89% after one scan in the figure.
Explore
Move to new places so that new cells come into view. On Pip: the point of the first half of lab 4.

A worked example: writing one reading into the grid

The robot is at x = 1 m, in the middle of a row of 0.5 m cells, facing along the row. Its scanner reports 2.2 m. Cell number i covers x from i × 0.5 to (i + 1) × 0.5.

  1. Where does the beam stop? At x = 1 + 2.2 = 3.2 m.
  2. Which cell is that? 3.2 ÷ 0.5 = 6.4, so the cell number is the whole part: 6. It covers x from 3 to 3.5 m.
  3. Cells before it. The robot's own cell is number 2. The beam passes through cells 2 to 5 before reaching cell 6, so these are free: 2, 3, 4, 5.
  4. The end cell. Cell 6 is occupied.
  5. The rest. The cells after it (7, 8, 9) and the cells behind the robot (0, 1) stay unknown.

The result, cell by cell: 0: unknown; 1: unknown; 2: free; 3: free; 4: free; 5: free; 6: occupied; 7: unknown; 8: unknown; 9: unknown.

The whole room. One scan from the room's left half knows 89% of the 96 cells and 32 of them are occupied. Two further scans from other places raise that to 100%. Each new scan adds knowledge: the number of unknown cells fell from 11 to 0.

Try it yourself

On squared paper draw a row of 10 cells and number them 0 to 9, each one 0.5 m wide. The robot is in cell 1 (x from 0.5 to 1.0 m) at x = 0.75 m and measures 2.6 m ahead. (a) At what x does the beam stop and in which cell? (b) Which cells are free, occupied and unknown? (c) A second reading from the same place, in the opposite direction, hits a wall 0.9 m behind. What does the map now say?

Answer

(a) 0.75 + 2.6 = 3.35 m, in cell 6 (3.0 to 3.5 m). (b) The beam passes through cells 1 to 5: free. Cell 6 is occupied. Cells 7 to 9 are unknown, and cell 0, behind the robot, is unknown until the second reading. (c) Going backwards, the beam stops at 0.75 − 0.9 = −0.15 m, which is outside the row: the wall is just beyond cell 0. Cell 0 becomes free (the beam crossed it) and the wall is at the edge. Now only cells 7 to 9 are unknown.

Common mistakes

"The cell behind the hit cell is occupied too, because the wall continues."

The beam never went there. The wall might be one cell thick. Unknown is the honest answer until another beam from another place shows it.

"If the scan reaches the maximum range, the last cell is a wall."

Only if it hit something. A beam that ran out of range found nothing, and its cells are all free.

"One scan makes the map."

One scan makes the part in view. The shadow cells behind objects, and everything out of range, need the robot to move.

Remember this

  1. A reading marks the cells it passed through free, the cell where it stopped occupied, and leaves the rest unknown.
  2. A beam that finds nothing leaves everything along it free and marks no wall.
  3. A free mark never overwrites an occupied one, so a wall does not flicker.
  4. One scan leaves shadows. The map grows only where the robot goes and looks.

Check yourself

Three questions. Write an answer in your own words, then reveal the explanation. Nothing you type leaves your browser.

Question 1

The robot is at x = 2.0 m, facing along a row of 0.5 m cells, and the scanner reports 1.3 m. Which cell is the hit cell?

Explanation

The beam stops at 2.0 + 1.3 = 3.3 m. 3.3 ÷ 0.5 = 6.6, so it is cell 6 (x from 3.0 to 3.5 m).

Question 2

A beam with a range of 4 m travels 4 m and meets nothing. What does the map record?

Explanation

All the cells along the beam, up to 4 m, become free. No cell becomes occupied, because the beam did not hit anything. Cells beyond 4 m stay unknown.

Question 3

Why must a free mark not overwrite an occupied cell?

Explanation

A beam skimming past a wall corner or a noisy reading can pass through a cell that really is occupied. If free overwrote occupied, that one beam would erase a wall the map already knew about, and the map would flicker between wall and no wall. Keeping occupied marks until there is stronger evidence keeps the map steady.

What’s next

You can build a map from readings. One thing was taken for granted: the position the robot stood at. The next lesson asks what happens when that position is wrong.

Next lesson

A Map Needs a Pose

Show how a wrong position smears a map, and state the chicken-and-egg problem that SLAM solves.

The lab of this module (after lesson 3)

Build the Robot's World

Turn range readings into an occupancy grid, explore a room, and see what a wrong pose does to the map. About 25–35 minutes.

Where this goes on the site

Nothing below is needed to continue this course. These pages take the topic of this module further, and most assume more mathematics or Python than this course does.