Skip to content

Commit df08400

Browse files
author
plnegre
committed
Publish the pointcloud at the same rate
1 parent f878fd5 commit df08400

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

pointcloud_tools/src/pointcloud_mapper.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <pcl/point_types.h>
66
#include <pcl/filters/voxel_grid.h>
77
#include <pcl/filters/passthrough.h>
8+
#include <boost/thread/mutex.hpp>
89

910
/**
1011
* Stores incoming point clouds in a map transforming
@@ -31,15 +32,16 @@ class PointCloudMapper
3132
nh_priv_.param("voxel_size", voxel_size_, 0.1);
3233
nh_priv_.param("filter_map", filter_map_, false);
3334

34-
cloud_sub_ = nh_.subscribe<PointCloud>("input", 15, &PointCloudMapper::callback, this);
35+
cloud_sub_ = nh_.subscribe<PointCloud>("input", 1, &PointCloudMapper::callback, this);
3536
bool latched = true;
3637
cloud_pub_ = nh_priv_.advertise<PointCloud>("output", 1, latched);
3738
pub_timer_ = nh_.createTimer(ros::Duration(10.0), &PointCloudMapper::publishCallback, this);
3839
}
3940

4041
void callback(const PointCloud::ConstPtr& cloud)
4142
{
42-
ROS_INFO_STREAM("received cloud with " << cloud->points.size() << " points.");
43+
m_.lock ();
44+
ROS_INFO_STREAM("Received cloud with " << cloud->points.size() << " points.");
4345
PointCloud transformed_cloud;
4446
ros::Time points_time;
4547
points_time.fromNSec((*cloud).header.stamp);
@@ -48,27 +50,33 @@ class PointCloudMapper
4850
if (success)
4951
{
5052
accumulated_cloud_ += transformed_cloud;
53+
54+
if(filter_map_)
55+
{
56+
PointCloud::Ptr cloud_downsampled = filter(accumulated_cloud_.makeShared());
57+
accumulated_cloud_ = *cloud_downsampled;
58+
}
59+
60+
// Publish the cloud
61+
if (cloud_pub_.getNumSubscribers() > 0)
62+
cloud_pub_.publish(accumulated_cloud_);
63+
last_pub_time_ = ros::WallTime::now();
5164
}
5265
else
5366
{
5467
ROS_ERROR("Could not transform point cloud to %s", fixed_frame_.c_str());
5568
}
5669

57-
if(filter_map_){
58-
PointCloud::Ptr cloud_downsampled = filter(accumulated_cloud_.makeShared());
59-
accumulated_cloud_ = *cloud_downsampled;
60-
}
61-
62-
6370
ROS_INFO_STREAM("Map has " << accumulated_cloud_.points.size() << " points.");
71+
m_.unlock();
6472
}
6573

6674
void publishCallback(const ros::TimerEvent&)
6775
{
68-
if (cloud_pub_.getNumSubscribers() > 0)
69-
{
76+
// Publish the accumulated cloud if last publication was more than 10 seconds before.
77+
ros::WallDuration elapsed_time = ros::WallTime::now() - last_pub_time_;
78+
if (cloud_pub_.getNumSubscribers() > 0 && elapsed_time.toSec() > 10.0)
7079
cloud_pub_.publish(accumulated_cloud_);
71-
}
7280
}
7381

7482
PointCloud::Ptr filter(PointCloud::Ptr cloud)
@@ -127,14 +135,16 @@ class PointCloudMapper
127135
ros::NodeHandle nh_;
128136
ros::NodeHandle nh_priv_;
129137

138+
ros::Timer pub_timer_;
139+
130140
ros::Subscriber cloud_sub_;
131141
ros::Publisher cloud_pub_;
132142

133-
ros::Timer pub_timer_;
134-
135143
std::string fixed_frame_;
136144
tf::TransformListener tf_listener_;
137145
PointCloud accumulated_cloud_;
146+
boost::mutex m_;
147+
ros::WallTime last_pub_time_;
138148
};
139149

140150
int main(int argc, char **argv)

pointcloud_tools/src/pointcloud_viewer.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
#include <pcl/visualization/pcl_visualizer.h>
4949
#include <pcl/features/feature.h>
5050
#include <pcl/io/pcd_io.h>
51-
#include <pcl/filters/voxel_grid.h>
52-
#include <pcl/filters/passthrough.h>
5351

5452
using pcl::visualization::PointCloudColorHandlerGenericField;
5553

@@ -60,38 +58,18 @@ typedef pcl::PointCloud<PointRGB> PointCloudRGB;
6058

6159
// Global data
6260
sensor_msgs::PointCloud2ConstPtr cloud_, cloud_old_;
63-
boost::mutex m;
61+
boost::mutex m_;
6462
bool viewer_initialized_;
6563
bool save_cloud_;
6664
std::string pcd_filename_;
6765
int counter_;
6866

6967
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr& cloud)
7068
{
71-
m.lock ();
72-
printf("\rPointCloud with %d data points (%s), stamp %f, and frame %s.",
73-
cloud->width * cloud->height, pcl::getFieldsList(*cloud).c_str(),
74-
cloud->header.stamp.toSec(), cloud->header.frame_id.c_str());
69+
m_.lock ();
70+
ROS_INFO_STREAM("Pointcloud viewer has received a pointcloud.");
7571
cloud_ = cloud;
76-
m.unlock();
77-
}
78-
79-
PointCloudRGB::Ptr filter(PointCloudRGB::Ptr cloud, double voxel_size)
80-
{
81-
PointCloudRGB::Ptr cloud_filtered_ptr(new PointCloudRGB);
82-
83-
// Downsampling using voxel grid
84-
pcl::VoxelGrid<PointRGB> grid_;
85-
PointCloudRGB::Ptr cloud_downsampled_ptr(new PointCloudRGB);
86-
87-
grid_.setLeafSize(voxel_size,
88-
voxel_size,
89-
voxel_size);
90-
grid_.setDownsampleAllData(true);
91-
grid_.setInputCloud(cloud);
92-
grid_.filter(*cloud_downsampled_ptr);
93-
94-
return cloud_downsampled_ptr;
72+
m_.unlock();
9573
}
9674

9775
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent& event, void* nothing)
@@ -111,7 +89,6 @@ void updateVisualization()
11189

11290
ros::WallDuration d(0.01);
11391
bool rgb = false;
114-
//std::vector<sensor_msgs::PointField> fields;
11592
std::vector<pcl::PCLPointField> fields;
11693

11794
// Create the visualizer
@@ -133,7 +110,7 @@ void updateVisualization()
133110

134111
if(cloud_old_ == cloud_)
135112
continue;
136-
m.lock ();
113+
m_.lock ();
137114

138115
// Convert to PointCloud<T>
139116
if(pcl::getFieldIndex(*cloud_, "rgb") != -1)
@@ -148,7 +125,7 @@ void updateVisualization()
148125
pcl::getFields(cloud_xyz, fields);
149126
}
150127
cloud_old_ = cloud_;
151-
m.unlock();
128+
m_.unlock();
152129

153130
// Delete the previous point cloud
154131
viewer.removePointCloud("cloud");

0 commit comments

Comments
 (0)