smoothing_trajectory_filter.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2014, 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
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Southwest Research Institute nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Chris Lewis */
00036 
00037 #include <industrial_trajectory_filters/smoothing_trajectory_filter.h>
00038 #include <console_bridge/console.h>
00039 #include <moveit/robot_state/conversions.h>
00040 #include <stdio.h>
00041 
00042 #include <ros/ros.h>
00043 #include <ros/console.h>
00044 
00045 namespace industrial_trajectory_filters
00046 {
00047 
00048 
00049   SmoothingTrajectoryFilter::SmoothingTrajectoryFilter()
00050   {
00051       initialized_ = false;
00052   }
00053 
00054   bool SmoothingTrajectoryFilter::init(std::vector<double> &coef)
00055   {
00056     if(coef.size()%2 == 1) {            // smoothing filters must have an odd number of coefficients
00057       initialized_ = true;
00058       num_coef_ = coef.size();
00059       double sum =0;
00060       for(int i=0; i<num_coef_; i++) {
00061         coef_.push_back(coef[i]);
00062         sum += coef[i];
00063       }
00064       gain_ = sum;              // set gain to be the sum of the coefficients because we need unity gain
00065       return(true);
00066     }
00067     else{
00068       initialized_ = false;
00069       return(false);
00070     }
00071   }
00072 
00073   SmoothingTrajectoryFilter::~SmoothingTrajectoryFilter()
00074   {
00075     coef_.clear();
00076   }
00077 
00078  bool SmoothingTrajectoryFilter::applyFilter(robot_trajectory::RobotTrajectory& rob_trajectory) const
00079   {
00080     if(!initialized_) return(false);
00081 
00082     const int num_points = rob_trajectory.getWayPointCount(); 
00083     if(num_points <=2) return(false); // nothing to do here, can't change either first or last point
00084     const int num_states = rob_trajectory.getWayPoint(0).getVariableCount();
00085     std::vector<double> xv;
00086     
00087     // filter each variable independently
00088     for(int i=0; i<num_states; i++){ 
00089       double start_value     = rob_trajectory.getWayPoint(0).getVariablePosition(i);
00090       double start_slope     = rob_trajectory.getWayPoint(1).getVariablePosition(i) - start_value; // slope at start
00091       double end_value      = rob_trajectory.getWayPoint(num_points-1).getVariablePosition(i);
00092       double end_slope      = end_value - rob_trajectory.getWayPoint(num_points-2).getVariablePosition(i); // slope at end
00093  
00094       // initialize the filter to have initial slope
00095       xv.clear();
00096       double value = start_value - (num_coef_/2)*start_slope;
00097       for(int j=0; j<num_coef_; j++) { 
00098         xv.push_back(value);
00099         value += start_slope;
00100       }
00101       
00102       // cycle through every waypoint, and apply the filter, NOTE, 1st and last waypoints should not be changed
00103       for(int j=1; j<num_points-1; j++){
00104         // shift backwards
00105         for(int k=0; k<num_coef_-1; k++){
00106           xv[k] = xv[k+1];  
00107         }
00108         
00109         // get next input to filter which is num_coef/2 in front of current point being smoothed
00110         if(j+num_coef_/2 < num_points){ 
00111           xv[num_coef_ - 1] = rob_trajectory.getWayPoint(j+num_coef_/2).getVariablePosition(i); // i'th state of j'th waypoint
00112         }
00113         else{
00114           end_value += end_slope;
00115           xv[num_coef_-1] = end_value; // fill by continuing with final slope
00116         }
00117         // apply the filter
00118         double sum = 0.0;
00119         for(int k=0; k<num_coef_; k++){ 
00120           sum += xv[k]*coef_[k];
00121         }
00122 
00123         // save the results
00124         rob_trajectory.getWayPointPtr(j)->setVariablePosition(i,sum/gain_); // j'th waypoint, i'th variable set to output value
00125 
00126       }// end for every waypoint
00127 
00128     } // end for every state
00129 
00130     return(true);
00131 
00132 }// end SmoothingTrajectoryFilter::applyfilter()
00133 
00134 }  // end namespace 


industrial_trajectory_filters
Author(s): Shaun Edwards , Jorge Nicho
autogenerated on Tue Jan 17 2017 21:09:57