Physical AI · Level 2 · Intermediate

How Robots Build a Map: Lidar Scans and Occupancy Grids

How a robot turns lidar scans into a map of free space and walls, shown in our simulation, and why the map falls apart as soon as the robot's position drifts.

By 6 min read
How a robot turns lidar scans into a map of free space and walls, shown in our simulation, and why the map falls apart as soon as the robot's position drifts.

A robot that enters a new building knows nothing about it. Before it can plan a route to the loading dock or the kitchen, it has to find out where the walls, shelves and doors are. It builds this knowledge from what its sensors see, scan by scan, into a map. The most common kind of map for this is the occupancy grid: the floor is divided into small squares, and for each square the robot estimates whether it is free, occupied or still unknown.

This is part 1 of Level 2 in our learning path Physical AI, which looks at how the systems work inside. We simulated a robot with a lidar sensor driving through a made-up warehouse with four rooms, shelves and doors. You will learn what a lidar scan contains, how a grid map collects evidence from many scans and why the best map is worthless if the robot is wrong about its own position.

What a lidar sees

A 2D lidar is a laser that spins and measures the distance to the nearest object in many directions. Our simulated sensor sends 360 beams per turn, one per degree, with a range of 10 meters and a measurement noise of about 2 centimeters, typical values for the lidars on delivery and warehouse robots. Every beam tells the robot two things at once: the space along the beam up to the measured distance is free, and the point where the beam stopped is occupied. A beam that hits nothing within its range only says that the space along it is free.

Three maps of the same floor plan in gray, white and black: after one scan, only a star-shaped patch around the robot is known; after ten scans, the first room is mapped; after all scans, the whole building with its walls, shelves and doors is visible
The map after 1, 10 and all scans. White is free, black is occupied, gray is still unknown.

Evidence instead of yes or no

The robot does not simply mark a square as free or occupied after one reading. Sensors make mistakes, people walk through the beam, and glass can reflect a laser in strange directions. Instead, every square keeps a running score, called log-odds. Each time a beam passes through the square, the score goes down a little; each time a beam ends in it, the score goes up. Only when enough evidence has collected does the square count as free or occupied. A single wrong reading changes the score, but it cannot overturn many good ones.

How Robots Build a Map: Lidar Scans and Occupancy Grids
State of a squareWhat it meansTypical cause
UnknownNo beam has reached it yetBehind a wall, around a corner, out of range
FreeMany beams passed through itOpen floor, hallways, doorways
OccupiedMany beams ended in itWalls, shelves, pillars
UnsureEvidence points both waysMoving people, glass, edges of objects

Our simulation: the map grows with every scan

Our robot drove 55.6 meters through the building and took a scan every half meter, 119 scans in total. After ten scans, it had explored 42.4% of the free floor area, mostly the first room. By the end, it had explored 94.3%. The remaining patches lie behind shelves and in corners that no beam could reach from the robot’s route. Because the robot in this run knew its exact position, the walls in the map sit where they belong: 100% of the squares marked as occupied lie on or right next to a real wall.

How the map grows with every scan
ScansArea exploredWalls on target
535.2%100.0%
1042.4%100.0%
2062.2%100.0%
4070.3%100.0%
8089.0%100.0%
11994.3%100.0%
119, drifting position93.8%55.9%

Our own simulation: a made-up floor plan of 24 × 14 m, grid 10 cm; lidar with 360 beams, range 10 m, noise 2 cm; one scan every 0.5 m along a 55.6 m route. Occupancy grid with log-odds. Drift: heading 0.1 °/m, distance 2% too long. Explored = share of the free area recognized as free; on target = share of cells marked occupied that lie at most one cell from a real wall.

The first scans add the most, and the curve then flattens. Every new room brings a jump, every turn into a known corridor adds little. This is why mapping robots plan their routes to visit places they have not seen yet, a strategy called exploration.

Why the position must be right

The map is only as good as the robot’s knowledge of where it stood for each scan. In a second run, we used exactly the same scans, but the robot placed them at the position it estimated from its wheels and gyroscope. As in our simulation of localization in Level 1, this estimate drifts slowly: here by 0.1 degrees of heading per meter and 2% in distance.

Two maps of the same building side by side: on the left, sharp walls from the run with the exact position; on the right, the run with a drifting position, where walls appear twice, slightly rotated and shifted, and the map gets blurrier toward the end of the route
The same scans, placed at the exact position (left) and at a slowly drifting position (right).

The result is a map in which walls appear twice, slightly rotated and shifted. Only 55.9% of the occupied squares still lie at a real wall; 44.1% are ghost walls that do not exist. A robot planning with this map would see closed doors where there are openings and open space where there is a shelf.

This is why mapping and localization are solved together. The method is called SLAM, simultaneous localization and mapping. While the robot builds the map, it also compares each new scan with the map so far and corrects its position when the two do not fit. When it returns to a place it has seen before, it can correct the drift of the whole loop at once, which is called loop closure.

What changes in the real world

Our simulation shows the core of the method, but real buildings are harder:

  • Glass and mirrors. Lasers pass through glass doors or bounce off mirrors, so the map can show walls in the wrong place or none at all.
  • Moving things. People, forklifts and doors change the scene. Robots keep their maps up to date and treat moving objects separately.
  • Three dimensions. A 2D lidar at one height misses a table top or an overhanging shelf. Many robots therefore combine it with 3D lidar or depth cameras.
  • Size and memory. A warehouse of several hectares at 10 centimeters per square needs millions of squares, so large maps are split into tiles and loaded as needed.

How we ran the simulation

We drew a floor plan of 24 by 14 meters with four rooms, five shelves and doors, divided it into squares of 10 centimeters and computed the lidar beams against the walls in Python with numpy, with a fixed random seed for the noise. Explored means the share of the free floor area that the map marks as free; on target means the share of occupied squares that lie at most one square from a real wall. The next part of this level uses a map to plan: how a robot or a car finds the shortest route through a real street network.