5
5
#include < pcl/point_types.h>
6
6
#include < pcl/filters/voxel_grid.h>
7
7
#include < pcl/filters/passthrough.h>
8
+ #include < boost/thread/mutex.hpp>
8
9
9
10
/* *
10
11
* Stores incoming point clouds in a map transforming
@@ -31,15 +32,16 @@ class PointCloudMapper
31
32
nh_priv_.param (" voxel_size" , voxel_size_, 0.1 );
32
33
nh_priv_.param (" filter_map" , filter_map_, false );
33
34
34
- cloud_sub_ = nh_.subscribe <PointCloud>(" input" , 15 , &PointCloudMapper::callback, this );
35
+ cloud_sub_ = nh_.subscribe <PointCloud>(" input" , 1 , &PointCloudMapper::callback, this );
35
36
bool latched = true ;
36
37
cloud_pub_ = nh_priv_.advertise <PointCloud>(" output" , 1 , latched);
37
38
pub_timer_ = nh_.createTimer (ros::Duration (10.0 ), &PointCloudMapper::publishCallback, this );
38
39
}
39
40
40
41
void callback (const PointCloud::ConstPtr& cloud)
41
42
{
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." );
43
45
PointCloud transformed_cloud;
44
46
ros::Time points_time;
45
47
points_time.fromNSec ((*cloud).header .stamp );
@@ -48,27 +50,33 @@ class PointCloudMapper
48
50
if (success)
49
51
{
50
52
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 ();
51
64
}
52
65
else
53
66
{
54
67
ROS_ERROR (" Could not transform point cloud to %s" , fixed_frame_.c_str ());
55
68
}
56
69
57
- if (filter_map_){
58
- PointCloud::Ptr cloud_downsampled = filter (accumulated_cloud_.makeShared ());
59
- accumulated_cloud_ = *cloud_downsampled;
60
- }
61
-
62
-
63
70
ROS_INFO_STREAM (" Map has " << accumulated_cloud_.points .size () << " points." );
71
+ m_.unlock ();
64
72
}
65
73
66
74
void publishCallback (const ros::TimerEvent&)
67
75
{
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 )
70
79
cloud_pub_.publish (accumulated_cloud_);
71
- }
72
80
}
73
81
74
82
PointCloud::Ptr filter (PointCloud::Ptr cloud)
@@ -127,14 +135,16 @@ class PointCloudMapper
127
135
ros::NodeHandle nh_;
128
136
ros::NodeHandle nh_priv_;
129
137
138
+ ros::Timer pub_timer_;
139
+
130
140
ros::Subscriber cloud_sub_;
131
141
ros::Publisher cloud_pub_;
132
142
133
- ros::Timer pub_timer_;
134
-
135
143
std::string fixed_frame_;
136
144
tf::TransformListener tf_listener_;
137
145
PointCloud accumulated_cloud_;
146
+ boost::mutex m_;
147
+ ros::WallTime last_pub_time_;
138
148
};
139
149
140
150
int main (int argc, char **argv)
0 commit comments