$search
00001 /* 00002 * Copyright (c) 2008, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 00031 #include "ros/ros.h" 00032 #include "sensor_msgs/LaserScan.h" 00033 #include "message_filters/subscriber.h" 00034 #include "tf/message_filter.h" 00035 #include "tf/transform_listener.h" 00036 #include "filters/filter_chain.h" 00037 00038 class ScanToScanFilterChain 00039 { 00040 protected: 00041 // Our NodeHandle 00042 ros::NodeHandle nh_; 00043 ros::NodeHandle private_nh_; 00044 00045 // Components for tf::MessageFilter 00046 tf::TransformListener tf_; 00047 message_filters::Subscriber<sensor_msgs::LaserScan> scan_sub_; 00048 message_filters::Subscriber<sensor_msgs::LaserScan> no_sub_; 00049 tf::MessageFilter<sensor_msgs::LaserScan> tf_filter_; 00050 00051 // Filter Chain 00052 filters::FilterChain<sensor_msgs::LaserScan> filter_chain_; 00053 00054 // Components for publishing 00055 sensor_msgs::LaserScan msg_; 00056 ros::Publisher output_pub_; 00057 00058 // Deprecation helpers 00059 ros::Timer deprecation_timer_; 00060 bool using_filter_chain_deprecated_; 00061 00062 public: 00063 // Constructor 00064 ScanToScanFilterChain() : 00065 private_nh_("~"), 00066 scan_sub_(nh_, "scan", 50), 00067 tf_filter_(scan_sub_, tf_, "", 50), 00068 filter_chain_("sensor_msgs::LaserScan") 00069 { 00070 // Configure filter chain 00071 00072 using_filter_chain_deprecated_ = private_nh_.hasParam("filter_chain"); 00073 00074 if (using_filter_chain_deprecated_) 00075 filter_chain_.configure("filter_chain", private_nh_); 00076 else 00077 filter_chain_.configure("scan_filter_chain", private_nh_); 00078 00079 std::string tf_message_filter_target_frame; 00080 00081 if (private_nh_.hasParam("tf_message_filter_target_frame")) 00082 { 00083 private_nh_.getParam("tf_message_filter_target_frame", tf_message_filter_target_frame); 00084 00085 tf_filter_.setTargetFrame(tf_message_filter_target_frame); 00086 tf_filter_.setTolerance(ros::Duration(0.03)); 00087 00088 // Setup tf::MessageFilter generates callback 00089 tf_filter_.registerCallback(boost::bind(&ScanToScanFilterChain::callback, this, _1)); 00090 } 00091 else 00092 { 00093 tf_filter_.connectInput(no_sub_); 00094 // Pass through if no tf_message_filter_target_frame 00095 scan_sub_.registerCallback(boost::bind(&ScanToScanFilterChain::callback, this, _1)); 00096 } 00097 00098 // Advertise output 00099 output_pub_ = nh_.advertise<sensor_msgs::LaserScan>("scan_filtered", 1000); 00100 00101 // Set up deprecation printout 00102 deprecation_timer_ = nh_.createTimer(ros::Duration(5.0), boost::bind(&ScanToScanFilterChain::deprecation_warn, this, _1)); 00103 } 00104 00105 // Deprecation warning callback 00106 void deprecation_warn(const ros::TimerEvent& e) 00107 { 00108 if (using_filter_chain_deprecated_) 00109 ROS_WARN("Use of '~filter_chain' parameter in scan_to_scan_filter_chain has been deprecated. Please replace with '~scan_filter_chain'."); 00110 } 00111 00112 // Callback 00113 void callback(const sensor_msgs::LaserScan::ConstPtr& msg_in) 00114 { 00115 // Run the filter chain 00116 filter_chain_.update (*msg_in, msg_); 00117 00118 // Publish the output 00119 output_pub_.publish(msg_); 00120 } 00121 }; 00122 00123 int main(int argc, char **argv) 00124 { 00125 ros::init(argc, argv, "scan_to_scan_filter_chain"); 00126 00127 ScanToScanFilterChain t; 00128 ros::spin(); 00129 00130 return 0; 00131 }