laser_footprint_filter.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011-2012, 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 * FOOTPRINTAL, 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 
35 #include <math.h>
36 #include <ros/ros.h>
37 #include <sensor_msgs/LaserScan.h>
38 #include <tf/transform_listener.h>
39 
41 {
42 public:
44  : nh_("~"), listener_(ros::Duration(10))
45  {
46  scan_filtered_pub_ = nh_.advertise<sensor_msgs::LaserScan>("/scan_filtered", 1);
47  scan_sub_ = nh_.subscribe("/scan", 1000, &LaserFootprintFilter::update, this);
48 
49  nh_.param<double>("footprint_inscribed_radius", inscribed_radius_, 0.16495*1.1);
50  nh_.param<std::string>("base_frame", base_frame_, "/base_link");
51  }
52 
53  void update(const sensor_msgs::LaserScan& input_scan)
54  {
55  sensor_msgs::LaserScan filtered_scan;
56  filtered_scan = input_scan;
57 
58  double angle = filtered_scan.angle_min - filtered_scan.angle_increment;
59  geometry_msgs::PointStamped p_input;
60  p_input.header = input_scan.header;
61 
62  for(size_t i=0; i < filtered_scan.ranges.size(); i++)
63  {
64  angle += filtered_scan.angle_increment;
65  if(filtered_scan.ranges[i] >= filtered_scan.range_max) continue;
66 
67  p_input.point.x = cos(angle) * filtered_scan.ranges[i];
68  p_input.point.y = sin(angle) * filtered_scan.ranges[i];
69 
70  geometry_msgs::PointStamped p_transformed;
71  try{
72  listener_.transformPoint(base_frame_, p_input, p_transformed);
73  }catch(tf::TransformException &ex){
74  ROS_ERROR("Received an exception trying to transform a point: %s", ex.what());
75  return;
76  }
77 
78  if( inFootprint(p_transformed) )
79  {
80  filtered_scan.ranges[i] = filtered_scan.range_max + 1.0;
81  }
82 
83  }
84 
85  scan_filtered_pub_.publish(filtered_scan);
86  }
87 
88  // Filter out circular area
89  bool inFootprint(const geometry_msgs::PointStamped& scan_pt)
90  {
91  // Do a radius instead of a box.
92  if (sqrt(scan_pt.point.x*scan_pt.point.x + scan_pt.point.y*scan_pt.point.y) > inscribed_radius_)
93  return false;
94  return true;
95  }
96 
97 private:
101  std::string base_frame_;
105 };
106 
107 int main(int argc, char** argv)
108 {
109  ros::init(argc, argv, "laser_footprint_filter");
110 
112  ros::spin();
113 }
114 
tf::TransformListener listener_
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())
int main(int argc, char **argv)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
GLfloat angle
void transformPoint(const std::string &target_frame, const geometry_msgs::PointStamped &stamped_in, geometry_msgs::PointStamped &stamped_out) const
ROSCPP_DECL void spin(Spinner &spinner)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void update(const sensor_msgs::LaserScan &input_scan)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
bool inFootprint(const geometry_msgs::PointStamped &scan_pt)
TimeStamp Duration
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
#define ROS_ERROR(...)
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)


turtlebot_navigation
Author(s): Tully Foote
autogenerated on Mon Jun 10 2019 15:44:21