n_point_filter.cpp
Go to the documentation of this file.
00001 /*
00002 * Software License Agreement (BSD License)
00003 *
00004 * Copyright (c) 2011, Southwest Research Institute
00005 * All rights reserved.
00006 *
00007 * Redistribution and use in source and binary forms, with or without
00008 * modification, are permitted provided that the following conditions are met:
00009 *
00010 *       * Redistributions of source code must retain the above copyright
00011 *       notice, this list of conditions and the following disclaimer.
00012 *       * Redistributions in binary form must reproduce the above copyright
00013 *       notice, this list of conditions and the following disclaimer in the
00014 *       documentation and/or other materials provided with the distribution.
00015 *       * Neither the name of the Southwest Research Institute, nor the names
00016 *       of its contributors may be used to endorse or promote products derived
00017 *       from this software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 * POSSIBILITY OF SUCH DAMAGE.
00030 */
00031 
00032 #include <arm_navigation_msgs/FilterJointTrajectoryWithConstraints.h>
00033 #include <industrial_trajectory_filters/n_point_filter.h>
00034 
00035 using namespace industrial_trajectory_filters;
00036 
00037 const int DEFAULT_N=2;
00038 
00039 
00040 template <typename T>
00041 NPointFilter<T>::NPointFilter()
00042 {
00043         ROS_INFO_STREAM("Constructing N point filter");
00044         n_points_ = DEFAULT_N;
00045 }
00046 
00047 template <typename T>
00048 NPointFilter<T>::~NPointFilter()
00049 {}
00050 
00051 template <typename T>
00052 bool NPointFilter<T>::configure()
00053 {
00054   if (!filters::FilterBase<T>::getParam("n_points", n_points_))
00055   {
00056     ROS_WARN_STREAM("NPointFilter, params has no attribute n_points.");
00057   }
00058   if (n_points_ < 2)
00059   {
00060         ROS_WARN_STREAM("n_points attribute less than min(2), setting to minimum");
00061         n_points_ = 2;
00062   }
00063   ROS_INFO_STREAM("Using a n_points value of " << n_points_);
00064 
00065  return true;
00066 }
00067 
00068 
00069 template <typename T>
00070 bool NPointFilter<T>::update(const T& trajectory_in,
00071                                    T& trajectory_out)
00072 {
00073   bool success = false;
00074   int size_in = trajectory_in.request.trajectory.points.size();
00075 
00076   // Copy non point related data
00077   trajectory_out = trajectory_in;
00078   // Clear out the trajectory points
00079   trajectory_out.request.trajectory.points.clear();
00080 
00081   if (size_in > n_points_)
00082   {
00083                 //Add first point to output trajectory
00084                 trajectory_out.request.trajectory.points.push_back(
00085                                 trajectory_in.request.trajectory.points.front());
00086 
00087                 int intermediate_points = n_points_ - 2;  //subtract the first and last elements
00088                 double int_point_increment = double(size_in)/double(intermediate_points + 1.0);
00089                 ROS_INFO_STREAM("Number of intermediate points: " << intermediate_points <<
00090                                 ", increment: " << int_point_increment);
00091 
00092                 // The intermediate point index is determined by the following equation:
00093                 //     int_point_index = i * int_point_increment
00094                 // Ex: n_points_ = 4, size_in = 11, intermediate_points = 2 ->
00095                 //     int_point_increment = 3.66667,
00096                 //     i = 1: int_point_index = 3
00097                 //               i = 2: int_point_index = 7
00098                 for (int i = 1; i <= intermediate_points; i++)
00099                 {
00100                         int int_point_index = int(double(i) * int_point_increment);
00101                         ROS_INFO_STREAM("Intermediate point index: " << int_point_index);
00102                         trajectory_out.request.trajectory.points.push_back(
00103                                         trajectory_in.request.trajectory.points[int_point_index]);
00104                 }
00105 
00106                 //Add last point to output trajectory
00107                 trajectory_out.request.trajectory.points.push_back(
00108                                 trajectory_in.request.trajectory.points.back());
00109 
00110                 ROS_INFO_STREAM("Filtered trajectory from: " << trajectory_in.request.trajectory.points.size()
00111                                 << " to: " << trajectory_out.request.trajectory.points.size());
00112 
00113                 success = true;
00114   }
00115   else
00116   {
00117         ROS_WARN_STREAM("Trajectory size less than n: " << n_points_ << ", pass through");
00118         trajectory_out = trajectory_in;
00119         success = true;
00120   }
00121 
00122   return success;
00123 }
00124 
00125 
00126 
00127 PLUGINLIB_DECLARE_CLASS(industrial_trajectory_filters,
00128                 IndustrialNPointFilterJointTrajectoryWithConstraints,
00129                 industrial_trajectory_filters::NPointFilter<arm_navigation_msgs::FilterJointTrajectoryWithConstraints>,
00130                 filters::FilterBase<arm_navigation_msgs::FilterJointTrajectoryWithConstraints>);
00131 


industrial_trajectory_filters
Author(s): Shaun Edwards
autogenerated on Mon Oct 6 2014 00:58:45