self_filter.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, 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 the Willow Garage 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 
37 #include <ros/ros.h>
38 #include <sstream>
40 #include <tf/message_filter.h>
42 
43 namespace robot_self_filter
44 {
46 {
47 public:
48 
49  SelfFilter(void): nh_("~"), subscribing_(false)
50  {
51  nh_.param<std::string>("sensor_frame", sensor_frame_, std::string());
52  nh_.param("use_rgb", use_rgb_, false);
53  nh_.param("max_queue_size", max_queue_size_, 10);
54  if (use_rgb_)
55  {
57  }
58  else
59  {
61  }
63  = boost::bind( &SelfFilter::connectionCallback, this, _1);
64 
65  if (use_rgb_)
66  {
68  }
69  else
70  {
72  }
73  pointCloudPublisher_ = root_handle_.advertise<sensor_msgs::PointCloud2>("cloud_out", 1,
74  connect_cb, connect_cb);
75  }
76 
78  {
79  if (self_filter_)
80  {
81  delete self_filter_;
82  }
83  if (self_filter_rgb_)
84  {
85  delete self_filter_rgb_;
86  }
87  }
88 
89 private:
90 
92  {
94  if (!subscribing_) {
95  subscribe();
96  subscribing_ = true;
97  }
98  }
99  else {
100  if (subscribing_) {
101  unsubscribe();
102  subscribing_ = false;
103  }
104  }
105  }
106 
107  void subscribe() {
108  if(frames_.empty())
109  {
110  ROS_DEBUG("No valid frames have been passed into the self filter. Using a callback that will just forward scans on.");
111  no_filter_sub_ = root_handle_.subscribe<sensor_msgs::PointCloud2>("cloud_in", 1, boost::bind(&SelfFilter::noFilterCallback, this, _1));
112  }
113  else
114  {
115  ROS_DEBUG("Valid frames were passed in. We'll filter them.");
118  mn_->setTargetFrames(frames_);
119  mn_->registerCallback(boost::bind(&SelfFilter::cloudCallback, this, _1));
120  }
121  }
122 
123  void unsubscribe() {
124  if (frames_.empty()) {
126  }
127  else {
128  sub_.unsubscribe();
129  }
130  }
131 
132  void noFilterCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud){
134  ROS_DEBUG("Self filter publishing unfiltered frame");
135  }
136 
137  void cloudCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud2)
138  {
139  ROS_DEBUG("Got pointcloud that is %f seconds old", (ros::Time::now() - cloud2->header.stamp).toSec());
140  std::vector<int> mask;
142 
143 
144  sensor_msgs::PointCloud2 out2;
145  int input_size = 0;
146  int output_size = 0;
147  if (use_rgb_)
148  {
149  typename pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
150  pcl::fromROSMsg(*cloud2, *cloud);
151  pcl::PointCloud<pcl::PointXYZRGB> out;
153  pcl::toROSMsg(out, out2);
154  out2.header.stamp = cloud2->header.stamp;
155  input_size = cloud->points.size();
156  output_size = out.points.size();
157  }
158  else
159  {
160  typename pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
161  pcl::fromROSMsg(*cloud2, *cloud);
162  pcl::PointCloud<pcl::PointXYZ> out;
164  pcl::toROSMsg(out, out2);
165  out2.header.stamp = cloud2->header.stamp;
166  input_size = cloud->points.size();
167  output_size = out.points.size();
168  }
169 
170  double sec = (ros::WallTime::now() - tm).toSec();
172  ROS_DEBUG("Self filter: reduced %d points to %d points in %f seconds", input_size, output_size, sec);
173 
174  }
175 
177  //tf::MessageNotifier<robot_self_filter::PointCloud> *mn_;
179 
182 
185  std::string sensor_frame_;
186  bool use_rgb_;
188  std::vector<std::string> frames_;
189 
193 };
194 }
195 
196 int main(int argc, char **argv)
197 {
198  ros::init(argc, argv, "self_filter");
199 
200  ros::NodeHandle nh("~");
202  ros::spin();
203 
204  return 0;
205 }
void getLinkNames(std::vector< std::string > &frames) const
Get the set of link names that have been instantiated for self filtering.
Definition: self_mask.h:406
filters::SelfFilter< pcl::PointXYZ > * self_filter_
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
XmlRpcServer s
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
std::vector< std::string > frames_
ROSCPP_DECL void spin(Spinner &spinner)
tf::TransformListener tf_
filters::SelfFilter< pcl::PointXYZRGB > * self_filter_rgb_
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void noFilterCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
robot_self_filter::SelfMask< PointT > * getSelfMask()
uint32_t getNumSubscribers() const
int main(int argc, char **argv)
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)
static WallTime now()
void toROSMsg(const sensor_msgs::PointCloud2 &cloud, sensor_msgs::Image &image)
static Time now()
boost::shared_ptr< tf::MessageFilter< sensor_msgs::PointCloud2 > > mn_
ros::Publisher pointCloudPublisher_
bool updateWithSensorFrame(const PointCloud &data_in, PointCloud &data_out, const std::string &sensor_frame)
void connectionCallback(const ros::SingleSubscriberPublisher &pub)
Definition: self_filter.cpp:91
#define ROS_DEBUG(...)
void cloudCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud2)


robot_self_filter
Author(s): Eitan Marder-Eppstein
autogenerated on Thu Jun 6 2019 19:59:05