Go to the documentation of this file.
36 #define MAX_ALLOWABLE_TIME 1.0e8
37 #define EPS_TRAJECTORY 1.0e-8
39 #define MAX_NUM_POINTS 1000
40 #define MAX_COEFF_SIZE 5
53 tp_.resize(num_points,dimension);
54 tc_.resize(num_points-1);
60 for(
int i=0;
i<num_points;
i++)
62 tp_[
i].setDimension(dimension);
65 for(
int i=0;
i<num_points-1;
i++)
67 tc_[
i].coeff_.resize(dimension);
68 for(
int j=0; j < dimension; j++)
71 for(
int i=0;
i < dimension;
i++)
91 ROS_WARN(
"Trying to set trajectory with number of points <= 0");
149 tp_[
i].qdot_[j] = 0.0;
167 ROS_ERROR(
"Index exceeds number of joints");
195 ROS_WARN(
"Number of points in vector specifying time (%d) does not match number of points %d",(
int) time.size(),
num_points_);
207 tp_[
i].time_ = time[
i];
219 int Trajectory::setTrajectory(
const std::vector<double> &p,
const std::vector<double> &pdot,
const std::vector<double> &time,
int numPoints)
227 ROS_WARN(
"Number of points in vector specifying time (%d) does not match number of points %d",(
int) time.size(),
num_points_);
239 tp_[
i].time_ = time[
i];
252 double time = tp.
time_;
255 std::vector<TPoint>::iterator it =
tp_.begin() + index;
265 while(time >
tp_[result+1].time_)
296 else if( time <
tp_.front().time_)
298 time =
tp_.front().time_;
304 ROS_WARN(
"Dimension of sample point passed in = %zd does not match dimension of trajectory = %d",tp.
q_.size(),
dimension_);
353 ROS_WARN(
"Input size: %zd does not match dimension of trajectory = %d",max_rate.size(),
dimension_);
369 ROS_WARN(
"Input size: %zd does not match dimension of trajectory = %d",max_acc.size(),
dimension_);
413 ROS_WARN(
"Trying to apply rate limits without setting max rate information. Use setMaxRate first");
424 tc_[
i-1].duration_ = dT;
428 temp[0] =
tp_[
i-1].q_[j];
432 tc_[
i-1].coeff_[j][0] = temp[0];
433 tc_[
i-1].coeff_[j][1] = temp[1];
434 tc_[
i-1].degree_ = 1;
460 ROS_WARN(
"Trying to apply rate limits without setting max rate information. Use setMaxRate first");
470 tc_[
i-1].duration_ = dT;
475 temp[0] =
tp_[
i-1].q_[j];
476 temp[1] =
tp_[
i-1].qdot_[j];
479 temp[2] = (3*diff-(2*
tp_[
i-1].qdot_[j]+
tp_[
i].qdot_[j])*
tc_[
i-1].duration_)/(
tc_[
i-1].duration_*
tc_[
i-1].duration_);
480 temp[3] = (-2*diff+(
tp_[
i-1].qdot_[j]+
tp_[
i].qdot_[j])*
tc_[
i-1].duration_)/(pow(
tc_[
i-1].duration_,3));
482 tc_[
i-1].coeff_[j][0] = temp[0];
483 tc_[
i-1].coeff_[j][1] = temp[1];
484 tc_[
i-1].coeff_[j][2] = temp[2];
485 tc_[
i-1].coeff_[j][3] = temp[3];
486 tc_[
i-1].degree_ = 1;
499 double dT(0),acc(0.0),tb(0.0);
513 ROS_WARN(
"Trying to apply rate and acc limits without setting them. Use setMaxRate and setMaxAcc first");
523 tc_[
i-1].duration_ = dT;
535 temp[0] =
tp_[
i-1].q_[j];
539 temp[4] = std::max(
tc_[
i-1].duration_-2*tb,0.0);
541 tc_[
i-1].coeff_[j][0] = temp[0];
542 tc_[
i-1].coeff_[j][1] = temp[1];
543 tc_[
i-1].coeff_[j][2] = temp[2];
544 tc_[
i-1].coeff_[j][3] = temp[3];
545 tc_[
i-1].coeff_[j][4] = temp[4];
546 tc_[
i-1].degree_ = 1;
557 double disc = (pow(bb,2) - 4*aa*cc);
561 double tb1 = (-bb + sqrt(disc))/(2*aa);
562 double tb2 = (-bb - sqrt(disc))/(2*aa);
567 return std::min(tb1,tb2);
573 double segment_time = time - segment_start_time;
593 double segment_time = time - segment_start_time;
596 double taccend = tc.
coeff_[
i][3];
599 double acc = tc.
coeff_[
i][2]*2;
602 if(segment_time <= taccend)
604 tp.
q_[
i] = tc.
coeff_[
i][0] + segment_time * v0 + 0.5 * segment_time * segment_time * acc;
607 else if(segment_time >= tvelend)
609 double dT = segment_time - tvelend;
610 tp.
q_[
i] = tc.
coeff_[
i][0] + v0 * taccend + 0.5 * acc * taccend * taccend + acc * taccend * tvel + acc * taccend * dT - 0.5 * acc * dT * dT;
611 tp.
qdot_[
i] = acc*taccend - acc*dT;
615 double dT = segment_time - taccend;
616 tp.
q_[
i] = tc.
coeff_[
i][0] + v0 * taccend + 0.5 * acc * taccend * taccend + acc * taccend * dT;
617 tp.
qdot_[
i] = acc * taccend;
630 double segment_time = time - segment_start_time;
633 tp.
q_[
i] = tc.
coeff_[
i][0] + segment_time * tc.
coeff_[
i][1] + segment_time*segment_time*tc.
coeff_[
i][2] + segment_time*segment_time*segment_time*tc.
coeff_[
i][3];
653 ROS_WARN(
"Size of duration vector %zd does not match number of segments in trajectory %d", duration.size(),
num_points_-1);
657 duration[
i] =
tc_[
i].duration_;
666 ROS_WARN(
"Index %d outside number of segments in the trajectory %d", index,
num_points_-1);
670 duration =
tc_[index].duration_;
680 ROS_WARN(
"Size of timestamps vector %zd does not match number of points in trajectory %d", timestamps.size(),
num_points_);
684 timestamps[
i] =
tp_[
i].time_;
694 for(
int i = 0;
i <
start.dimension_;
i++)
703 if(minTime < minJointTime)
704 minTime = minJointTime;
716 for(
int i = 0;
i <
start.dimension_;
i++)
725 if(minTime < minJointTime)
726 minTime = minJointTime;
744 double a = 3.0*(v0+v1)*v - 3.0* (v0+v1)*v0 + pow((2.0*v0+v1),2.0);
745 double b = -6.0*dq*v + 6.0 * v0 *dq - 6.0*dq*(2.0*v0+v1);
746 double c = 9.0 * pow(dq,2);
750 if((pow(b,2)-4.0*a*c) >= 0)
752 t1 = (-b + sqrt(pow(b,2)-4.0*a*c))/(2.0*a);
753 t2 = (-b - sqrt(pow(b,2)-4.0*a*c))/(2.0*a);
768 result = std::min(t1,t2);
777 for(
int i = 0;
i <
start.dimension_;
i++)
784 if(minTime < minJointTime)
785 minTime = minJointTime;
796 double tb = std::min(fabs(vmax/amax),sqrt(fabs(diff)/amax));
802 double dist_tb = acc*tb*tb;
803 double ts = (diff - dist_tb)/(acc*tb);
850 ROS_WARN(
"Trying to apply rate limits without setting max rate information. Use setMaxRate first.");
866 tc_[
i-1].duration_ = dT;
870 temp[0] =
tp_[
i-1].q_[j];
872 if(std::isnan(temp[1]))
878 tc_[
i-1].coeff_[j][0] = temp[0];
879 tc_[
i-1].coeff_[j][1] = temp[1];
880 tc_[
i-1].degree_ = 1;
921 ROS_WARN(
"Trying to apply rate limits without setting max rate information. Use setMaxRate first.");
938 tc_[
i-1].duration_ = dT;
943 temp[0] =
tp_[
i-1].q_[j];
944 temp[1] =
tp_[
i-1].qdot_[j];
945 temp[2] = (3*diff-(2*
tp_[
i-1].qdot_[j]+
tp_[
i].qdot_[j])*
tc_[
i-1].duration_)/(
tc_[
i-1].duration_*
tc_[
i-1].duration_);
946 temp[3] = (-2*diff+(
tp_[
i-1].qdot_[j]+
tp_[
i].qdot_[j])*
tc_[
i-1].duration_)/(pow(
tc_[
i-1].duration_,3));
947 if(std::isnan(temp[2]))
949 if(std::isnan(temp[3]))
952 tc_[
i-1].coeff_[j][0] = temp[0];
953 tc_[
i-1].coeff_[j][1] = temp[1];
954 tc_[
i-1].coeff_[j][2] = temp[2];
955 tc_[
i-1].coeff_[j][3] = temp[3];
956 tc_[
i-1].degree_ = 1;
974 double dT(0.0),acc(0.0),tb(0.0);
989 ROS_WARN(
"Trying to apply rate and acc limits without setting max rate or acc information. Use setMaxRate and setMaxAcc first.");
1006 tc_[
i-1].duration_ = dT;
1018 temp[0] =
tp_[
i-1].q_[j];
1022 temp[4] = std::max(
tc_[
i-1].duration_-2*tb,0.0);
1024 tc_[
i-1].coeff_[j][0] = temp[0];
1025 tc_[
i-1].coeff_[j][1] = temp[1];
1026 tc_[
i-1].coeff_[j][2] = temp[2];
1027 tc_[
i-1].coeff_[j][3] = temp[3];
1028 tc_[
i-1].coeff_[j][4] = temp[4];
1029 tc_[
i-1].degree_ = 1;
1048 FILE *
f = fopen(filename.c_str(),
"w");
1049 double time =
tp_.front().time_;
1056 fprintf(
f,
"%f ",time);
1059 fprintf(
f,
"%f ",tp.
q_[j]);
1063 fprintf(
f,
"%f ",tp.
qdot_[j]);
1075 double time =
tp_.front().time_;
int findTrajectorySegment(double time)
finds the trajectory segment corresponding to a particular time
double blendTime(double aa, double bb, double cc)
static double shortest_angular_distance(double from, double to)
int getNumberPoints()
Get the number of points in the trajectory.
int parameterizeBlendedLinear()
calculate the coefficients for interpolation between trajectory points using blended linear interpola...
double calculateMinTimeCubic(double q0, double q1, double v0, double v1, double vmax, int index)
std::vector< double > max_rate_
double getTotalTime()
Get the total time for the trajectory.
std::vector< bool > joint_wraps_
std::vector< TCoeff > tc_
void setInterpolationMethod(std::string interp_method)
Set the interpolation method.
def normalize_angle(angle)
double calculateMinTimeLSPB(double q0, double q1, double vmax, double amax, int index)
int minimizeSegmentTimesWithBlendedLinearInterpolation()
calculate a minimum time trajectory using blended linear interpolation Timings for the trajectory are...
std::vector< TPoint > tp_
int minimizeSegmentTimesWithLinearInterpolation()
calculate a minimum time trajectory using linear interpolation Timings for the trajectory are automat...
const TPoint & lastPoint()
void clear()
clear the trajectory
double jointDiff(double from, double to, int index)
#define MAX_ALLOWABLE_TIME
void addPoint(const TPoint)
Add a point to the trajectory.
std::vector< std::vector< double > > coeff_
void setDimension(int dimension)
Set the dimension of a trajectory point. This resizes the internal vectors to the right sizes.
int parameterize()
calculate the coefficients for interpolation between trajectory points If autocalc_timing_ is true,...
int write(std::string filename, double dT)
std::string interp_method_
void init(int num_points, int dimension)
void sampleLinear(TPoint &tp, double time, const TCoeff &tc, double segment_start_time)
Sample the trajectory based on a linear interpolation.
void sampleCubic(TPoint &tp, double time, const TCoeff &tc, double segment_start_time)
Sample the trajectory based on a cubic interpolation.
int getDuration(std::vector< double > &duration)
std::vector< double > min_limit_
void getTrajectory(std::vector< trajectory::Trajectory::TPoint > &traj, double dT)
int parameterizeCubic()
calculate the coefficients for interpolation between trajectory points using cubic interpolation....
double calculateMinimumTimeLinear(const TPoint &start, const TPoint &end)
calculate minimum time for a trajectory segment using a linear interpolation
int setMaxAcc(std::vector< double > max_acc)
int setTrajectory(const std::vector< TPoint > &tp)
Set the trajectory using a vector of trajectory points.
double calculateMinimumTimeLSPB(const TPoint &start, const TPoint &end)
calculate minimum time for a trajectory segment using LSPB.
int getTimeStamps(std::vector< double > ×tamps)
void setJointWraps(int index)
int minimizeSegmentTimesWithCubicInterpolation()
calculate a minimum time trajectory using cubic interpolation Timings for the trajectory are automati...
double calculateMinimumTimeCubic(const TPoint &start, const TPoint &end)
int parameterizeLinear()
calculate the coefficients for interpolation between trajectory points using linear interpolation If ...
void sampleBlendedLinear(TPoint &tp, double time, const TCoeff &tc, double segment_start_time)
Sample the trajectory based on a cubic interpolation.
int setMaxRates(std::vector< double > max_rate)
set the max rates (velocities)
std::vector< double > max_acc_
Trajectory(int dimension)
Constructor for instantiation of the class by specifying the dimension.
std::vector< double > qdot_
int sample(TPoint &tp, double time)
Sample the trajectory at a certain point in time.
std::vector< double > max_limit_
int minimizeSegmentTimes()
Minimize segment times in the trajectory Timings for the trajectory segments are automatically calcul...