Skip to content

Commit 0250da4

Browse files
author
plnegre
committed
Fix ros timers to work when bagfile finishes
1 parent 3151d54 commit 0250da4

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

pointcloud_tools/src/pointcloud_mapper.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
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>
98

109
/**
1110
* Stores incoming point clouds in a map transforming
@@ -35,12 +34,11 @@ class PointCloudMapper
3534
cloud_sub_ = nh_.subscribe<PointCloud>("input", 1, &PointCloudMapper::callback, this);
3635
bool latched = true;
3736
cloud_pub_ = nh_priv_.advertise<PointCloud>("output", 1, latched);
38-
pub_timer_ = nh_.createTimer(ros::Duration(3.0), &PointCloudMapper::publishCallback, this);
37+
pub_timer_ = nh_.createWallTimer(ros::WallDuration(3.0), &PointCloudMapper::publishCallback, this);
3938
}
4039

4140
void callback(const PointCloud::ConstPtr& cloud)
4241
{
43-
m_.lock ();
4442
ROS_INFO_STREAM("Received cloud with " << cloud->points.size() << " points.");
4543
PointCloud transformed_cloud;
4644
ros::Time points_time;
@@ -68,10 +66,9 @@ class PointCloudMapper
6866
}
6967

7068
ROS_INFO_STREAM("Map has " << accumulated_cloud_.points.size() << " points.");
71-
m_.unlock();
7269
}
7370

74-
void publishCallback(const ros::TimerEvent&)
71+
void publishCallback(const ros::WallTimerEvent&)
7572
{
7673
// Publish the accumulated cloud if last publication was more than 5 seconds before.
7774
ros::WallDuration elapsed_time = ros::WallTime::now() - last_pub_time_;
@@ -135,15 +132,14 @@ class PointCloudMapper
135132
ros::NodeHandle nh_;
136133
ros::NodeHandle nh_priv_;
137134

138-
ros::Timer pub_timer_;
135+
ros::WallTimer pub_timer_;
139136

140137
ros::Subscriber cloud_sub_;
141138
ros::Publisher cloud_pub_;
142139

143140
std::string fixed_frame_;
144141
tf::TransformListener tf_listener_;
145142
PointCloud accumulated_cloud_;
146-
boost::mutex m_;
147143
ros::WallTime last_pub_time_;
148144
};
149145

pointcloud_tools/src/pointcloud_viewer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool viewer_initialized_;
6363
bool save_cloud_;
6464
std::string pcd_filename_;
6565
int counter_;
66-
ros::Timer save_timer_;
66+
ros::WallTimer save_timer_;
6767

6868
PointCloud cloud_xyz_;
6969
PointCloudRGB cloud_xyz_rgb_;
@@ -112,9 +112,9 @@ void updateVisualization()
112112

113113
if(cloud_old_ == cloud_)
114114
continue;
115-
m_.lock ();
116115

117116
// Convert to PointCloud<T>
117+
m_.lock ();
118118
if(pcl::getFieldIndex(*cloud_, "rgb") != -1)
119119
{
120120
rgb = true;
@@ -188,7 +188,7 @@ void updateVisualization()
188188
}
189189
}
190190

191-
void saveCallback(const ros::TimerEvent&)
191+
void saveCallback(const ros::WallTimerEvent&)
192192
{
193193
if (!save_cloud_) return;
194194

@@ -235,10 +235,8 @@ int main(int argc, char** argv)
235235
ROS_INFO("Subscribing to %s for PointCloud2 messages...", nh.resolveName ("input").c_str ());
236236

237237
signal(SIGINT, sigIntHandler);
238-
239238
boost::thread visualization_thread(&updateVisualization);
240-
241-
save_timer_ = nh.createTimer(ros::Duration(1), &saveCallback);
239+
save_timer_ = nh.createWallTimer(ros::WallDuration(1), &saveCallback);
242240

243241
// Spin
244242
ros::spin();

0 commit comments

Comments
 (0)