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());
102  message_filter_.reset(new MessageFilter(sub_, *tf2_, target_frame_, input_queue_size_, nh_));
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 
#define NODELET_WARN_STREAM_THROTTLE(rate,...)
ros::NodeHandle & getNodeHandle() const
uint32_t getNumPublishers() const
INLINE Rall1d< T, V, S > hypot(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
ros::NodeHandle & getPrivateNodeHandle() const
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
void failureCb(const sensor_msgs::PointCloud2ConstPtr &cloud_msg, tf2_ros::filter_failure_reasons::FilterFailureReason reason)
#define NODELET_ERROR_STREAM(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void publish(const boost::shared_ptr< M > &message) const
const ros::Subscriber & getSubscriber() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle & getMTNodeHandle() const
#define NODELET_INFO(...)
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
void cloudCb(const sensor_msgs::PointCloud2ConstPtr &cloud_msg)
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
boost::shared_ptr< tf2_ros::TransformListener > tf2_listener_
uint32_t getNumSubscribers() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define NODELET_DEBUG(...)
tf2_ros::MessageFilter< sensor_msgs::LaserScan > MessageFilter
Connection registerCallback(const C &callback)


pointcloud_to_laserscan
Author(s): Paul Bovbel , Tully Foote
autogenerated on Mon Feb 28 2022 23:13:15