|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import rospy |
| 5 | +import tf2_ros |
| 6 | +import tf2_sensor_msgs |
| 7 | +from sensor_msgs.msg import PointCloud2 |
| 8 | + |
| 9 | +class LidarDataTransformer: |
| 10 | + def __init__(self): |
| 11 | + # Initialize the ROS node |
| 12 | + rospy.init_node('pub_pcl_world', anonymous=True) |
| 13 | + |
| 14 | + # Set up the transform listener |
| 15 | + self.tf_buffer = tf2_ros.Buffer() |
| 16 | + self.tf_listener = tf2_ros.TransformListener(self.tf_buffer) |
| 17 | + |
| 18 | + # Subscribe to the PointCloud2 topic |
| 19 | + self.pc_subscriber = rospy.Subscriber('/velodyne_points', PointCloud2, self.point_cloud_callback) |
| 20 | + |
| 21 | + # Publisher for transformed point cloud data |
| 22 | + self.pc_publisher = rospy.Publisher('/velodyne_points_world', PointCloud2, queue_size=10) |
| 23 | + |
| 24 | + def point_cloud_callback(self, msg): |
| 25 | + try: |
| 26 | + # Look up the transform from 'world' to 'lidar_link' |
| 27 | + transform = self.tf_buffer.lookup_transform('world', msg.header.frame_id, rospy.Time(0), rospy.Duration(1.0)) |
| 28 | + |
| 29 | + # Transform the point cloud to the 'world' frame |
| 30 | + transformed_cloud = tf2_sensor_msgs.do_transform_cloud(msg, transform) |
| 31 | + |
| 32 | + # Publish the transformed point cloud |
| 33 | + self.pc_publisher.publish(transformed_cloud) |
| 34 | + |
| 35 | + except tf2_ros.LookupException as e: |
| 36 | + rospy.logerr(f"Transform lookup failed: {e}") |
| 37 | + except tf2_ros.ExtrapolationException as e: |
| 38 | + rospy.logerr(f"Transform extrapolation failed: {e}") |
| 39 | + except Exception as e: |
| 40 | + rospy.logerr(f"Error transforming point cloud: {e}") |
| 41 | + |
| 42 | + def run(self): |
| 43 | + rospy.spin() |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + try: |
| 47 | + transformer = LidarDataTransformer() |
| 48 | + transformer.run() |
| 49 | + except rospy.ROSInterruptException: |
| 50 | + pass |
0 commit comments