An occupancy grid is a map made of small square cells, where each cell stores the probability that something solid is there. A robot builds it by firing sensor rays into the world, marking the cells a ray passes through as more likely free and the cell where it ends as more likely occupied.
Key takeaways
- An occupancy grid divides the floor into cells, and each cell holds a probability of being occupied.
- Robots update cells in log-odds form, where each sensor hit adds a fixed number, so repeated evidence builds confidence.
- A ray from the sensor marks cells along the beam as free and the end cell as occupied.
- In ROS 2, the map is an OccupancyGrid message with values 0 to 100 and -1 for unknown, usually produced by a SLAM package.
Cells and Probabilities
A cell is one small square of the floor, for example 5 cm by 5 cm, and its resolution is the cell size. Each cell holds a probability between 0 and 1 that it is occupied. A value near 0 means probably free, near 1 means probably occupied, and 0.5 means unknown, which is where every cell starts.
Keeping a probability, instead of a plain yes or no, matters because sensors are noisy. An ultrasonic reading can be a false echo, and a laser can miss glass. A cell that was marked occupied once should not stay occupied forever on one noisy reading, and a probability lets later evidence correct it. A small cell size gives a detailed map but needs more memory: a 10 m by 10 m room at 5 cm resolution is 200 by 200, which is 40,000 cells.
Log-Odds Update
Log-odds is a way of storing probability as l = ln(p / (1 - p)), where ln is the natural logarithm. Its use is that combining evidence becomes simple addition: each new reading adds a number to the cell, and a cell never needs repeated multiplications.
Typical values, as an illustration: a hit with probability 0.7 adds ln(0.7/0.3) = 0.847, and a miss with probability 0.4 adds ln(0.4/0.6) = -0.405. The table shows one cell, starting unknown with l = 0.
| Event | Log-odds l | Probability p |
|---|---|---|
| Start (unknown) | 0 | 0.50 |
| Hit | 0.847 | 0.70 |
| Hit again | 1.695 | 0.84 |
| Hit a third time | 2.542 | 0.93 |
| Then a miss | 2.137 | 0.89 |
To get back to a probability, use p = 1 / (1 + e^(-l)). For l = 1.695, e^(-1.695) = 0.184, so p = 1 / 1.184 = 0.845. Many implementations also clamp l to a range, so a cell can still change if the world changes, for example when a door opens or a person walks away.
Ray Casting from a Sensor
Ray casting means tracing a straight line from the sensor through the grid, cell by cell, along the direction of one beam. A laser scanner gives hundreds of rays per sweep, while an ultrasonic sensor on a turning servo gives one ray per angle, as in the ultrasonic radar scanner project.
To trace a ray, you need the sensor’s pose on the map, its angle for that beam, and the measured distance. The cells from the sensor out to the measured distance are the ray path. A standard line-drawing method for grids, such as Bresenham’s algorithm, lists them. Uncertainty in the robot’s pose is the main weakness here, because a pose error of a few degrees moves the far end of a long ray by a visible amount. This is why mapping and localisation are done together, as explained in SLAM for beginners.
Free and Occupied Cells
For one ray, the cells before the end point receive the free update, a negative number, because the beam passed through them. The cell at the end point receives the occupied update, a positive number, because something reflected the beam. Cells beyond the end point are not touched, since the sensor said nothing about them.
If a ray returns no echo, for example beyond the sensor’s maximum range, the end cell should not be marked occupied. Mark the cells along the ray free up to the maximum range and leave the end alone. After many scans from different positions, the walls show as high-probability cells, open floor as low, and unvisited places stay at 0.5. For use in navigation, thresholds are set, such as occupied above about 0.65 and free below about 0.2, as typical starting points in many tools. Check the defaults in your own package.
Map Quality Issues
A map is only as good as the pose and the sensor behind it. The common problems are:
- Drift. Odometry errors make walls appear doubled or bent. Loop closure in SLAM corrects this by recognising a place seen before.
- Moving objects. People and carts leave faint occupied smears unless free updates erase them over time.
- Sensor blind spots. Glass, black or shiny surfaces and thin legs of chairs can be missed, and ultrasonic beams are wide and can round off corners.
- Resolution choice. Cells larger than the doorway tolerance can close gaps, and cells that are too small cost memory and make the map noisy.
The costmap layers that Nav2 builds on top of the grid add inflation around obstacles, covered in the Nav2 costmap tuning guide, so a poor base map directly shows up as poor navigation. Review your map in the visualiser before trusting it.
Running One in ROS 2
In ROS 2, an occupancy grid is published as a nav_msgs/msg/OccupancyGrid message. It includes the resolution, width, height, the map origin, and a flat array of cell values: 0 for free, 100 for occupied, and -1 for unknown. The usual way to get one is to run a SLAM package, for example SLAM Toolbox as covered in the SLAM Toolbox ROS 2 tutorial, which publishes the map on the /map topic while you drive the robot around.
After mapping, the Nav2 map server package can save the map to an image and a YAML file, for example with its map saver command line tool, and load it later for navigation. Check the exact command and options in the documentation for your ROS 2 release, since names can change. Small hobby builds can start without ROS by keeping a byte array in an ESP32 or a PC script, and the maze solver robot stores a similar idea in a simple grid of walls. More mapping topics are in the Learn hub.
How to check this yourself
- Take a sheet of graph paper as a 10 by 10 grid, mark a sensor at one edge, and pick a wall cell some distance away in a straight line.
- Trace the ray across the cells and add -0.405 to each free cell and +0.847 to the end cell on a second sheet.
- Repeat from a second position for another ray that ends at the same wall cell, and convert the cell’s l to a probability with 1 / (1 + e^(-l)).
- In ROS 2, drive a robot in SLAM mode, subscribe to the map topic and check that unknown cells show as -1.
Common mistakes
- Marking the end cell occupied when the sensor returned no echo at maximum range.
- Starting cells at 0 probability instead of 0.5, which makes unknown look like free.
- Letting log-odds grow without limits, so the map cannot update after the scene changes.
- Trusting a map built with bad odometry.
- Choosing a resolution without checking doorway widths.
Quick checklist
- Pick a cell size that suits your robot and doorways.
- Start every cell at unknown, 0.5 or log-odds zero.
- Trace each ray, with free updates before the end and an occupied update at it.
- Clamp log-odds so the map can still change.
- Inspect the map visually before using it for navigation.
Frequently asked questions
What is an occupancy grid map?
An occupancy grid is a map made of small square cells, each storing the probability that the cell is occupied by something solid. Robots build it from sensor rays: cells along a beam become more likely free, and the cell where it ends becomes more likely occupied, with unvisited cells staying unknown.
Why do occupancy grids use log-odds?
Log-odds turns the update into simple addition. Each hit or miss adds a fixed number to the cell, so combining many readings is fast and avoids repeated multiplications of probabilities. You convert back to a probability with p = 1 / (1 + e^(-l)) whenever you need a value for display or thresholds.
How do I make an occupancy grid in ROS 2?
Run a SLAM package such as SLAM Toolbox with a laser scanner or similar sensor, and drive the robot around slowly. It publishes an OccupancyGrid message on the map topic. When the map looks right, save it with the Nav2 map server tools, and check the command for your ROS 2 version.
Related reading
- ROS 2 tutorial for beginners: topics and messages, the basics behind the map topic.
- Field-Oriented Control in Plain Language
- Battery State of Charge: Coulomb Counting Explained
About the author and how this is kept accurate
Written, checked and maintained by Jignesh Panchal of Jaadu Digital. Figures in worked examples come from the stated assumptions, not from a bench test of our own. Read the editorial standard, follow the author on LinkedIn, or send a correction. Last reviewed: November 2026.



