scan_to_scan_filter_chain.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 
31 #include "ros/ros.h"
32 #include "sensor_msgs/LaserScan.h"
34 #include "tf/message_filter.h"
35 #include "tf/transform_listener.h"
36 #include "filters/filter_chain.h"
37 
39 {
40 protected:
41  // Our NodeHandle
44 
45  // Components for tf::MessageFilter
50 
51  // Filter Chain
53 
54  // Components for publishing
55  sensor_msgs::LaserScan msg_;
57 
58  // Deprecation helpers
61 
62 public:
63  // Constructor
65  private_nh_("~"),
66  scan_sub_(nh_, "scan", 50),
67  tf_(NULL),
68  tf_filter_(NULL),
69  filter_chain_("sensor_msgs::LaserScan")
70  {
71  // Configure filter chain
72 
73  using_filter_chain_deprecated_ = private_nh_.hasParam("filter_chain");
74 
75  if (using_filter_chain_deprecated_)
76  filter_chain_.configure("filter_chain", private_nh_);
77  else
78  filter_chain_.configure("scan_filter_chain", private_nh_);
79 
80  std::string tf_message_filter_target_frame;
81 
82  if (private_nh_.hasParam("tf_message_filter_target_frame"))
83  {
84  private_nh_.getParam("tf_message_filter_target_frame", tf_message_filter_target_frame);
85 
86  private_nh_.param("tf_message_filter_tolerance", tf_filter_tolerance_, 0.03);
87 
88  tf_ = new tf::TransformListener();
89  tf_filter_ = new tf::MessageFilter<sensor_msgs::LaserScan>(scan_sub_, *tf_, "", 50);
90  tf_filter_->setTargetFrame(tf_message_filter_target_frame);
91  tf_filter_->setTolerance(ros::Duration(tf_filter_tolerance_));
92 
93  // Setup tf::MessageFilter generates callback
94  tf_filter_->registerCallback(boost::bind(&ScanToScanFilterChain::callback, this, _1));
95  }
96  else
97  {
98  // Pass through if no tf_message_filter_target_frame
99  scan_sub_.registerCallback(boost::bind(&ScanToScanFilterChain::callback, this, _1));
100  }
101 
102  // Advertise output
103  output_pub_ = nh_.advertise<sensor_msgs::LaserScan>("scan_filtered", 1000);
104 
105  // Set up deprecation printout
106  deprecation_timer_ = nh_.createTimer(ros::Duration(5.0), boost::bind(&ScanToScanFilterChain::deprecation_warn, this, _1));
107  }
108 
109  // Destructor
111  {
112  if (tf_filter_)
113  delete tf_filter_;
114  if (tf_)
115  delete tf_;
116  }
117 
118  // Deprecation warning callback
120  {
121  if (using_filter_chain_deprecated_)
122  ROS_WARN("Use of '~filter_chain' parameter in scan_to_scan_filter_chain has been deprecated. Please replace with '~scan_filter_chain'.");
123  }
124 
125  // Callback
126  void callback(const sensor_msgs::LaserScan::ConstPtr& msg_in)
127  {
128  // Run the filter chain
129  if (filter_chain_.update(*msg_in, msg_))
130  {
131  //only publish result if filter succeeded
132  output_pub_.publish(msg_);
133  } else {
134  ROS_ERROR_THROTTLE(1, "Filtering the scan from time %i.%i failed.", msg_in->header.stamp.sec, msg_in->header.stamp.nsec);
135  }
136  }
137 };
138 
139 int main(int argc, char **argv)
140 {
141  ros::init(argc, argv, "scan_to_scan_filter_chain");
142 
144  ros::spin();
145 
146  return 0;
147 }
int main(int argc, char **argv)
void publish(const boost::shared_ptr< M > &message) const
message_filters::Subscriber< sensor_msgs::LaserScan > scan_sub_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
tf::MessageFilter< sensor_msgs::LaserScan > * tf_filter_
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
#define ROS_ERROR_THROTTLE(rate,...)
filters::FilterChain< sensor_msgs::LaserScan > filter_chain_
void deprecation_warn(const ros::TimerEvent &e)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool getParam(const std::string &key, std::string &s) const
void callback(const sensor_msgs::LaserScan::ConstPtr &msg_in)
bool update(const T &data_in, T &data_out)
bool configure(std::string param_name, ros::NodeHandle node=ros::NodeHandle())
void setTargetFrame(const std::string &target_frame)
tf::TransformListener * tf_
bool hasParam(const std::string &key) const
void setTolerance(const ros::Duration &tolerance)
Connection registerCallback(const C &callback)


laser_filters
Author(s): Tully Foote
autogenerated on Tue Mar 17 2020 03:40:02