Navigation · Theory · M2
M2-T4 — Layered Costmaps
One costmap, many sources: how a robot combines a building map, live sensors and rules about people without letting one of them spoil the others.
A static map is out of date the moment someone parks a cart. A laser sees the cart and not the corridor beyond the glass. A rule says to stay out of the surgery wing. No single source is enough, so the costmap is built in layers, each doing one job, and a planner reads only the combination.
Learning objectives
After this lesson, you should be able to:
- Describe the static, obstacle, inflation and social layers and what each contributes.
- Explain marking and clearing, and how raytracing removes obstacles that have left.
- Explain why ghost obstacles appear and list the usual fixes.
- Combine layers into a master costmap and say why order and update rates matter.
The intuition
Think of transparent sheets laid over a floor plan. The bottom sheet is the building: it does not change. The next sheet is drawn by the robot with a marker as its sensors see things, and wiped with a cloth where its sensors see through. Another sheet adds a halo around everything on the sheets below. A last sheet adds “do not go here” stamps, for example around a ward round in progress. The planner looks down through all the sheets at once.
Each layer is simple because it ignores the others. That is also why a mistake in one layer is easy to find: switch it off, and the problem vanishes.
The mathematics
The layers
| Layer | Source | Job |
|---|---|---|
| Static | The saved map | Walls and fixed furniture; changes only when the map does |
| Obstacle | Lidar, depth camera, sonar | Marks what sensors hit, clears what they see through |
| Inflation | The cells marked by the layers before it | Grows lethal cells into the cost profile of the previous lesson |
| Social / keep-out | Rules, zones, tracked people | Costs for personal space, forbidden or slow zones |
Combining layers
Each cell of the master takes the largest cost any layer gives it, so no layer can hide another’s obstacle. Unknown has its own value, which the policy of lesson M2-T1 resolves.
Marking and clearing
For each sensor reading, the obstacle layer does two things:
- Marking: the cell where the beam ended, if it ended on something within the marking range, becomes occupied.
- Clearing: the cells the beam passed through, from the sensor to its end (a raytrace), become free.
A beam that returns nothing clears all the way to its maximum range and marks nothing. The ranges are separate parameters, so a robot may clear far but mark near. Common defaults are 2.5 m for marking and 3.0 m for clearing.
Why ghosts appear
- No beam passes through: the obstacle left but nothing looks through its cells (a blind spot, or the robot moved away).
- Clearing range too short: the robot marked the obstacle from 2 m but only clears to 1 m.
- Sensor field of view: a camera sees ahead only, so obstacles beside the robot are never cleared.
- Wrong hit flag: maximum-range readings reported as hits create phantom walls (lab M2-L3).
- Dynamic obstacles: a walking person leaves a trail of marks behind them unless the beams behind them clear it.
A worked example
A laser that clears. A 360° lidar with 0.5° steps puts 720 beams in a scan. At 4 m, neighbouring beams are 4 × tan(0.5°) = 0.035 m apart, closer than a grid cell of 0.10 m, so every cell between the robot and a wall at 4 m is crossed by at least one beam, and a moved cart disappears within one scan.
Where it breaks. A camera with a 70° field of view cannot clear the cells beside the robot. A cart parked there stays on the map until the robot turns towards it. The fix is to give the layer an expiry (observation persistence) so that old marks without fresh evidence fade, or to keep a lidar for clearing.
The hospital hallway. A cart that left at 13:00 remains because the only sensor that saw the hallway looks forward. The routes in the lab (M2-L3) show the repair: trace each beam, clear what it crosses, mark only a real return, and treat a beam without a return as free. Check the layer after a change by looking at a picture of it, not only at the planner’s behaviour.
Engineering implications and limitations
Debug one layer at a time
If a path goes the wrong way, publish and look at each layer’s own costmap before the master. The faulty layer is usually obvious.
Expiry is a safety net
Time-based expiry for obstacle marks catches the ghosts that clearing misses. Choose a time long enough that the robot does not forget a wall behind it, and short enough that a parked cart that has gone does not linger.
Rates
The static layer updates rarely, the obstacle layer at sensor rate, the master at a fixed rate. A slow update rate makes a moving robot plan on a stale map, which looks like clipping a corner it had “already seen”.
Layers for people and rules
A social layer raises costs around people and in front of them. A keep-out layer sets cells to lethal or to a speed limit. Both belong in layers, not in the planner, so they can be toggled by the operator and tested alone (lesson M2-T5).
Common misconceptions
“Marking is the hard part.”
Clearing is. Marking is a hit; clearing needs a beam that crosses the cell and a policy for what “no return” means.
“A ghost is a sensor error.”
It is a memory error. The sensor was correct once, and the layer never learned that the world changed.
“More layers make a safer map.”
More layers make a more capable map, and every layer is another source of cost to understand. Add a layer for a reason.
“The planner should deal with stale obstacles.”
It should not have to: the costmap should not contain them. Planners that second-guess the map hide the bug.
Engineering takeaways
- Layers keep sources separate: static, obstacle, inflation, and social or keep-out rules.
- The master costmap takes the maximum per cell, so no layer hides another’s obstacle.
- Marking sets the cell a beam ended in; clearing sets the cells it crossed.
- Ghosts come from missing clearing, short ranges, narrow fields of view, wrong hit flags and moving obstacles.
- Expiry and visual debugging, one layer at a time, are the practical cures.
References and further reading
The layered costmap paper and the Nav2 documentation.
- D. Lu, D. Hershberger and W. Smart, “Layered Costmaps for Context-Sensitive Navigation”, IEEE/RSJ IROS, 2014.
- S. Macenski, F. Martín, R. White and J. Ginés Clavero, “The Marathon 2: A Navigation System”, IEEE/RSJ IROS, 2020.
- Nav2 documentation, “Obstacle Layer Parameters” and “Voxel Layer Parameters”, docs.nav2.org.
Knowledge check
Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
Two layers give a cell costs 0 and 253. What is the master cost, and why?
Explanation
253. The master takes the maximum, so a layer that sees an obstacle cannot be overruled by one that does not.
Question 2
A forward-looking camera marks a box beside a corridor and the robot drives past. The box is then removed. Why does the cell stay occupied?
Explanation
No beam of the camera crosses the cell once the robot has moved on, so nothing clears it. An expiry for old marks or a sensor that looks sideways fixes it.
Question 3
Marking range is 2.5 m and clearing range is 3.0 m. A wall is seen at 2.8 m. What is marked and what is cleared?
Explanation
Nothing is marked, because the hit is beyond the marking range. The cells along the beam up to the hit are cleared, as the hit is inside the clearing range.
What’s next
Next: when a grid is the wrong map: roads, lanes and meaning.
Related content
Nothing below is required to finish this lesson.
- BeforeM2-T3: Distance Transforms and Euclidean Distance Fields
Compute a distance field with a two-pass sweep and use it to find clearance and a gradient away from obstacles.
- LabM2-L3: Find the Ghost Obstacles
A cart left hours ago still blocks the hallway on the map: fix the marking and clearing logic.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.