00001 #ifndef __TRGEN_H
00002 #define __TRGEN_H
00003
00019 #ifdef MATLAB_MEX_FILE
00020 #include <mex.h>
00021 #endif
00022
00023 #include <list>
00024 #include <math.h>
00025
00026
00031 struct TrajectoryConstraints {
00032 double maxv;
00033 double maxomega;
00034 double maxangacc;
00035 double maxacc;
00036 double maxcenacc;
00037 double maxe;
00038 };
00039
00043 class Pos {
00044 public:
00045 double x, y, phi;
00046 double v, omega;
00047 Pos() : x(0), y(0), phi(0), v(0), omega(0) {};
00048 };
00049
00054 class Point {
00055 public:
00056 double x, y;
00057 Point() : x(0), y(0) {};
00058 Point(double _x, double _y) : x(_x), y(_y) {};
00059
00060 double distanceTo(class Point &wp) {
00061 double dx = wp.x-x, dy = wp.y-y;
00062 return sqrt(dx*dx + dy*dy);
00063 }
00064
00065 double angleTo(class Point &wp) {
00066 double dx = wp.x-x, dy = wp.y-y;
00067 return atan2(dy,dx);
00068 }
00069
00070 bool operator==(Point &o) {
00071 return (x == o.x && y == o.y);
00072 }
00073
00074 bool operator!=(Point &o) {
00075 return !(*this == o);
00076 }
00077 };
00078
00083 class TrajectoryPoints : public std::list<Point*> {
00084 public:
00085 ~TrajectoryPoints() {
00086 for (iterator wp = begin(); wp != end(); wp++) {
00087 delete(*wp);
00088 }
00089 clear();
00090 }
00091 };
00092
00093
00098 class TrajectorySegment {
00099 protected:
00102 double t1, t2;
00103 double acc;
00104 public:
00105
00106 double maxv;
00107 double v1, v2;
00108
00109 virtual void setMaxV(const TrajectoryConstraints &constr) = 0;
00110 virtual double getMaxAcc(const TrajectoryConstraints &constr) const { return constr.maxacc; };
00111 virtual bool isTurn() const { return false; };
00112
00114 double s, r, alphahalf;
00115
00116 TrajectorySegment() : s(0), r(0), alphahalf(0){};
00117 virtual ~TrajectorySegment() {};
00118
00119 virtual double getLength() const = 0;
00125 virtual double getUnifiedLength() const {
00126 return getLength();
00127 }
00135 virtual void getPointAt(double distance, Point *p) = 0;
00142 virtual void shortenBy(double distance, Point *newEnd) = 0;
00152 virtual TrajectorySegment* splitAt(double distance, Point *newEnd) = 0;
00153
00164 virtual double startAt(double time) {
00165 t1 = time;
00166 t2 = time + getUnifiedLength()/((v1+v2)/2.0);
00167 acc = (v2-v1)/(t2-t1);
00168 return t2;
00169 }
00176 virtual void getRefPos(double time, Pos &rp) = 0;
00188 int containsTime(double t) {
00189 if (t >= t1 && t <= t2)
00190 return 0;
00191 else if (t >= t1)
00192 return 1;
00193 else
00194 return -1;
00195 }
00196 double getT1() { return t1; }
00197 double getT2() { return t2; }
00198 #ifdef MATLAB_MEX_FILE
00199 virtual void plot(const char *style) = 0;
00200 void plotSpeed(const char *style) {
00201 char cmd[300];
00202 sprintf(cmd, "plot([%g %g], [%g %g], '%s')",
00203 t1, t2, v1, v2, style);
00204 mexEvalString(cmd);
00205
00206 }
00207 #endif
00208 };
00209
00210
00221 class Trajectory : public std::list<TrajectorySegment*> {
00222 Pos initPos;
00223 iterator currentSeg;
00224 TrajectoryPoints wayPoints;
00225 Point *initialPoint, *finalPoint;
00226 TrajectoryConstraints constr;
00227 bool prepared;
00228
00229 void deleteSegments() {
00230 for (iterator seg = begin(); seg != end(); seg++) {
00231 delete(*seg);
00232 }
00233 clear();
00234 }
00235
00236 bool splitSegment(iterator &seg, double distance);
00237 bool points2segments();
00238 void corners2arcs();
00239 void addTurns();
00240 void calcSpeeds();
00241
00242 void getSegRefPos(TrajectorySegment *seg, double time, Pos &rp);
00243
00244 public:
00247 double finalHeading;
00248 bool backward;
00249
00256 Trajectory(TrajectoryConstraints tc, bool _backward = false) :
00257 wayPoints(), initialPoint(0), finalPoint(0), constr(tc),
00258 prepared(false), finalHeading(NAN), backward(_backward) {};
00259
00260 ~Trajectory() {
00261 deleteSegments();
00262 }
00263
00270 void addPoint(double x, double y) {
00271 finalPoint = new Point(x,y);
00272 wayPoints.push_back(finalPoint);
00273 }
00274 void addTurn(double angle);
00275
00276 double calcLength();
00278 bool prepare(Pos _initPos);
00279 bool getRefPos(double time, Pos &rp);
00280 #ifdef MATLAB_MEX_FILE
00281 void plot(const char *style);
00282 void plotSpeed(const char *style);
00283 #endif
00284 };
00285
00287
00288 #endif