import rospy from nav_msgs.msg import OccupancyGrid # Create a new OccupancyGrid message map_msg = OccupancyGrid() # Initialize the message fields map_msg.header.stamp = rospy.Time.now() map_msg.header.frame_id = 'map' map_msg.info.resolution = 0.05 # meters per cell map_msg.info.width = 100 # width in cells map_msg.info.height = 100 # height in cells map_msg.info.origin.position.x = -2.5 map_msg.info.origin.position.y = -2.5 map_msg.info.origin.orientation.w = 1.0 map_msg.data = [0] * (map_msg.info.width * map_msg.info.height)
from math import sqrt # Set occupancy probabilities for some cells for i in range(map_msg.info.width): for j in range(map_msg.info.height): dist = sqrt((i - map_msg.info.width / 2) ** 2 + (j - map_msg.info.height / 2) ** 2) if dist < 20: map_msg.data[j * map_msg.info.width + i] = 100 # Publish the updated OccupancyGrid message publisher.publish(map_msg)This example shows how to set the occupancy probabilities for some cells in the OccupancyGrid message data field based on their distance from the center of the map. The updated message is then published on a ROS topic. Package library: The nav_msgs.msg OccupancyGrid message type is part of the nav_msgs package in ROS. The library for working with this message type in Python is rospy.