Skip to content

Commit 82f7505

Browse files
committed
fixed pointcloud mapper
1 parent 06b3ae9 commit 82f7505

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

pointcloud_tools/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 2.8.3)
22
project(pointcloud_tools)
33

4-
find_package(catkin REQUIRED COMPONENTS roscpp pcl_ros std_msgs sensor_msgs nav_msgs)
4+
find_package(catkin REQUIRED COMPONENTS roscpp pcl_ros std_msgs sensor_msgs nav_msgs pcl_conversions)
55
find_package(PCL REQUIRED)
66
find_package(VTK REQUIRED)
77

pointcloud_tools/package.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
<build_depend>sensor_msgs</build_depend>
1919
<build_depend>nav_msgs</build_depend>
2020
<build_depend>libvtk</build_depend>
21+
<build_depend>pcl_conversions</build_depend>
2122
<run_depend>roscpp</run_depend>
2223
<run_depend>pcl_ros</run_depend>
2324
<run_depend>std_msgs</run_depend>
2425
<run_depend>sensor_msgs</run_depend>
2526
<run_depend>nav_msgs</run_depend>
2627
<run_depend>libvtk</run_depend>
27-
28+
<run_depend>pcl_conversions</run_depend>
2829
<export>
2930

3031
</export>

pointcloud_tools/src/pointcloud_mapper.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,38 @@ class PointCloudMapper
3131
nh_priv_.param("voxel_size", voxel_size_, 0.1);
3232
nh_priv_.param("filter_map", filter_map_, false);
3333

34-
cloud_sub_ = nh_.subscribe<PointCloud>("input", 1, &PointCloudMapper::callback, this);
35-
cloud_pub_ = nh_priv_.advertise<PointCloud>("output", 1, true);
34+
cloud_sub_ = nh_.subscribe<sensor_msgs::PointCloud2>("input", 1, &PointCloudMapper::callback, this);
35+
cloud_pub_ = nh_priv_.advertise<sensor_msgs::PointCloud2>("output", 1, true);
36+
37+
accumulated_cloud_.header.frame_id = fixed_frame_;
38+
39+
ROS_INFO_STREAM("[PointCloudMapper params:\n" <<
40+
"\t* Fixed frame: " << fixed_frame_ << "\n" <<
41+
"\t* Filter map: " << filter_map_ << "\n" <<
42+
"\t* Filter bounds: " << "( " << x_filter_min_ << ", " << y_filter_min_ << ", " << z_filter_min_ << ") and ( " << x_filter_max_ << ", " << y_filter_max_ << ", " << z_filter_max_ << ")" << "\n" <<
43+
"\t* Voxel size: " << voxel_size_);
44+
3645
pub_timer_ = nh_.createWallTimer(ros::WallDuration(3.0), &PointCloudMapper::publishCallback, this);
3746
}
3847

39-
void callback(const PointCloud::ConstPtr& cloud)
48+
void callback(const sensor_msgs::PointCloud2ConstPtr& cloud_msg)
4049
{
41-
ROS_INFO_STREAM("Received cloud with " << cloud->points.size() << " points.");
50+
PointCloud cloud;
51+
pcl::fromROSMsg(*cloud_msg, cloud);
52+
ROS_INFO_STREAM("Received cloud with " << cloud.points.size() << " points.");
53+
4254
PointCloud transformed_cloud;
43-
ros::Time points_time;
44-
points_time.fromNSec(cloud->header.stamp);
45-
tf_listener_.waitForTransform(fixed_frame_, cloud->header.frame_id, points_time, ros::Duration(5.0));
46-
bool success = pcl_ros::transformPointCloud(fixed_frame_, *cloud, transformed_cloud, tf_listener_);
55+
tf::StampedTransform transform;
56+
try {
57+
tf_listener_.waitForTransform(fixed_frame_, cloud_msg->header.frame_id, cloud_msg->header.stamp, ros::Duration(5.0));
58+
tf_listener_.lookupTransform(fixed_frame_, cloud_msg->header.frame_id, cloud_msg->header.stamp, transform);
59+
} catch (tf::TransformException ex){
60+
ROS_ERROR("%s",ex.what());
61+
ros::Duration(1.0).sleep();
62+
}
63+
64+
pcl_ros::transformPointCloud(cloud, transformed_cloud, tf::Transform(transform));
65+
bool success = true;
4766
if (success)
4867
{
4968
accumulated_cloud_ += transformed_cloud;
@@ -71,8 +90,12 @@ class PointCloudMapper
7190
{
7291
// Publish the accumulated cloud if last publication was more than 5 seconds before.
7392
ros::WallDuration elapsed_time = ros::WallTime::now() - last_pub_time_;
74-
if (cloud_pub_.getNumSubscribers() > 0 && elapsed_time.toSec() > 5.0)
75-
cloud_pub_.publish(accumulated_cloud_);
93+
if (cloud_pub_.getNumSubscribers() > 0 && elapsed_time.toSec() > 5.0) {
94+
95+
sensor_msgs::PointCloud2 cloud_msg;
96+
pcl::toROSMsg(accumulated_cloud_, cloud_msg);
97+
cloud_pub_.publish(cloud_msg);
98+
}
7699
}
77100

78101
PointCloud::Ptr filter(PointCloud::Ptr cloud)

0 commit comments

Comments
 (0)