Main Content


Create occupancy grid message


The OccupancyGrid object is an implementation of the nav_msgs/OccupancyGrid message type in ROS. The object contains meta-information about the message and the occupancy grid data.

To create a binaryOccupancyMap (Robotics System Toolbox) object from a ROS message, use the readBinaryOccupancyGrid function.

To create an occupancyMap (Navigation Toolbox) object , use the readOccupancyGrid function.




msg = rosmessage('nav_msgs/OccupancyGrid'); creates an empty OccupancyGrid object. To specify map information and data, use the map.Info and msg.Data properties. You can also get the occupancy grid messages off the ROS network using rossubscriber.


expand all

This property is read-only.

Message type of ROS message, returned as a character vector.

Data Types: char

This property is read-only.

ROS Header message, returned as a Header object. This header message contains the MessageType, sequence (Seq), timestamp (Stamp), and FrameId.

Information about the map, specified as a MapMetaData object. It contains the width, height, resolution, and origin of the map.

Map data, specified as a vector. The vector is all the occupancy data from each grid location in a single 1-D array.

Object Functions

readOccupancyGridRead occupancy grid message
readBinaryOccupancyGridRead binary occupancy grid
writeBinaryOccupancyGridWrite values from grid to ROS message
writeOccupancyGridWrite values from grid to ROS message


collapse all

Load two maps, simpleMap and complexMap, as logical matrices. Use whos to display the map.

load exampleMaps.mat
whos *Map*
  Name              Size               Bytes  Class      Attributes

  complexMap       41x52                2132  logical              
  emptyMap         26x27                 702  logical              
  simpleMap        26x27                 702  logical              
  ternaryMap      501x501            2008008  double               

Create a ROS message from simpleMap using a binaryOccupancyMap object. Write the OccupancyGrid message using writeBinaryOccupancyGrid.

bogMap = binaryOccupancyMap(double(simpleMap));
mapMsg = rosmessage('nav_msgs/OccupancyGrid');
mapMsg = 
  ROS OccupancyGrid message with properties:

    MessageType: 'nav_msgs/OccupancyGrid'
         Header: [1x1 Header]
           Info: [1x1 MapMetaData]
           Data: [702x1 int8]

  Use showdetails to show the contents of the message

Use readBinaryOccupancyGrid to convert the ROS message to a binaryOccupancyMap object. Use the object function show to display the map.

bogMap2 = readBinaryOccupancyGrid(mapMsg);

Figure contains an axes. The axes with title Binary Occupancy Grid contains an object of type image.

See Also



Introduced in R2019b