chomp_trajectory.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009, Willow Garage, Inc.
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 the Willow Garage 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 
00037 #include <ros/ros.h>
00038 #include <chomp_motion_planner/chomp_trajectory.h>
00039 #include <iostream>
00040 
00041 namespace chomp
00042 {
00043 
00044 ChompTrajectory::ChompTrajectory(const planning_models::RobotModelConstPtr& robot_model,
00045                                  double duration,
00046                                  double discretization,
00047                                  std::string group_name):
00048   planning_group_name_(group_name),
00049   num_points_((duration/discretization)+1),
00050   discretization_(discretization),
00051   duration_(duration),
00052   start_index_(1),
00053   end_index_(num_points_-2)
00054 {
00055   std::map<std::string, planning_models::RobotModel::JointModelGroup*> group_map = robot_model->getJointModelGroupMap();
00056   const planning_models::RobotModel::JointModelGroup* modelGroup = group_map[planning_group_name_];
00057   num_joints_ = modelGroup->getJointModels().size();
00058   init();
00059 }
00060 
00061 ChompTrajectory::ChompTrajectory(const planning_models::RobotModelConstPtr& robot_model,
00062                                  int num_points,
00063                                  double discretization,
00064                                  std::string group_name):
00065   planning_group_name_(group_name),
00066   num_points_(num_points),
00067   discretization_(discretization),
00068   duration_((num_points-1)*discretization),
00069   start_index_(1),
00070   end_index_(num_points_-2)
00071 {
00072   std::map<std::string, planning_models::RobotModel::JointModelGroup*> group_map = robot_model->getJointModelGroupMap();
00073   const planning_models::RobotModel::JointModelGroup* modelGroup = group_map[planning_group_name_];
00074   num_joints_ = modelGroup->getJointModels().size();
00075   init();
00076 }
00077 
00078 ChompTrajectory::ChompTrajectory(const ChompTrajectory& source_traj, const std::string& planning_group, int diff_rule_length):
00079   planning_group_name_(planning_group),
00080   discretization_(source_traj.discretization_)
00081 {
00082   num_joints_ = source_traj.getNumJoints();
00083 
00084   // figure out the num_points_:
00085   // we need diff_rule_length-1 extra points on either side:
00086   int start_extra = (diff_rule_length - 1) - source_traj.start_index_;
00087   int end_extra = (diff_rule_length - 1) - ((source_traj.num_points_-1) - source_traj.end_index_);
00088 
00089   num_points_ = source_traj.num_points_ + start_extra + end_extra;
00090   start_index_ = diff_rule_length - 1;
00091   end_index_ = (num_points_ - 1) - (diff_rule_length - 1);
00092   duration_ = (num_points_ - 1)*discretization_;
00093 
00094   // allocate the memory:
00095   init();
00096 
00097   full_trajectory_index_.resize(num_points_);
00098 
00099   // now copy the trajectories over:
00100   for (int i=0; i<num_points_; i++)
00101   {
00102     int source_traj_point = i - start_extra;
00103     if (source_traj_point < 0)
00104       source_traj_point = 0;
00105     if (source_traj_point >= source_traj.num_points_)
00106       source_traj_point = source_traj.num_points_-1;
00107     full_trajectory_index_[i] = source_traj_point;
00108     for (int j=0; j<num_joints_; j++)
00109     {
00110       (*this)(i,j) = source_traj(source_traj_point, j);
00111     }
00112   }
00113 }
00114 
00115 ChompTrajectory::ChompTrajectory(const planning_models::RobotModelConstPtr& robot_model,
00116                                  const std::string& planning_group,
00117                                  const trajectory_msgs::JointTrajectory& traj) :
00118   planning_group_name_(planning_group)
00119 {
00120   std::map<std::string, planning_models::RobotModel::JointModelGroup*> group_map = robot_model->getJointModelGroupMap();
00121   const planning_models::RobotModel::JointModelGroup* model_group = group_map[planning_group_name_];
00122   num_joints_ = model_group->getJointModels().size();
00123   double discretization = (traj.points[1].time_from_start-traj.points[0].time_from_start).toSec();
00124 
00125   double discretization2 = (traj.points[2].time_from_start-traj.points[1].time_from_start).toSec();
00126 
00127   if(fabs(discretization2-discretization) > .001) {
00128     ROS_WARN_STREAM("Trajectory Discretization not constant " << discretization << " " << discretization2);
00129   }
00130   discretization_ = discretization;
00131 
00132   num_points_ = traj.points.size()+1;
00133   duration_ = (traj.points.back().time_from_start-traj.points[0].time_from_start).toSec();
00134 
00135   start_index_ = 1;
00136   end_index_ = num_points_-2;
00137 
00138   init();
00139 
00140   for(int i = 0; i < num_points_; i++) {
00141     for(int j = 0; j < num_joints_; j++) {
00142       trajectory_(i,j) = 0.0;
00143     }
00144   }
00145   overwriteTrajectory(traj);
00146 }
00147 
00148 ChompTrajectory::~ChompTrajectory()
00149 {
00150 }
00151 
00152 void ChompTrajectory::overwriteTrajectory(const trajectory_msgs::JointTrajectory& traj) {
00153   for(unsigned int i = 1; i <= traj.points.size(); i++) {
00154     for(unsigned int j = 0; j < traj.joint_names.size(); j++) {
00155       trajectory_(i,j) = traj.points[i-1].positions[j];
00156     }
00157   }
00158 }
00159 
00160 void ChompTrajectory::init()
00161 {
00162   trajectory_.resize(num_points_, num_joints_);
00163   trajectory_ = Eigen::MatrixXd(num_points_, num_joints_);
00164 }
00165 
00166 void ChompTrajectory::updateFromGroupTrajectory(const ChompTrajectory& group_trajectory)
00167 {
00168   int num_vars_free = end_index_ - start_index_ + 1;
00169   for (int i=0; i < num_joints_; i++)
00170   {
00171     trajectory_.block(start_index_, i, num_vars_free, 1) = group_trajectory.trajectory_.block(group_trajectory.start_index_, i, num_vars_free, 1);
00172   }
00173 }
00174 
00175 void ChompTrajectory::fillInMinJerk()
00176 {
00177   double start_index = start_index_-1;
00178   double end_index = end_index_+1;
00179   double T[6]; // powers of the time duration
00180   T[0] = 1.0;
00181   T[1] = (end_index - start_index)*discretization_;
00182 
00183   for (int i=2; i<=5; i++)
00184     T[i] = T[i-1]*T[1];
00185 
00186   // calculate the spline coefficients for each joint:
00187   // (these are for the special case of zero start and end vel and acc)
00188   double coeff[num_joints_][6];
00189   for (int i=0; i<num_joints_; i++)
00190   {
00191     double x0 = (*this)(start_index,i);
00192     double x1 = (*this)(end_index,i);
00193     coeff[i][0] = x0;
00194     coeff[i][1] = 0;
00195     coeff[i][2] = 0;
00196     coeff[i][3] = (-20*x0 + 20*x1) / (2*T[3]);
00197     coeff[i][4] = (30*x0 - 30*x1) / (2*T[4]);
00198     coeff[i][5] = (-12*x0 + 12*x1) / (2*T[5]);
00199   }
00200 
00201   // now fill in the joint positions at each time step
00202   for (int i=start_index+1; i<end_index; i++)
00203   {
00204     double t[6]; // powers of the time index point
00205     t[0] = 1.0;
00206     t[1] = (i - start_index)*discretization_;
00207     for (int k=2; k<=5; k++)
00208       t[k] = t[k-1]*t[1];
00209 
00210     for (int j=0; j<num_joints_; j++)
00211     {
00212       (*this)(i,j) = 0.0;
00213       for (int k=0; k<=5; k++)
00214       {
00215         (*this)(i,j) += t[k]*coeff[j][k];
00216       }
00217     }
00218   }
00219 }
00220 
00221 } // namespace chomp


chomp_motion_planner
Author(s): Gil Jones
autogenerated on Mon Oct 6 2014 11:12:26