Main Content

Read Occupancy Grid

Read occupancy grid data from ROS 2 message structure

Since R2026a

  • ROS 2 Occupancy Grid Block Icon

Libraries:
ROS Toolbox / ROS 2

Description

The Read Occupancy Grid block extracts map data from a ROS 2 occupancy grid message. It accepts a nav_msgs/OccupancyGrid message at the Msg input port and outputs the grid as a matrix. Based on the selected output type, the Grid port returns a probability occupancy grid with values in the range [0 1] or a binary occupancy grid with cell values of 0 and 1.

The block can also output an ErrorCode that indicates the conversion status, including whether the message is truncated or the occupancy map is empty. Use block parameters to specify the maximum grid size, set replacement values for unknown cells, and define the threshold for binary conversion.

You can enable variable-size signals when the map dimensions change at run time. To configure grid size and message details automatically, use the Configure using ROS 2 option with an active topic on a ROS 2 network.

Examples

expand all

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.

% Read the PGM image
image = imread('playpen_map.pgm');

% Crop region of interest
imageCropped = image(750:1250, 750:1250);

% Normalize and convert to occupancy probability
imageNorm = double(imageCropped)/255;
imageOccupancy = 1 - imageNorm;

% Convert to occupancyMap object
map = occupancyMap(imageOccupancy, 20);  % 20 cells/meter

% Create ROS 2 OccupancyGrid message
msg = ros2message('nav_msgs/OccupancyGrid');
msg = rosWriteOccupancyGrid(msg, map);

% Set up ROS 2 node and publisher
node = ros2node('occupancy_grid_node');
pub = ros2publisher(node, '/occupancy_grid', 'nav_msgs/OccupancyGrid');

% Publish message continuously
disp("Publishing occupancy grid on /occupancy_grid...");
Publishing occupancy grid on /occupancy_grid...
while true
    send(pub, msg);
    pause(0.5);  % Publish at 2 Hz
end

Open Simulink Model

In a second MATLAB session, open the model that subscribes to the /occupancy_grid topic. The model includes these components.

open_system('listen_occupancy_grid.slx');

  • 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.

Setiing ROS 2 Read Occupancy Grid block parameter to Occupancy Grid

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.

sim('listen_occupancy_grid.slx');

ROS 2 Read Occupancy Grid Output

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.

Setting ROS 2 Read Occupancy Grid block parameter to Binary Occupancy Grid

To view the binary occupancy map, start the simulation in Simulink while the MATLAB script is running.

sim('listen_occupancy_grid.slx');

ROS 2 binary occupancy grid map

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.

Ports

Input

expand all

ROS 2 message of the nav_msgs/OccupancyGrid type, specified as a message structure. You can use the Subscribe block to get a message from an active ROS 2 network.

Data Types: bus

Output

expand all

Extracted grid data signal from a ROS 2 message of the nav_msgs/OccupancyGrid type, returned as an M-by-N matrix. Each cell in the output matrix represents whether an obstacle is present at that location.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16

Status code for grid data conversion, returned as a scalar. The error code values are:

  • 0 — Conversion of grid data is successful.

  • 1 — The dimensions of the grid data message exceed the limits specified in the Maximum Grid Size parameter.

  • 2 — The Data field of the incoming message is truncated. For information on increasing the maximum length of the array, see Manage Array Sizes for ROS Messages in Simulink.

  • 3 — The received map is empty.

Dependencies

To enable this parameter at the output port, select Show ErrorCode output port under Output settings.

Data Types: uint8

Parameters

expand all

To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.

Occupancy Grid Settings

Occupancy grid type, specified as one of the following options:

  • Occupancy grid — Each cell in the occupancy grid has a value in the range [0 , 1] representing the probability of the occupancy of that cell. Values close to 1 represent a high certainty that the cell contains an obstacle. Values close to 0 represent certainty that the cell is not occupied and obstacle free.

  • Binary occupancy grid — Each cell in the occupancy grid has a value, either 0 or 1. Values of 1 indicate the occupied workspace (obstacles) and values of 0 indicate the free workspace.

Select Configure using ROS 2 to set this parameter automatically using an active topic on a ROS 2 network. You must be connected to the ROS 2 network.

Maximum occupancy grid size, specified as a two-element [height width] vector.

Select Configure using ROS 2 to set this parameter automatically using an active topic on a ROS 2 network. You must be connected to the ROS 2 network.

Value to replace unknown message values, specified in the range of [0 1]. The block replaces unknown message values, specified as -1, with the specified value of the parameter.

Threshold for occupied values, specified as a scalar. Any value greater than or equal to the threshold is set to occupied, 1. All other values are set to unoccupied, 0.

Dependencies

To enable this parameter, select Binary occupancy grid in the Occupancy grid type parameter under Output settings.

Output settings

Select this parameter to enable ErrorCode at output port.

Select this parameter to output a variable-size signal. Use variable-size signals only if you expect the image size to change over time. For more information about variable-size signals, see Variable-Size Signal Basics (Simulink).

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Version History

Introduced in R2026a