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 
44 {
45 public:
46 
47  SelfFilter(void): nh_("~"), subscribing_(false)
48  {
49  nh_.param<std::string>("sensor_frame", sensor_frame_, std::string());
50  nh_.param("use_rgb", use_rgb_, false);
51  if (use_rgb_)
52  {
54  }
55  else
56  {
58  }
60  = boost::bind( &SelfFilter::connectionCallback, this, _1);
61 
62  if (use_rgb_)
63  {
65  }
66  else
67  {
69  }
70  pointCloudPublisher_ = root_handle_.advertise<sensor_msgs::PointCloud2>("cloud_out", 1,
71  connect_cb, connect_cb);
72  }
73 
75  {
76  if (self_filter_)
77  {
78  delete self_filter_;
79  }
80  if (self_filter_rgb_)
81  {
82  delete self_filter_rgb_;
83  }
84  }
85 
86 private:
87 
89  {
91  if (!subscribing_) {
92  subscribe();
93  subscribing_ = true;
94  }
95  }
96  else {
97  if (subscribing_) {
98  unsubscribe();
99  subscribing_ = false;
100  }
101  }
102  }
103 
104  void subscribe() {
105  if(frames_.empty())
106  {
107  ROS_DEBUG("No valid frames have been passed into the self filter. Using a callback that will just forward scans on.");
108  no_filter_sub_ = root_handle_.subscribe<sensor_msgs::PointCloud2>("cloud_in", 1, boost::bind(&SelfFilter::noFilterCallback, this, _1));
109  }
110  else
111  {
112  ROS_DEBUG("Valid frames were passed in. We'll filter them.");
113  sub_.subscribe(root_handle_, "cloud_in", 1);
115  mn_->setTargetFrames(frames_);
116  mn_->registerCallback(boost::bind(&SelfFilter::cloudCallback, this, _1));
117  }
118  }
119 
120  void unsubscribe() {
121  if (frames_.empty()) {
123  }
124  else {
125  sub_.unsubscribe();
126  }
127  }
128 
129  void noFilterCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud){
131  ROS_DEBUG("Self filter publishing unfiltered frame");
132  }
133 
134  void cloudCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud2)
135  {
136  ROS_DEBUG("Got pointcloud that is %f seconds old", (ros::Time::now() - cloud2->header.stamp).toSec());
137  std::vector<int> mask;
139 
140 
141  sensor_msgs::PointCloud2 out2;
142  int input_size = 0;
143  int output_size = 0;
144  if (use_rgb_)
145  {
146  typename pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
147  pcl::fromROSMsg(*cloud2, *cloud);
148  pcl::PointCloud<pcl::PointXYZRGB> out;
150  pcl::toROSMsg(out, out2);
151  out2.header.stamp = cloud2->header.stamp;
152  input_size = cloud->points.size();
153  output_size = out.points.size();
154  }
155  else
156  {
157  typename pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
158  pcl::fromROSMsg(*cloud2, *cloud);
159  pcl::PointCloud<pcl::PointXYZ> out;
161  pcl::toROSMsg(out, out2);
162  out2.header.stamp = cloud2->header.stamp;
163  input_size = cloud->points.size();
164  output_size = out.points.size();
165  }
166 
167  double sec = (ros::WallTime::now() - tm).toSec();
169  ROS_DEBUG("Self filter: reduced %d points to %d points in %f seconds", input_size, output_size, sec);
170 
171  }
172 
174  //tf::MessageNotifier<robot_self_filter::PointCloud> *mn_;
176 
179 
182  std::string sensor_frame_;
183  bool use_rgb_;
185  std::vector<std::string> frames_;
186 
189 
190 };
191 
192 
193 int main(int argc, char **argv)
194 {
195  ros::init(argc, argv, "self_filter");
196 
197  ros::NodeHandle nh("~");
198  SelfFilter s;
199  ros::spin();
200 
201  return 0;
202 }
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
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
ros::Publisher pointCloudPublisher_
void publish(const boost::shared_ptr< M > &message) const
void cloudCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud2)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
~SelfFilter(void)
Definition: self_filter.cpp:74
ros::NodeHandle nh_
XmlRpcServer s
std::string sensor_frame_
filters::SelfFilter< pcl::PointXYZ > * self_filter_
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)
ros::Subscriber no_filter_sub_
void noFilterCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud)
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_
ROSCPP_DECL void spin(Spinner &spinner)
ros::NodeHandle root_handle_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void subscribe()
void connectionCallback(const ros::SingleSubscriberPublisher &pub)
Definition: self_filter.cpp:88
bool subscribing_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
boost::shared_ptr< tf::MessageFilter< sensor_msgs::PointCloud2 > > mn_
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()
tf::TransformListener tf_
void toROSMsg(const sensor_msgs::PointCloud2 &cloud, sensor_msgs::Image &image)
static Time now()
void unsubscribe()
std::vector< std::string > frames_
filters::SelfFilter< pcl::PointXYZRGB > * self_filter_rgb_
SelfFilter(void)
Definition: self_filter.cpp:47
bool updateWithSensorFrame(const PointCloud &data_in, PointCloud &data_out, const std::string &sensor_frame)
#define ROS_DEBUG(...)


pr2_navigation_self_filter
Author(s): Eitan Marder-Eppstein
autogenerated on Mon Jun 10 2019 14:28:54