import rospy from sensor_msgs.msg import LaserScan def callback(data): # print the maximum angle of the laser scan print("Angle Max: ", data.angle_max) rospy.init_node('laser_scan_subscriber') rospy.Subscriber("/laser_scan", LaserScan, callback) rospy.spin() # keeps the node running until shutdown
import rospy from sensor_msgs.msg import LaserScan def callback(data): # print the original and modified maximum angles print("Original Angle Max: ", data.angle_max) # modify the maximum angle to 180 degrees data.angle_max = 3.14 # radians print("Modified Angle Max: ", data.angle_max) rospy.init_node('laser_scan_subscriber') rospy.Subscriber("/laser_scan", LaserScan, callback) # create a publisher to publish the modified LaserScan message pub = rospy.Publisher('/modified_scan', LaserScan, queue_size=10) # set the publishing rate rate = rospy.Rate(10) # 10hz while not rospy.is_shutdown(): # publish the modified LaserScan message pub.publish(data) rate.sleep()In this example, we are subscribing to a topic called "/laser_scan" that publishes LaserScan messages. When a new message is received, the callback function is called, which modifies the maximum angle of the laser scan to 180 degrees (3.14 radians). We then create a publisher to publish the modified LaserScan message to a new topic called "/modified_scan". The message is published at a rate of 10hz. Package Library: the sensor_msgs.msg LaserScan is part of the rosmsg package library in ROS.