Maps in ROS2: storing an occupancy grid as a grayscale image
Atlasbot · 17/8/2026 · 11 min read
The article What is navigation? places Map as the first piece of the puzzle. The standard map format in ROS2 —Occupancy Grid— is much simpler than "map" in the usual sense: it is just a grid of squares, each carrying a probability value.
Key concepts
Each cell in the grid carries one of three states:
0 → free (empty, traversable by the robot)
100 → occupied (contains an obstacle)
-1 → unknown (not yet scanned)
The entire map is nothing more than a 2D array of integers — published on topic /map with message type nav_msgs/msg/OccupancyGrid, accompanied by resolution (the real size of each cell, e.g. 0.05m = 5cm/cell) and origin (the real coordinates of the map angle, to switch between cell index and real world coordinates in map, as stated in the article Coordinates in Robot ).
How it works
On disk, the map is saved in two files: my_map.pgm — real graycale images, white=free, black=occupied, gray=unknown; and my_map.yaml — metadata including resolution, origin, free/occupied threshold. .pgm (Portable GrayMap) is an extremely simple gray image format that can be opened with any image viewer — this is why you can open the robot map with Paint or GIMP to manually edit (delete noise, add virtual walls) before reloading into the robot, without the need for specialized tools.
After SLAM (separate section) has constructed and saved the map, map_server is the node that reads the file .yaml/.pgm and re-pubes to the topic /map — this is the step of transition from "mapping" (SLAM) to "using existing map" (AMCL + Nav2), exactly the boundary stated in the article What is AMCL?.
Summarized: Occupancy Grid deliberately is simple — with only 3 states per cell, saved as a common grey image — in return for simplicity that is wide compatibility: every SLAM tool, every path planning algorithm, every common image editing software reads/records the same format.
Technical details
Formulas
resolution (resolution) — tradeoff between map details and computed capacity/speed:
Smaller resolution (smaller cells) → more detailed map, better detection of small obstacles
→ larger files, slower costmap computation
Larger resolution (larger cells) → lighter and faster, but misses small obstacles
Grid_cell_count = (Physical_width / resolution) × (Physical_height / resolution)
Example: warehouse map of 40m × 30m, resolution 0.05m
Cell count = (40/0.05) × (30/0.05) = 800 × 600 = 480,000 cells
Parameters
| resolution | Matching |
|---|---|
| 0.02-0.03m | Small space, high detail (lab, narrow area) |
| 0.05m | Common for indoor AMRs — balance details and performance |
| 0.1 m or more | Large space (large warehouse, outdoor), speed priority costmap |
Practical applications
Common values for indoor AMR: 0.05m (5cm/cell) — detailed enough to distinguish table legs, small columns, while keeping a reasonable file size and costmap calculation speed on embedded hardware such as Raspberry Pi 4 or Jetson Orin Nano (as learned in Choose Computer for AMR). With large inventories (hundreds of square meters), choosing too small a resolution can cause the number of grid cells to skyrocket, significantly slowing down the costmap calculation at each cycle — which needs to balance the real-world detail demand and the available hardware limitations.
Practical example
# Save built map from SLAM to 2.pgm +.yaml files
ros2 run nav2_map_server map_saver_cli -f my_map
# Reread saved map, publish to topic / map
ros2 run nav2_map_server map_server --ros-args -p yaml_filename:=my_map.yaml
# Check if the map being published is correct
ros2 topic echo /map --once
Typical my_map.yaml content:
image: my_map.pgm
resolution: 0.05
origin: [-10.0, -8.0, 0.0]
occupied_thresh: 0.65
free_thresh: 0.25
negate: 0
origin means here that the bottom-left corner of the image .pgm corresponds to the coordinates (-10.0, -8.0) in the map— system all the conversions between pixel index and real-world robot coordinates are based on these two resolution and origin numbers.
Common mistakes
- Choosing too small resolution for large— spaces as calculated in the Formula section, the number of grid cells increases by the square when reducing resolution, easily causing the costmap to compute slowly unnecessarily on embedded hardware.
- Banding of.pgm files forgetting to update threshold occupied_thresh/free_thresh suitable — to add virtual walls in absolute black (0) but the occupied threshold is set higher than that and the tile has not been identified as occupied.
- The confusion between the origin of the map and the origin of the robot when debugging the coordinates— origin in the file
.yamlis the map angle, not the boot position of the robot, two completely different concepts that are confusing at first learning. - Do not check that
/maphas been published correctly before suspecting AMCL/Path Planning — if the map has not been published or published incorrectly, every back layer (Localization, Path Planning) fails accordingly, this should be the first debugging step.
Conclusion
Grid occupancy is a deliberately simple mapping format — of 3 states per cell, stored as a common grey image — in exchange for wide compatibility with every SLAM tool, path planning, and also conventional image editing software. Choosing the correct resolution according to the size of the space and available hardware is an important design decision, which directly affects the performance of all rear levels. The next article in the column goes to Localization. — We've learned how to do robot self-localization on the map here.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.