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");
96 ROS_WARN(
"Dimension of trajectory point %d does not match dimension of trajectory %d",tp[0].
dimension_, dimension_);
148 tp_[i].q_[j] = p[i*dimension_+j];
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];
242 tp_[i].q_[j] = p[i*dimension_+j];
243 tp_[i].qdot_[j] = pdot[i*dimension_+j];
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");
421 tp_[i].time_ =
tp_[i-1].time_ + dT;
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");
468 tp_[i].time_ =
tp_[i-1].time_ + dT;
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");
521 tp_[i].time_ =
tp_[i-1].time_ + dT;
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];
598 double tvel = tc.
coeff_[i][4];
599 double acc = tc.
coeff_[i][2]*2;
600 double v0 = tc.
coeff_[i][1];
602 if(segment_time <= taccend)
604 tp.
q_[i] = tc.
coeff_[i][0] + segment_time * v0 + 0.5 * segment_time * segment_time * acc;
605 tp.
qdot_[i] = tc.
coeff_[i][1] + 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_;
699 minJointTime = fabs(diff) /
max_rate_[i];
703 if(minTime < minJointTime)
704 minTime = minJointTime;
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);
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.");
857 dT =
tp_[i].time_ -
tp_[i-1].time_;
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;
895 tp_[i].time_ =
tp_[i-1].time_ +
tc_[i-1].duration_;
921 ROS_WARN(
"Trying to apply rate limits without setting max rate information. Use setMaxRate first.");
929 dT =
tp_[i].time_ -
tp_[i-1].time_;
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;
966 tp_[i].time_ =
tp_[i-1].time_ +
tc_[i-1].duration_;
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.");
997 dT =
tp_[i].time_ -
tp_[i-1].time_;
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;
1041 tp_[i].time_ =
tp_[i-1].time_ +
tc_[i-1].duration_;
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_;
void clear()
clear the trajectory
double calculateMinimumTimeLSPB(const TPoint &start, const TPoint &end)
calculate minimum time for a trajectory segment using LSPB.
int parameterizeBlendedLinear()
calculate the coefficients for interpolation between trajectory points using blended linear interpola...
std::vector< bool > joint_wraps_
std::vector< double > max_rate_
double calculateMinTimeLSPB(double q0, double q1, double vmax, double amax, int index)
int minimizeSegmentTimes()
Minimize segment times in the trajectory Timings for the trajectory segments are automatically calcul...
int parameterize()
calculate the coefficients for interpolation between trajectory points If autocalc_timing_ is true...
void sampleCubic(TPoint &tp, double time, const TCoeff &tc, double segment_start_time)
Sample the trajectory based on a cubic interpolation.
double getTotalTime()
Get the total time for the trajectory.
void sampleBlendedLinear(TPoint &tp, double time, const TCoeff &tc, double segment_start_time)
Sample the trajectory based on a cubic interpolation.
double calculateMinimumTimeLinear(const TPoint &start, const TPoint &end)
calculate minimum time for a trajectory segment using a linear interpolation
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
int parameterizeCubic()
calculate the coefficients for interpolation between trajectory points using cubic interpolation...
int minimizeSegmentTimesWithCubicInterpolation()
calculate a minimum time trajectory using cubic interpolation Timings for the trajectory are automati...
int getDuration(std::vector< double > &duration)
int parameterizeLinear()
calculate the coefficients for interpolation between trajectory points using linear interpolation If ...
double calculateMinimumTimeCubic(const TPoint &start, const TPoint &end)
int write(std::string filename, double dT)
void sampleLinear(TPoint &tp, double time, const TCoeff &tc, double segment_start_time)
Sample the trajectory based on a linear interpolation.
void addPoint(const TPoint)
Add a point to the trajectory.
Trajectory(int dimension)
Constructor for instantiation of the class by specifying the dimension.
double blendTime(double aa, double bb, double cc)
std::vector< std::vector< double > > coeff_
void init(int num_points, int dimension)
std::vector< TCoeff > tc_
int minimizeSegmentTimesWithLinearInterpolation()
calculate a minimum time trajectory using linear interpolation Timings for the trajectory are automat...
double calculateMinTimeCubic(double q0, double q1, double v0, double v1, double vmax, int index)
void setDimension(int dimension)
Set the dimension of a trajectory point. This resizes the internal vectors to the right sizes...
int getNumberPoints()
Get the number of points in the trajectory.
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
def normalize_angle(angle)
void setJointWraps(int index)
double jointDiff(double from, double to, int index)
std::vector< double > max_limit_
std::vector< double > min_limit_
#define MAX_ALLOWABLE_TIME
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
int getTimeStamps(std::vector< double > ×tamps)
std::vector< double > max_acc_
const TPoint & lastPoint()
int sample(TPoint &tp, double time)
Sample the trajectory at a certain point in time.
int findTrajectorySegment(double time)
finds the trajectory segment corresponding to a particular time
std::string interp_method_
void getTrajectory(std::vector< trajectory::Trajectory::TPoint > &traj, double dT)
std::vector< double > qdot_
std::vector< TPoint > tp_
int setMaxAcc(std::vector< double > max_acc)
void setInterpolationMethod(std::string interp_method)
Set the interpolation method.
int minimizeSegmentTimesWithBlendedLinearInterpolation()
calculate a minimum time trajectory using blended linear interpolation Timings for the trajectory are...
def shortest_angular_distance(from_angle, to_angle)
int setMaxRates(std::vector< double > max_rate)
set the max rates (velocities)
int setTrajectory(const std::vector< TPoint > &tp)
Set the trajectory using a vector of trajectory points.