pointcloud_to_laserscan_nodelet.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010-2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  */
36 
37 /*
38  * Author: Paul Bovbel
39  */
40 
41 #include <limits>
44 #include <sensor_msgs/LaserScan.h>
46 #include <string>
48 
50 {
52 {
53 }
54 
56 {
57  boost::mutex::scoped_lock lock(connect_mutex_);
59 
60  private_nh_.param<std::string>("target_frame", target_frame_, "");
61  private_nh_.param<double>("transform_tolerance", tolerance_, 0.01);
62  private_nh_.param<double>("min_height", min_height_, std::numeric_limits<double>::min());
63  private_nh_.param<double>("max_height", max_height_, std::numeric_limits<double>::max());
64 
65  private_nh_.param<double>("angle_min", angle_min_, -M_PI);
66  private_nh_.param<double>("angle_max", angle_max_, M_PI);
67  private_nh_.param<double>("angle_increment", angle_increment_, M_PI / 180.0);
68  private_nh_.param<double>("scan_time", scan_time_, 1.0 / 30.0);
69  private_nh_.param<double>("range_min", range_min_, 0.0);
70  private_nh_.param<double>("range_max", range_max_, std::numeric_limits<double>::max());
71  private_nh_.param<double>("inf_epsilon", inf_epsilon_, 1.0);
72 
73  int concurrency_level;
74  private_nh_.param<int>("concurrency_level", concurrency_level, 1);
75  private_nh_.param<bool>("use_inf", use_inf_, true);
76 
77  // Check if explicitly single threaded, otherwise, let nodelet manager dictate thread pool size
78  if (concurrency_level == 1)
79  {
80  nh_ = getNodeHandle();
81  }
82  else
83  {
84  nh_ = getMTNodeHandle();
85  }
86 
87  // Only queue one pointcloud per running thread
88  if (concurrency_level > 0)
89  {
90  input_queue_size_ = concurrency_level;
91  }
92  else
93  {
94  input_queue_size_ = boost::thread::hardware_concurrency();
95  }
96 
97  // if pointcloud target frame specified, we need to filter by transform availability
98  if (!target_frame_.empty())
99  {
100  tf2_.reset(new tf2_ros::Buffer());
103  message_filter_->registerCallback(boost::bind(&PointCloudToLaserScanNodelet::cloudCb, this, _1));
104  message_filter_->registerFailureCallback(boost::bind(&PointCloudToLaserScanNodelet::failureCb, this, _1, _2));
105  }
106  else // otherwise setup direct subscription
107  {
109  }
110 
111  pub_ = nh_.advertise<sensor_msgs::LaserScan>("scan", 10, boost::bind(&PointCloudToLaserScanNodelet::connectCb, this),
112  boost::bind(&PointCloudToLaserScanNodelet::disconnectCb, this));
113 }
114 
116 {
117  boost::mutex::scoped_lock lock(connect_mutex_);
119  {
120  NODELET_INFO("Got a subscriber to scan, starting subscriber to pointcloud");
121  sub_.subscribe(nh_, "cloud_in", input_queue_size_);
122  }
123 }
124 
126 {
127  boost::mutex::scoped_lock lock(connect_mutex_);
128  if (pub_.getNumSubscribers() == 0)
129  {
130  NODELET_INFO("No subscibers to scan, shutting down subscriber to pointcloud");
131  sub_.unsubscribe();
132  }
133 }
134 
135 void PointCloudToLaserScanNodelet::failureCb(const sensor_msgs::PointCloud2ConstPtr& cloud_msg,
137 {
138  NODELET_WARN_STREAM_THROTTLE(1.0, "Can't transform pointcloud from frame " << cloud_msg->header.frame_id << " to "
139  << message_filter_->getTargetFramesString()
140  << " at time " << cloud_msg->header.stamp
141  << ", reason: " << reason);
142 }
143 
144 void PointCloudToLaserScanNodelet::cloudCb(const sensor_msgs::PointCloud2ConstPtr& cloud_msg)
145 {
146  // build laserscan output
147  sensor_msgs::LaserScan output;
148  output.header = cloud_msg->header;
149  if (!target_frame_.empty())
150  {
151  output.header.frame_id = target_frame_;
152  }
153 
154  output.angle_min = angle_min_;
155  output.angle_max = angle_max_;
156  output.angle_increment = angle_increment_;
157  output.time_increment = 0.0;
158  output.scan_time = scan_time_;
159  output.range_min = range_min_;
160  output.range_max = range_max_;
161 
162  // determine amount of rays to create
163  uint32_t ranges_size = std::ceil((output.angle_max - output.angle_min) / output.angle_increment);
164 
165  // determine if laserscan rays with no obstacle data will evaluate to infinity or max_range
166  if (use_inf_)
167  {
168  output.ranges.assign(ranges_size, std::numeric_limits<double>::infinity());
169  }
170  else
171  {
172  output.ranges.assign(ranges_size, output.range_max + inf_epsilon_);
173  }
174 
175  sensor_msgs::PointCloud2ConstPtr cloud_out;
176  sensor_msgs::PointCloud2Ptr cloud;
177 
178  // Transform cloud if necessary
179  if (!(output.header.frame_id == cloud_msg->header.frame_id))
180  {
181  try
182  {
183  cloud.reset(new sensor_msgs::PointCloud2);
184  tf2_->transform(*cloud_msg, *cloud, target_frame_, ros::Duration(tolerance_));
185  cloud_out = cloud;
186  }
187  catch (tf2::TransformException& ex)
188  {
189  NODELET_ERROR_STREAM("Transform failure: " << ex.what());
190  return;
191  }
192  }
193  else
194  {
195  cloud_out = cloud_msg;
196  }
197 
198  // Iterate through pointcloud
199  for (sensor_msgs::PointCloud2ConstIterator<float> iter_x(*cloud_out, "x"), iter_y(*cloud_out, "y"),
200  iter_z(*cloud_out, "z");
201  iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z)
202  {
203  if (std::isnan(*iter_x) || std::isnan(*iter_y) || std::isnan(*iter_z))
204  {
205  NODELET_DEBUG("rejected for nan in point(%f, %f, %f)\n", *iter_x, *iter_y, *iter_z);
206  continue;
207  }
208 
209  if (*iter_z > max_height_ || *iter_z < min_height_)
210  {
211  NODELET_DEBUG("rejected for height %f not in range (%f, %f)\n", *iter_z, min_height_, max_height_);
212  continue;
213  }
214 
215  double range = hypot(*iter_x, *iter_y);
216  if (range < range_min_)
217  {
218  NODELET_DEBUG("rejected for range %f below minimum value %f. Point: (%f, %f, %f)", range, range_min_, *iter_x,
219  *iter_y, *iter_z);
220  continue;
221  }
222  if (range > range_max_)
223  {
224  NODELET_DEBUG("rejected for range %f above maximum value %f. Point: (%f, %f, %f)", range, range_max_, *iter_x,
225  *iter_y, *iter_z);
226  continue;
227  }
228 
229  double angle = atan2(*iter_y, *iter_x);
230  if (angle < output.angle_min || angle > output.angle_max)
231  {
232  NODELET_DEBUG("rejected for angle %f not in range (%f, %f)\n", angle, output.angle_min, output.angle_max);
233  continue;
234  }
235 
236  // overwrite range at laserscan ray if new range is smaller
237  int index = (angle - output.angle_min) / output.angle_increment;
238  if (range < output.ranges[index])
239  {
240  output.ranges[index] = range;
241  }
242  }
243  pub_.publish(output);
244 }
245 } // namespace pointcloud_to_laserscan
246 
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::message_filter_
boost::shared_ptr< MessageFilter > message_filter_
Definition: pointcloud_to_laserscan_nodelet.h:84
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::PointCloudToLaserScanNodelet
PointCloudToLaserScanNodelet()
Definition: pointcloud_to_laserscan_nodelet.cpp:51
NODELET_WARN_STREAM_THROTTLE
#define NODELET_WARN_STREAM_THROTTLE(rate,...)
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::nh_
ros::NodeHandle nh_
Definition: pointcloud_to_laserscan_nodelet.h:77
point_cloud2_iterator.h
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::inf_epsilon_
double inf_epsilon_
Definition: pointcloud_to_laserscan_nodelet.h:92
nodelet::Nodelet::getNodeHandle
ros::NodeHandle & getNodeHandle() const
angle
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
PointCloud2IteratorBase< T, const T, const unsigned char, const sensor_msgs::PointCloud2, PointCloud2ConstIterator >::end
PointCloud2ConstIterator< T > end() const
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::tf2_listener_
boost::shared_ptr< tf2_ros::TransformListener > tf2_listener_
Definition: pointcloud_to_laserscan_nodelet.h:82
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::min_height_
double min_height_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::scan_time_
double scan_time_
Definition: pointcloud_to_laserscan_nodelet.h:90
tf2_ros::filter_failure_reasons::FilterFailureReason
FilterFailureReason
tf2_sensor_msgs.h
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::max_height_
double max_height_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan
Definition: laserscan_to_pointcloud_nodelet.h:55
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::input_queue_size_
unsigned int input_queue_size_
Definition: pointcloud_to_laserscan_nodelet.h:87
nodelet::Nodelet::getPrivateNodeHandle
ros::NodeHandle & getPrivateNodeHandle() const
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber::unsubscribe
void unsubscribe()
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
sensor_msgs::PointCloud2ConstIterator
ros::Subscriber::getNumPublishers
uint32_t getNumPublishers() const
tf2_ros::TransformListener
class_list_macros.h
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::angle_max_
double angle_max_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::range_min_
double range_min_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::private_nh_
ros::NodeHandle private_nh_
Definition: pointcloud_to_laserscan_nodelet.h:77
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
NODELET_ERROR_STREAM
#define NODELET_ERROR_STREAM(...)
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::pub_
ros::Publisher pub_
Definition: pointcloud_to_laserscan_nodelet.h:78
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::angle_increment_
double angle_increment_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::tf2_
boost::shared_ptr< tf2_ros::Buffer > tf2_
Definition: pointcloud_to_laserscan_nodelet.h:81
pointcloud_to_laserscan_nodelet.h
tf2_ros::Buffer
message_filters::SimpleFilter::registerCallback
Connection registerCallback(const boost::function< void(P)> &callback)
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::connect_mutex_
boost::mutex connect_mutex_
Definition: pointcloud_to_laserscan_nodelet.h:79
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::onInit
virtual void onInit()
Definition: pointcloud_to_laserscan_nodelet.cpp:55
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::connectCb
void connectCb()
Definition: pointcloud_to_laserscan_nodelet.cpp:115
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::cloudCb
void cloudCb(const sensor_msgs::PointCloud2ConstPtr &cloud_msg)
Definition: pointcloud_to_laserscan_nodelet.cpp:144
message_filters::Subscriber::subscribe
void subscribe()
pointcloud_to_laserscan::PointCloudToLaserScanNodelet
Definition: pointcloud_to_laserscan_nodelet.h:61
NODELET_INFO
#define NODELET_INFO(...)
nodelet::Nodelet
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::failureCb
void failureCb(const sensor_msgs::PointCloud2ConstPtr &cloud_msg, tf2_ros::filter_failure_reasons::FilterFailureReason reason)
Definition: pointcloud_to_laserscan_nodelet.cpp:135
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::disconnectCb
void disconnectCb()
Definition: pointcloud_to_laserscan_nodelet.cpp:125
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::sub_
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_
Definition: pointcloud_to_laserscan_nodelet.h:83
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::target_frame_
std::string target_frame_
Definition: pointcloud_to_laserscan_nodelet.h:88
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::range_max_
double range_max_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::use_inf_
bool use_inf_
Definition: pointcloud_to_laserscan_nodelet.h:91
ros::Publisher::getNumSubscribers
uint32_t getNumSubscribers() const
message_filters::Subscriber::getSubscriber
const ros::Subscriber & getSubscriber() const
tf2::TransformException
ros::Duration
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::angle_min_
double angle_min_
Definition: pointcloud_to_laserscan_nodelet.h:90
pointcloud_to_laserscan::MessageFilter
tf2_ros::MessageFilter< sensor_msgs::LaserScan > MessageFilter
Definition: laserscan_to_pointcloud_nodelet.h:57
pointcloud_to_laserscan::PointCloudToLaserScanNodelet::tolerance_
double tolerance_
Definition: pointcloud_to_laserscan_nodelet.h:89
nodelet::Nodelet::getMTNodeHandle
ros::NodeHandle & getMTNodeHandle() const
NODELET_DEBUG
#define NODELET_DEBUG(...)


pointcloud_to_laserscan
Author(s): Paul Bovbel , Tully Foote
autogenerated on Wed Mar 2 2022 00:44:25