laserscan_multi_merger.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <string.h>
4 #include <pcl_ros/transforms.h>
6 #include <pcl/conversions.h>
7 #include <pcl/point_cloud.h>
8 #include <pcl/point_types.h>
9 #include <pcl/io/pcd_io.h>
10 #include <sensor_msgs/PointCloud.h>
11 #include <sensor_msgs/PointCloud2.h>
13 #include "sensor_msgs/LaserScan.h"
14 #include "pcl_ros/point_cloud.h"
15 #include <Eigen/Dense>
16 #include <dynamic_reconfigure/server.h>
17 #include <ira_laser_tools/laserscan_multi_mergerConfig.h>
18 
19 using namespace std;
20 using namespace pcl;
21 using namespace laserscan_multi_merger;
22 
24 {
25 public:
27  void scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan, std::string topic);
28  void pointcloud_to_laserscan(Eigen::MatrixXf points, pcl::PCLPointCloud2 *merged_cloud);
29  void reconfigureCallback(laserscan_multi_mergerConfig &config, uint32_t level);
30 
31 private:
35 
38  vector<ros::Subscriber> scan_subscribers;
39  vector<bool> clouds_modified;
40 
41  vector<pcl::PCLPointCloud2> clouds;
42  vector<string> input_topics;
43 
44  void laserscan_topic_parser();
45 
46  double angle_min;
47  double angle_max;
50  double scan_time;
51  double range_min;
52  double range_max;
53 
58 };
59 
60 void LaserscanMerger::reconfigureCallback(laserscan_multi_mergerConfig &config, uint32_t level)
61 {
62  this->angle_min = config.angle_min;
63  this->angle_max = config.angle_max;
64  this->angle_increment = config.angle_increment;
65  this->time_increment = config.time_increment;
66  this->scan_time = config.scan_time;
67  this->range_min = config.range_min;
68  this->range_max = config.range_max;
69 }
70 
72 {
73  // LaserScan topics to subscribe
75  ros::master::getTopics(topics);
76 
77  istringstream iss(laserscan_topics);
78  vector<string> tokens;
79  copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(tokens));
80 
81  vector<string> tmp_input_topics;
82 
83  for(int i=0;i<tokens.size();++i)
84  {
85  for(int j=0;j<topics.size();++j)
86  {
87  if( (tokens[i].compare(topics[j].name) == 0) && (topics[j].datatype.compare("sensor_msgs/LaserScan") == 0) )
88  {
89  tmp_input_topics.push_back(topics[j].name);
90  }
91  }
92  }
93 
94  sort(tmp_input_topics.begin(),tmp_input_topics.end());
95  std::vector<string>::iterator last = std::unique(tmp_input_topics.begin(), tmp_input_topics.end());
96  tmp_input_topics.erase(last, tmp_input_topics.end());
97 
98 
99  // Do not re-subscribe if the topics are the same
100  if( (tmp_input_topics.size() != input_topics.size()) || !equal(tmp_input_topics.begin(),tmp_input_topics.end(),input_topics.begin()))
101  {
102 
103  // Unsubscribe from previous topics
104  for(int i=0; i<scan_subscribers.size(); ++i)
105  scan_subscribers[i].shutdown();
106 
107  input_topics = tmp_input_topics;
108  if(input_topics.size() > 0)
109  {
110  scan_subscribers.resize(input_topics.size());
111  clouds_modified.resize(input_topics.size());
112  clouds.resize(input_topics.size());
113  ROS_INFO("Subscribing to topics\t%ld", scan_subscribers.size());
114  for(int i=0; i<input_topics.size(); ++i)
115  {
116  scan_subscribers[i] = node_.subscribe<sensor_msgs::LaserScan> (input_topics[i].c_str(), 1, boost::bind(&LaserscanMerger::scanCallback,this, _1, input_topics[i]));
117  clouds_modified[i] = false;
118  cout << input_topics[i] << " ";
119  }
120  }
121  else
122  ROS_INFO("Not subscribed to any topic.");
123  }
124 }
125 
127 {
128  ros::NodeHandle nh("~");
129 
130  nh.getParam("destination_frame", destination_frame);
131  nh.getParam("cloud_destination_topic", cloud_destination_topic);
132  nh.getParam("scan_destination_topic", scan_destination_topic);
133  nh.getParam("laserscan_topics", laserscan_topics);
134 
135  nh.param("angle_min", angle_min, -2.36);
136  nh.param("angle_max", angle_max, 2.36);
137  nh.param("angle_increment", angle_increment, 0.0058);
138  nh.param("scan_time", scan_time, 0.0333333);
139  nh.param("range_min", range_min, 0.45);
140  nh.param("range_max", range_max, 25.0);
141 
142  this->laserscan_topic_parser();
143 
144  point_cloud_publisher_ = node_.advertise<sensor_msgs::PointCloud2> (cloud_destination_topic.c_str(), 1, false);
145  laser_scan_publisher_ = node_.advertise<sensor_msgs::LaserScan> (scan_destination_topic.c_str(), 1, false);
146 
147 }
148 
149 void LaserscanMerger::scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan, std::string topic)
150 {
151  sensor_msgs::PointCloud tmpCloud1,tmpCloud2;
152  sensor_msgs::PointCloud2 tmpCloud3;
153 
154  // Verify that TF knows how to transform from the received scan to the destination scan frame
155  tfListener_.waitForTransform(scan->header.frame_id.c_str(), destination_frame.c_str(), scan->header.stamp, ros::Duration(1));
156  projector_.transformLaserScanToPointCloud(scan->header.frame_id, *scan, tmpCloud1, tfListener_, laser_geometry::channel_option::Distance);
157  try
158  {
159  tfListener_.transformPointCloud(destination_frame.c_str(), tmpCloud1, tmpCloud2);
160  }catch (tf::TransformException ex){ROS_ERROR("%s",ex.what());return;}
161 
162  for(int i=0; i<input_topics.size(); ++i)
163  {
164  if(topic.compare(input_topics[i]) == 0)
165  {
166  sensor_msgs::convertPointCloudToPointCloud2(tmpCloud2,tmpCloud3);
167  pcl_conversions::toPCL(tmpCloud3, clouds[i]);
168  clouds_modified[i] = true;
169  }
170  }
171 
172  // Count how many scans we have
173  int totalClouds = 0;
174  for(int i=0; i<clouds_modified.size(); ++i)
175  if(clouds_modified[i])
176  ++totalClouds;
177 
178  // Go ahead only if all subscribed scans have arrived
179  if(totalClouds == clouds_modified.size())
180  {
181  pcl::PCLPointCloud2 merged_cloud = clouds[0];
182  clouds_modified[0] = false;
183 
184  for(int i=1; i<clouds_modified.size(); ++i)
185  {
186  pcl::concatenatePointCloud(merged_cloud, clouds[i], merged_cloud);
187  clouds_modified[i] = false;
188  }
189 
190  point_cloud_publisher_.publish(merged_cloud);
191 
192  Eigen::MatrixXf points;
193  getPointCloudAsEigen(merged_cloud,points);
194 
195  pointcloud_to_laserscan(points, &merged_cloud);
196  }
197 }
198 
199 void LaserscanMerger::pointcloud_to_laserscan(Eigen::MatrixXf points, pcl::PCLPointCloud2 *merged_cloud)
200 {
201  sensor_msgs::LaserScanPtr output(new sensor_msgs::LaserScan());
202  output->header = pcl_conversions::fromPCL(merged_cloud->header);
203  output->header.frame_id = destination_frame.c_str();
204  output->header.stamp = ros::Time::now(); //fixes #265
205  output->angle_min = this->angle_min;
206  output->angle_max = this->angle_max;
207  output->angle_increment = this->angle_increment;
208  output->time_increment = this->time_increment;
209  output->scan_time = this->scan_time;
210  output->range_min = this->range_min;
211  output->range_max = this->range_max;
212 
213  uint32_t ranges_size = std::ceil((output->angle_max - output->angle_min) / output->angle_increment);
214  output->ranges.assign(ranges_size, output->range_max + 1.0);
215 
216  for(int i=0; i<points.cols(); i++)
217  {
218  const float &x = points(0,i);
219  const float &y = points(1,i);
220  const float &z = points(2,i);
221 
222  if ( std::isnan(x) || std::isnan(y) || std::isnan(z) )
223  {
224  ROS_DEBUG("rejected for nan in point(%f, %f, %f)\n", x, y, z);
225  continue;
226  }
227 
228  double range_sq = y*y+x*x;
229  double range_min_sq_ = output->range_min * output->range_min;
230  if (range_sq < range_min_sq_) {
231  ROS_DEBUG("rejected for range %f below minimum value %f. Point: (%f, %f, %f)", range_sq, range_min_sq_, x, y, z);
232  continue;
233  }
234 
235  double angle = atan2(y, x);
236  if (angle < output->angle_min || angle > output->angle_max)
237  {
238  ROS_DEBUG("rejected for angle %f not in range (%f, %f)\n", angle, output->angle_min, output->angle_max);
239  continue;
240  }
241  int index = (angle - output->angle_min) / output->angle_increment;
242 
243 
244  if (output->ranges[index] * output->ranges[index] > range_sq)
245  output->ranges[index] = sqrt(range_sq);
246  }
247 
248  laser_scan_publisher_.publish(output);
249 }
250 
251 int main(int argc, char** argv)
252 {
253  ros::init(argc, argv, "laser_multi_merger");
254 
255  LaserscanMerger _laser_merger;
256 
257  dynamic_reconfigure::Server<laserscan_multi_mergerConfig> server;
258  dynamic_reconfigure::Server<laserscan_multi_mergerConfig>::CallbackType f;
259 
260  f = boost::bind(&LaserscanMerger::reconfigureCallback,&_laser_merger, _1, _2);
261  server.setCallback(f);
262 
263  ros::spin();
264 
265  return 0;
266 }
vector< bool > clouds_modified
tf::TransformListener tfListener_
f
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
laser_geometry::LaserProjection projector_
ROSCPP_DECL bool getTopics(V_TopicInfo &topics)
vector< ros::Subscriber > scan_subscribers
std::vector< TopicInfo > V_TopicInfo
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
ROSCPP_DECL void spin(Spinner &spinner)
void fromPCL(const pcl::uint64_t &pcl_stamp, ros::Time &stamp)
int main(int argc, char **argv)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
vector< string > input_topics
TFSIMD_FORCE_INLINE const tfScalar & z() const
bool concatenatePointCloud(const sensor_msgs::PointCloud2 &cloud1, const sensor_msgs::PointCloud2 &cloud2, sensor_msgs::PointCloud2 &cloud_out)
void reconfigureCallback(laserscan_multi_mergerConfig &config, uint32_t level)
static bool convertPointCloudToPointCloud2(const sensor_msgs::PointCloud &input, sensor_msgs::PointCloud2 &output)
bool getParam(const std::string &key, std::string &s) const
void scanCallback(const sensor_msgs::LaserScan::ConstPtr &scan, std::string topic)
static Time now()
void pointcloud_to_laserscan(Eigen::MatrixXf points, pcl::PCLPointCloud2 *merged_cloud)
ros::Publisher point_cloud_publisher_
ros::Publisher laser_scan_publisher_
#define ROS_ERROR(...)
vector< pcl::PCLPointCloud2 > clouds
void toPCL(const ros::Time &stamp, pcl::uint64_t &pcl_stamp)
#define ROS_DEBUG(...)


ira_laser_tools
Author(s):
autogenerated on Wed Jun 5 2019 21:41:53