import rclpy from rclpy.node import Node from sensor_msgs.msg import LaserScan class ScanSubscriber(Node): def __init__(self): super().__init__('scan_subscriber') self.subscription = self.create_subscription( LaserScan, 'scan', self.scan_callback, 10) self.subscription def scan_callback(self, msg): # process scan message pass def main(args=None): rclpy.init(args=args) scan_subscriber = ScanSubscriber() rclpy.spin(scan_subscriber) # Destroy the node explicitly # (optional - otherwise it will be done automatically # when the garbage collector destroys the node object) scan_subscriber.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()`
import rospy from sensor_msgs.msg import LaserScan rospy.init_node('laserscan_publisher') pub = rospy.Publisher('scan', LaserScan, queue_size=10) msg = LaserScan() msg.angle_min = -1.57 msg.angle_max = 1.57 msg.angle_increment = 0.1 msg.time_increment = 0.001 msg.scan_time = 0.05 msg.range_min = 0.0 msg.range_max = 10.0 msg.ranges = [1.0] * int((msg.angle_max - msg.angle_min) / msg.angle_increment) while not rospy.is_shutdown(): pub.publish(msg) rospy.spin()Package Library is roscpp This example sets up a ROS node that generates LaserScan messages and publishes them on the `scan` topic. The LaserScan message is initialized with various parameters and a loop continually publishes the message until `rospy.is_shutdown()` is set to true.