Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <pr2_arm_move_ik/trajectory_generation.h>
00039 #include <kdl/velocityprofile_trap.hpp>
00040
00041
00042 namespace trajectory{
00043
00044 TrajectoryGenerator::TrajectoryGenerator(double max_vel, double max_acc, unsigned int size)
00045 : generators_(size)
00046 {
00047 for (unsigned int i=0; i<size; i++)
00048 generators_[i] = new KDL::VelocityProfile_Trap(max_vel, max_acc);
00049 }
00050
00051
00052 TrajectoryGenerator::~TrajectoryGenerator()
00053 {
00054 for (unsigned int i=0; i<generators_.size(); i++)
00055 delete generators_[i];
00056 }
00057
00058 void TrajectoryGenerator::generate(const trajectory_msgs::JointTrajectory& traj_in, trajectory_msgs::JointTrajectory& traj_out)
00059 {
00060 ROS_INFO("Generating trajectory for list of points of size %d", (int) traj_in.points.size());
00061
00062
00063 if (traj_in.points.size() < 2){
00064 ROS_WARN("Trajectory message should contain at least two points, but it contains %d joints. Returning original trajectory", (int)traj_in.points.size());
00065 traj_out = traj_in;
00066 return;
00067 }
00068
00069
00070 trajectory_msgs::JointTrajectory traj_res = traj_in;
00071 traj_res.points.clear();
00072 trajectory_msgs::JointTrajectoryPoint points_tmp;
00073 points_tmp.positions.resize(generators_.size());
00074 points_tmp.velocities.resize(generators_.size());
00075 points_tmp.accelerations.resize(generators_.size());
00076
00077 double initial_time = traj_in.points[0].time_from_start.toSec();
00078
00079 for (unsigned int pnt=0; pnt<traj_in.points.size()-1; pnt++){
00080
00081 if (traj_in.points[pnt].positions.size() != generators_.size() ||
00082 traj_in.points[pnt+1].positions.size() != generators_.size()){
00083 ROS_ERROR("The point lists in the trajectory do not have the same size as the generators");
00084 return;
00085 }
00086
00087
00088 for (unsigned int i=0; i<generators_.size(); i++)
00089 generators_[i]->SetProfile(traj_in.points[pnt].positions[i], traj_in.points[pnt+1].positions[i]);
00090
00091
00092 double max_time = (traj_in.points[pnt+1].time_from_start - traj_in.points[pnt].time_from_start).toSec();
00093 for (unsigned int i=0; i<generators_.size(); i++)
00094 if (generators_[i]->Duration() > max_time)
00095 max_time = generators_[i]->Duration();
00096
00097
00098 for (unsigned int i=0; i<generators_.size(); i++)
00099 generators_[i]->SetProfileDuration(traj_in.points[pnt].positions[i], traj_in.points[pnt+1].positions[i], max_time);
00100
00101
00102 unsigned int steps = fmax(10, (unsigned int)(max_time / 0.1));
00103 double time = 0;
00104 for (unsigned int s=0; s<=steps; s++){
00105 for (unsigned int i=0; i<generators_.size(); i++){
00106 points_tmp.positions[i] = generators_[i]->Pos(time);
00107 points_tmp.velocities[i] = generators_[i]->Vel(time);
00108 points_tmp.accelerations[i] = generators_[i]->Acc(time);
00109 }
00110 points_tmp.time_from_start = ros::Duration(initial_time) + ros::Duration(time);
00111 traj_res.points.push_back(points_tmp);
00112 time += max_time/(double)steps;
00113 }
00114 initial_time += max_time;
00115 }
00116 traj_out = traj_res;
00117 }
00118
00119
00120
00121 }