An occupancy grid map helps a robot understand which areas around it are free or blocked. It turns the space into a simple grid of squares that say if they are empty, occupied, or unknown.
Occupancy grid map concept in ROS
nav_msgs/OccupancyGrid
header
info
map_load_time
resolution
width
height
origin
data[]header contains timestamp and frame info.
data is a 1D array representing the grid cells, with values: -1 (unknown), 0 (free), 100 (occupied).
data = [-1, 0, 100, 0, -1] width = 5 height = 1
info.resolution = 0.05 # each cell is 5cm x 5cm info.width = 10 info.height = 10
This code creates a 3x3 occupancy grid where some cells are free (0) and some are occupied (100). It prints the grid rows to show the map layout.
from nav_msgs.msg import OccupancyGrid from std_msgs.msg import Header from geometry_msgs.msg import Pose # Create a simple 3x3 occupancy grid grid = OccupancyGrid() grid.header = Header() grid.header.frame_id = "map" grid.info.resolution = 1.0 # 1 meter per cell grid.info.width = 3 grid.info.height = 3 grid.info.origin = Pose() # default at (0,0,0) grid.data = [0, 0, 100, 0, 100, 100, 0, 0, 0] print("Occupancy Grid Data:") for i in range(grid.info.height): row = grid.data[i * grid.info.width:(i + 1) * grid.info.width] print(row)
Cells with value -1 mean the robot does not know if they are free or blocked yet.
The grid is stored as a single list but represents a 2D map, so you need to calculate rows and columns.
The resolution tells how big each cell is in the real world, so smaller values mean more detail but bigger data.
An occupancy grid map divides space into small squares to show free, occupied, or unknown areas.
It helps robots plan paths and avoid obstacles safely.
The map data is a simple list of numbers representing each cell's state.
Practice
What does an occupancy grid map represent in ROS?
Solution
Step 1: Understand the purpose of occupancy grid maps
Occupancy grid maps divide space into small cells to mark if they are free, occupied, or unknown.Step 2: Match the description to the options
A grid showing which areas are free, occupied, or unknown correctly describes this as a grid showing cell states. Other options describe unrelated concepts.Final Answer:
A grid showing which areas are free, occupied, or unknown -> Option AQuick Check:
Occupancy grid = space divided into free/occupied/unknown cells [OK]
- Confusing occupancy grid with robot commands
- Thinking it is a 3D robot model
- Mixing it up with raw sensor data
Which data type correctly represents the occupancy grid map data in ROS?
nav_msgs::OccupancyGrid map;
// What type is map.data?Solution
Step 1: Recall the ROS message type for occupancy grid data
The map.data field is a list of signed 8-bit integers representing cell states.Step 2: Match the data type to the options
std::vector<int8_t> matches the expected type. Other options do not fit the ROS message definition.Final Answer:
std::vector<int8_t> -> Option CQuick Check:
map.data type = std::vector<int8_t> [OK]
- Assuming map.data is a string or float array
- Thinking it is a 2D bool array
- Confusing with sensor_msgs data types
Given this snippet in ROS Python:
grid = [0, 100, -1, 0]
free_cells = sum(1 for cell in grid if cell == 0)
occupied_cells = sum(1 for cell in grid if cell == 100)
unknown_cells = sum(1 for cell in grid if cell == -1)
print(f"Free: {free_cells}, Occupied: {occupied_cells}, Unknown: {unknown_cells}")What will be printed?
Solution
Step 1: Count free cells (value 0) in the list
grid has two zeros at positions 0 and 3, so free_cells = 2.Step 2: Count occupied cells (value 100) and unknown cells (value -1)
One 100 at position 1 (occupied_cells = 1), one -1 at position 2 (unknown_cells = 1).Final Answer:
Free: 2, Occupied: 1, Unknown: 1 -> Option DQuick Check:
Count values 0, 100, -1 correctly [OK]
- Miscounting zeros or other values
- Confusing unknown (-1) with free (0)
- Mixing occupied (100) with unknown (-1)
Identify the error in this ROS Python code snippet that tries to update an occupancy grid cell:
map_data = [0, 0, 0, 0]
index = 2
map_data[index] = 100
print(map_data)Why might this code fail in a real ROS occupancy grid update?
Solution
Step 1: Check data type and mutability
map_data is a list here, so assignment works in Python, no error from mutability.Step 2: Understand ROS occupancy grid update rules
In ROS, occupancy grid data must be updated through the OccupancyGrid message interface, not by directly modifying a local list copy.Final Answer:
map_data should be updated via the OccupancyGrid message, not a plain list -> Option AQuick Check:
Update occupancy grid via ROS message, not local list [OK]
- Assuming direct list change updates ROS map
- Thinking index 2 is invalid
- Believing 100 is invalid occupancy value
You want to create a 5x5 occupancy grid map in ROS where the center cell is occupied (value 100), edges are unknown (-1), and others are free (0). Which Python code correctly creates the data list for this map?
# Options:
# A
# data = [100 if i == 12 else -1 if i % 5 == 0 or i % 5 == 4 else 0 for i in range(25)]
# B
# data = [0]*25
# data[12] = 100
# for i in range(25):
# if i % 5 == 0 or i % 5 == 4 or i < 5 or i > 19:
# data[i] = -1
# C
# data = [-1]*25
# data[12] = 100
# for i in range(25):
# if i % 5 != 0 and i % 5 != 4 and 5 <= i <= 19:
# data[i] = 0
# D
# data = [0 if i != 7 else 100 for i in range(25)]
# for i in range(25):
# if i % 5 == 0 or i % 5 == 4 or i < 5 or i > 19:
# data[i] = -1Solution
Step 1: Understand the map layout requirements
Center cell (index 12) = 100, edges = -1, others = 0 in a 5x5 grid (indexes 0-24).Step 2: Analyze each option's logic
Starts with all 0s, sets center to 100, then sets edges to -1 in a loop, matching requirements exactly.Final Answer:
Starts with all 0s, sets center 100, then sets edges to -1 in a loop -> Option BQuick Check:
Initialize edges as -1, center 100, inner cells 0 [OK]
- Confusing edge indexes with inner cells
- Overwriting center cell after setting edges
- Using incorrect conditions for edges
Ready to prove it?
Pass the test and this pattern moves unknown
