This example shows how to visualize a static occupancy grid in Simulink® by subscribing to ROS 2 messages published on the /occupancy_grid topic. It reads a .pgm image file, converts it to an occupancy grid, and sends the map as a nav_msgs/OccupancyGrid message to the Simulink model. It then uses the Read Occupancy Grid block to decode the message and display it using a Video Viewer block.
To run this example, you must use two MATLAB sessions, one for publishing the ROS 2 message and one for running the model.
Create and Publish Occupancy Grid from Image
Run this code in the first MATLAB session to read a grayscale image, convert it to an occupancyMap object, and publish the message on the /occupancy_grid topic.
Publishing occupancy grid on /occupancy_grid...
Open Simulink Model
In a second MATLAB session, open the model that subscribes to the /occupancy_grid topic. The model includes these components.
Subscribe — Subscribes to /occupancy_grid and outputs the message and update flag.
New Message Indicator — Indicates when a new message is received.
Read Occupancy Grid — Converts the message into a 2-D matrix representing the occupancy grid.
Video Viewer — Displays the occupancy grid visually as an image.
Status Indicator — Displays error codes from the Read Occupancy Grid block.
In the Read Occupancy Grid block, set the Occupancy grid type parameter to the default value of Occupancy grid, which decodes messages created using the rosWriteOccupancyGrid function. The block outputs a matrix with values in the range [0, 1], where each element represents the occupancy probability of a map cell. The block also outputs an ErrorCode signal. An error code of 0 indicates successful conversion, while other values correspond to issues such as truncated data or oversized maps.
Run Simulink Model to Visualize Map
Start the simulation in Simulink while the MATLAB script is running. When the simulation runs:
The subscriber receives new occupancy grid messages.
The grid is decoded and rendered in the Video Viewer block.
The New Message Indicator flashes with every incoming message.
The Status Indicator shows the conversion status based on the error code.
You can see the cropped occupancy map appearing in the Video Viewer in near real time, updated continuously at 2 Hz.
Use Binary Occupancy Grid for Logical Maps
For a logical representation of free and occupied cells, you can configure the Read Occupancy Grid block to output a binary map.
To do so, set the Occupancy grid type parameter to Binary occupancy grid and Threshold to a value that separates occupied and free cells. In this mode, the block compares occupancy values against the threshold and outputs 1 for occupied cells and 0 for free cells. This example sets the Threshold value to 50.
To view the binary occupancy map, start the simulation in Simulink while the MATLAB script is running.
You can use the binary occupancy grid mode when the application requires a simplified logical map with only free and occupied cells. This mode is suited for tasks such as obstacle detection, path planning, or any downstream logic that operates on binary masks. It is also useful on memory-constrained systems and when you decode messages published using the rosWriteBinaryOccupancyGrid function.