|
14 | 14 | #include <sb_utils.h>
|
15 | 15 |
|
16 | 16 | ros::Publisher pub;
|
17 |
| -std::string output_frame; |
18 | 17 | tf2_ros::Buffer tf_buffer;
|
19 | 18 |
|
| 19 | +std::string output_frame; |
| 20 | +ros::Time last_exception_time; |
| 21 | + |
| 22 | +// The amount of time(s) for a new pointcloud transform to be published. |
| 23 | +double transform_period; |
| 24 | + |
20 | 25 | void pointCloudCallback(const sensor_msgs::PointCloud2::ConstPtr& msg) {
|
21 | 26 | try {
|
22 | 27 | // Create an empty pointcloud
|
23 | 28 | sensor_msgs::PointCloud2 output;
|
24 | 29 | // Transform the pointcloud to the requested frame
|
25 | 30 | geometry_msgs::TransformStamped tf_stamped = tf_buffer.lookupTransform(
|
26 |
| - msg->header.frame_id, output_frame, ros::Time(0), ros::Duration(1.0)); |
| 31 | + msg->header.frame_id, output_frame, ros::Time(0), ros::Duration(0.1)); |
| 32 | + |
27 | 33 | tf2::doTransform(*msg, output, tf_stamped);
|
28 | 34 |
|
| 35 | + output.header.frame_id = output_frame; |
| 36 | + |
29 | 37 | // Publish the transformed pointcloud
|
30 | 38 | pub.publish(output);
|
31 |
| - } catch (tf2::TransformException ex){ |
32 |
| - ROS_WARN("%s", ex.what()); |
33 |
| - ros::Duration(1.0).sleep(); |
| 39 | + |
| 40 | + ros::Duration(transform_period).sleep(); |
| 41 | + } catch (tf2::TransformException ex) { |
| 42 | + if (ros::Time::now().toSec() - last_exception_time.toSec() > 1.0) |
| 43 | + ROS_WARN("%s", ex.what()); |
| 44 | + |
| 45 | + last_exception_time = ros::Time::now(); |
34 | 46 | }
|
35 | 47 | }
|
36 | 48 |
|
37 |
| -int main(int argc, char** argv){ |
| 49 | +int main(int argc, char** argv) { |
38 | 50 | ros::init(argc, argv, "pointcloud_transformer");
|
39 | 51 | ros::NodeHandle private_nh("~");
|
40 |
| - if (!SB_getParam(private_nh, "output_frame", output_frame)){ |
| 52 | + if (!SB_getParam(private_nh, "output_frame", output_frame)) { |
41 | 53 | // Error and exit if we didn't get a frame to transform to.
|
42 |
| - // We need this to transform anything, and there is no reasonable default |
43 |
| - ROS_ERROR("Param 'output_frame' not provided. " \ |
44 |
| - "Can't transform anything without a frame to transform it to"); |
| 54 | + // We need this to transform anything, and there is no reasonable |
| 55 | + // default |
| 56 | + ROS_ERROR( |
| 57 | + "Param 'output_frame' not provided. " |
| 58 | + "Can't transform anything without a frame to transform it to"); |
45 | 59 | return 1;
|
46 | 60 | }
|
47 |
| - ros::Subscriber sub = private_nh.subscribe("/input_pointcloud", 1, pointCloudCallback); |
48 |
| - pub = private_nh.advertise<sensor_msgs::PointCloud2>("/output_pointcloud", 1); |
| 61 | + |
| 62 | + double default_transform_period = 0; |
| 63 | + SB_getParam( |
| 64 | + private_nh, "transform_period", transform_period, default_transform_period); |
| 65 | + |
| 66 | + ros::Subscriber sub = |
| 67 | + private_nh.subscribe("/input_pointcloud", 1, pointCloudCallback); |
| 68 | + pub = |
| 69 | + private_nh.advertise<sensor_msgs::PointCloud2>("/output_pointcloud", 1); |
49 | 70 | tf2_ros::TransformListener tfListener(tf_buffer);
|
50 | 71 | ros::spin();
|
51 | 72 | return 0;
|
|
0 commit comments