Bird
Raised Fist0
ROSframework~10 mins

Occupancy grid map concept in ROS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Occupancy grid map concept
Start: Robot senses environment
↓
Sensor data collected
↓
Convert sensor data to grid cells
↓
Update occupancy probabilities
↓
Store updated grid in map
↓
Use map for navigation or planning
↓
End
The robot collects sensor data, converts it into a grid of cells, updates each cell's occupancy probability, and stores this in a map used for navigation.
Execution Sample
ROS
for cell in grid:
  if sensor_detects_obstacle(cell):
    cell.probability = update_prob(cell.probability, True)
  else:
    cell.probability = update_prob(cell.probability, False)
This code updates each grid cell's occupancy probability based on sensor detection.
Execution Table
StepCell IndexSensor Detects Obstacle?Previous ProbabilityUpdated ProbabilityAction
10Yes0.50.7Increase probability - obstacle likely
21No0.50.3Decrease probability - free space likely
32Yes0.50.7Increase probability - obstacle likely
43No0.50.3Decrease probability - free space likely
54No0.50.3Decrease probability - free space likely
6End---All cells updated, map ready
💡 All grid cells processed; occupancy probabilities updated based on sensor data.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
cell[0].probability0.50.70.70.70.70.70.7
cell[1].probability0.50.50.30.30.30.30.3
cell[2].probability0.50.50.50.70.70.70.7
cell[3].probability0.50.50.50.50.30.30.3
cell[4].probability0.50.50.50.50.50.30.3
Key Moments - 3 Insights
Why does the probability increase when the sensor detects an obstacle?
Because the sensor data suggests the cell is occupied, so the probability is updated to reflect higher confidence in an obstacle, as shown in steps 1 and 3 of the execution_table.
Why do some cells have their probability decreased?
When the sensor does not detect an obstacle in a cell, the probability is lowered to indicate free space, as seen in steps 2, 4, and 5 in the execution_table.
What happens if sensor data is noisy or uncertain?
The update_prob function adjusts probabilities gradually rather than jumping to 0 or 1, allowing the map to reflect uncertainty and improve over time.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the updated probability of cell 1?
A0.5
B0.3
C0.7
D0.0
💡 Hint
Check the 'Updated Probability' column for step 2 in the execution_table.
At which step does cell 3's probability first change from the start value?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the variable_tracker row for cell[3].probability to see when it changes.
If the sensor detected an obstacle in cell 4 at step 5, how would the updated probability change?
AIt would increase to 0.7
BIt would decrease to 0.3
CIt would stay at 0.5
DIt would reset to 0.0
💡 Hint
Refer to how probabilities increase when obstacles are detected in the execution_table steps 1 and 3.
Concept Snapshot
Occupancy Grid Map Concept:
- Divide environment into grid cells.
- Each cell holds a probability of being occupied.
- Sensor data updates these probabilities.
- Probabilities increase if obstacle detected, decrease if free.
- Map guides robot navigation and planning.
Full Transcript
An occupancy grid map divides the robot's environment into small cells. Each cell stores a probability that shows how likely it is to have an obstacle. The robot uses sensors to detect obstacles and updates these probabilities accordingly. If the sensor sees an obstacle in a cell, the probability for that cell goes up. If no obstacle is detected, the probability goes down. This map helps the robot understand where it can safely move and where it should avoid. The process repeats as the robot moves and senses more, improving the map over time.

Practice

(1/5)
1.

What does an occupancy grid map represent in ROS?

easy
A. A grid showing which areas are free, occupied, or unknown
B. A list of robot commands to move in space
C. A 3D model of the robot's shape
D. A sensor data stream from the robot's camera

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    A grid showing which areas are free, occupied, or unknown -> Option A
  4. Quick Check:

    Occupancy grid = space divided into free/occupied/unknown cells [OK]
Hint: Occupancy grid = map of free or blocked spaces [OK]
Common Mistakes:
  • Confusing occupancy grid with robot commands
  • Thinking it is a 3D robot model
  • Mixing it up with raw sensor data
2.

Which data type correctly represents the occupancy grid map data in ROS?

nav_msgs::OccupancyGrid map;
// What type is map.data?
easy
A. bool[][]
B. std::string
C. std::vector<int8_t>
D. float[]

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    std::vector<int8_t> -> Option C
  4. Quick Check:

    map.data type = std::vector<int8_t> [OK]
Hint: Occupancy data uses vector of int8_t in ROS [OK]
Common Mistakes:
  • Assuming map.data is a string or float array
  • Thinking it is a 2D bool array
  • Confusing with sensor_msgs data types
3.

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?

medium
A. Free: 1, Occupied: 1, Unknown: 2
B. Free: 1, Occupied: 2, Unknown: 1
C. Free: 2, Occupied: 0, Unknown: 2
D. Free: 2, Occupied: 1, Unknown: 1

Solution

  1. Step 1: Count free cells (value 0) in the list

    grid has two zeros at positions 0 and 3, so free_cells = 2.
  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).
  3. Final Answer:

    Free: 2, Occupied: 1, Unknown: 1 -> Option D
  4. Quick Check:

    Count values 0, 100, -1 correctly [OK]
Hint: Count each cell value carefully to avoid off-by-one errors [OK]
Common Mistakes:
  • Miscounting zeros or other values
  • Confusing unknown (-1) with free (0)
  • Mixing occupied (100) with unknown (-1)
4.

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?

medium
A. map_data should be updated via the OccupancyGrid message, not a plain list
B. map_data is a tuple and cannot be changed
C. Index 2 is out of range for map_data
D. 100 is not a valid occupancy value

Solution

  1. Step 1: Check data type and mutability

    map_data is a list here, so assignment works in Python, no error from mutability.
  2. 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.
  3. Final Answer:

    map_data should be updated via the OccupancyGrid message, not a plain list -> Option A
  4. Quick Check:

    Update occupancy grid via ROS message, not local list [OK]
Hint: Update occupancy grid through ROS message, not local list [OK]
Common Mistakes:
  • Assuming direct list change updates ROS map
  • Thinking index 2 is invalid
  • Believing 100 is invalid occupancy value
5.

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] = -1
hard
A. Creates data with center 100, edges -1, others 0 in one list comprehension
B. Starts with all 0s, sets center 100, then sets edges to -1 in a loop
C. Starts with all -1s, sets center 100, then sets inner cells to 0 in a loop
D. Starts with zeros except center 100, then sets edges -1 in a loop

Solution

  1. Step 1: Understand the map layout requirements

    Center cell (index 12) = 100, edges = -1, others = 0 in a 5x5 grid (indexes 0-24).
  2. 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.
  3. Final Answer:

    Starts with all 0s, sets center 100, then sets edges to -1 in a loop -> Option B
  4. Quick Check:

    Initialize edges as -1, center 100, inner cells 0 [OK]
Hint: Initialize edges first, then set center and free cells [OK]
Common Mistakes:
  • Confusing edge indexes with inner cells
  • Overwriting center cell after setting edges
  • Using incorrect conditions for edges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard