Seeing obstacles
Sensors fill a grid map with free, blocked and unknown squares. Then we grow every obstacle by a safety margin, so the robot's body never scrapes past.
In 60 seconds
Seeing obstacles
Sensors fill a grid map with free, blocked and unknown squares. Then we grow every obstacle by a safety margin, so the robot's body never scrapes past.
Building an occupancy grid
- A lidar or depth sensor sends out a beam. Where it hits something, that square is probably occupied.
- The squares the beam passed through on the way are probably free.
- Squares no beam has reached are unknown. A careful robot treats unknown like blocked, or slows down.
- One reading is not trusted fully. Each new reading nudges a square's probability up or down, so a single noisy reading does not create or erase a wall.
import math
# Log-odds: a handy way to add up evidence. 0 means 50/50.
def to_prob(log_odds):
return 1 / (1 + math.exp(-log_odds))
HIT, MISS = 0.85, -0.4 # how much one "hit" or one "passed through" changes our belief
cell = 0.0 # start unknown: 50%
readings = ["hit", "hit", "miss", "hit", "hit", "miss", "hit"]
for i, r in enumerate(readings, 1):
cell += HIT if r == "hit" else MISS
print(f"reading {i} ({r:4}): chance occupied = {to_prob(cell):.2f}")Moving obstacles
Safety margins: inflating obstacles
- The path hugs walls and corners
- The robot's side scrapes the wall
- A small position error causes a crash
- The path keeps a gap from every obstacle
- The whole body fits through
- There is room for sensor and position error
| Margin should grow when | Because |
|---|---|
| The robot moves faster | It needs more distance to stop |
| Position is less certain | GPS or dead reckoning may be off by more |
| It is windy (for drones) | Wind can push the drone sideways |
| People or animals are near | They move unpredictably, and a collision can hurt them |
Watch and read more
Lab
An inflate() function that grows obstacles by a safety margin on a grid, without changing the original map.
The problem
This runs in your browser.
- Finish
inflate(grid, radius). Return a new list of strings. A free cell becomes#if any obstacle is withinradiuscells of it in every direction, including diagonals (the rows apart and the columns apart are both at mostradius). Do not change the input grid. - Run it with radius 1 and 2 and look at the printed maps. Which gaps close?
- Write two sentences: for a drone 30 cm wide, with grid squares of 10 cm, what radius would you choose, and why?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1You inflate by radius 2 and the planner finds no path through the only doorway. What are your honest options?Reveal
Check the numbers first: maybe the margin is larger than needed, or the grid squares are too coarse, so a real gap looks smaller than it is. A finer grid can help. You can also use a hard inner margin plus a soft, costly outer zone, so the robot may pass through the doorway slowly and centred. What you should not do is quietly shrink the margin below the robot's real size plus its position error.
Q2Why is square inflation (including diagonals) slightly different from a real round robot, and does it matter?Reveal
A square grows obstacles a little further along the diagonals than a circle would, so it is slightly more cautious in corners. For most small robots this extra caution is fine. When space is tight, planners can use a round shape, by checking the real distance to the nearest obstacle instead of rows and columns.
Questions people ask
Why use log-odds and not just probabilities?
With log-odds, each new reading is just added. It avoids repeated multiplying, and keeps numbers stable when a square is almost certainly free or blocked. You can turn it back into a probability whenever you need one.
How big should a grid square be?
Small squares show more detail but use more memory and make planning slower. A common approach is to choose squares a little smaller than the smallest gap you care about. Drones in open fields can use much bigger squares than robots inside a house.
Is inflating obstacles the same as a configuration space?
Yes, for a round robot. Growing obstacles by the robot's radius turns "does the body fit?" into "is this point free?". This idea is called planning in configuration space. For long or odd-shaped robots, the direction the robot faces also matters, which makes it more complex.
What should a robot do when a gap is too narrow after inflation?
Plan another route, slow down and look more carefully, or stop and ask a human. It should not quietly shrink its own safety margin just to get through.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.