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 #ifndef __PATH_PLANNER_
00028 #define __PATH_PLANNER_
00029
00035
00036
00037 #ifdef MIP_HOST_APPLE
00038 #include <applePatch.h>
00039 #include <stdlib.h>
00040 #endif
00041
00042
00043 #include <vector>
00044 #include <Spaces.h>
00045 #include <assert.h>
00046 #include <math.h>
00047 #include <Scan.h>
00048 #include <TrajControl.h>
00049 #include <ExplorationGraph.h>
00050
00051 #include <algorithm>
00052
00053 #include <Types.h>
00054
00055
00056 using namespace std;
00057 using namespace MipBaselib;
00058
00060 enum PathPlanners{
00061 RRT_PLANNER,
00062 PATH_PLANNERS_NUM
00063 };
00064
00066 static const char* PathPlannersNames[PATH_PLANNERS_NUM] = {
00067 "RRT Planner"
00068 };
00069
00071 enum PrimitivesType{
00072 STRAIGHT,
00073 TURN_LEFT,
00074 TURN_RIGHT,
00075 NULL_PRIMITIVE,
00076 PRIMITIVES_NUM
00077 };
00078
00080 static const char* PrimitivesNames[PRIMITIVES_NUM] = {
00081 "Straight",
00082 "Turn Left",
00083 "Turn Right",
00084 "Null Primitive"
00085 };
00086
00088 enum RRTstate{
00089 FIND_Q_RAND,
00090 FIND_Q_NEAR,
00091 FIND_Q_NEW,
00092 COLLISION_CHECK,
00093 UPDATE,
00094 GOAL_REACHED,
00095 FIND_PATH,
00096 SEARCH_COMPLETE,
00097 STATE_NUM
00098 };
00099
00101 static const char* RRTstateNames[STATE_NUM] = {
00102 "Find qRand",
00103 "Find qNear",
00104 "Find qNew",
00105 "Collision check",
00106 "Update",
00107 "Goal reached",
00108 "Find Path",
00109 "Search Complete"
00110 };
00111
00112 class RRTnode{
00113 private:
00114 public:
00115 int _ID;
00116 Pose _q;
00117 vector<int> _freePrimitives;
00118 int _freePrimitivesNum;
00119 int _parentID;
00120 int _edgeType;
00121 Decimal _CSpaceDist;
00122 Cell* _cell;
00123 Segment _straight;
00124 Ellipse _left;
00125 Ellipse _right;
00126
00127 RRTnode(){
00128 _parentID=-1;
00129 _freePrimitives=vector<int>(3,1);
00130 _freePrimitivesNum=3;
00131 }
00132
00133 RRTnode(int ID,Pose q,int maxIter){
00134 _ID=ID;
00135 _q=q;
00136 _parentID=-1;
00137 _freePrimitives=vector<int>(3,1);
00138 _freePrimitivesNum=3;
00139 }
00140
00141
00142 RRTnode(int maxIter){
00143 _q=Pose(INFINITY,INFINITY,0.0);
00144 _parentID=-1;
00145 _freePrimitives=vector<int>(3,1);
00146 _freePrimitivesNum=3;
00147 }
00148
00150 RRTnode(const RRTnode& n){
00151 _ID=n._ID;
00152 _q=n._q;
00153 _freePrimitives=n._freePrimitives;
00154 _freePrimitivesNum=n._freePrimitivesNum;
00155 _parentID=n._parentID;
00156 _straight=n._straight;
00157 _left=n._left;
00158 _right=n._right;
00159 _CSpaceDist=n._CSpaceDist;
00160 _cell=n._cell;
00161 _edgeType=n._edgeType;
00162 }
00164 RRTnode& operator=(const RRTnode& n){
00165 if (this != &n){
00166 _ID=n._ID;
00167 _q=n._q;
00168 _freePrimitives=n._freePrimitives;
00169 _freePrimitivesNum=n._freePrimitivesNum;
00170 _parentID=n._parentID;
00171 _straight=n._straight;
00172 _left=n._left;
00173 _right=n._right;
00174 _CSpaceDist=n._CSpaceDist;
00175 _cell=n._cell;
00176 _edgeType=n._edgeType;
00177 }
00178 return *this;
00179 }
00180
00181
00182
00184 int ID(){
00185 return _ID;
00186 };
00187
00189 Pose q(){
00190 return _q;
00191 };
00192
00194 vector<int> freePrimitives(){
00195 return _freePrimitives;
00196 };
00197
00199 int freePrimitivesNum(){
00200 return _freePrimitivesNum;
00201 };
00202
00204 int parentID(){
00205 return _parentID;
00206 };
00207
00209 int edgeType(){
00210 return _edgeType;
00211 };
00212
00214 Decimal CSpaceDist(){
00215 return _CSpaceDist;
00216 };
00217
00219 Cell* cell(){
00220 return _cell;
00221 };
00222
00223
00224
00226 Segment straight(){
00227 return _straight;
00228 };
00229
00231 Ellipse left(){
00232 return _left;
00233 };
00234
00236 Ellipse right(){
00237 return _right;
00238 };
00239
00241 string print(){
00242 stringstream s;
00243 s.precision(3);
00244 s.setf(ios::fixed,ios::floatfield);
00245 s<<"ID\t\t"<<_ID<<endl;
00246 s<<"q\t\t"<<_q.print()<<endl;
00247 s<<"parent ID\t\t"<<_parentID<<endl;
00248 s<<"CSpaceDist\t\t"<<_CSpaceDist<<endl;
00249 return s.str();
00250 }
00251
00252
00253
00255 void setID(int value){
00256 _ID=value;
00257 };
00258
00260 void setQ(Pose value){
00261 _q=value;
00262 };
00263
00265 void setFreePrimitives(vector<int> value){
00266 _freePrimitives=value;
00267 };
00268
00270 void clearFreePrimitivesElem(int value){
00271 _freePrimitives[value]=0;
00272 };
00273
00275 void setFreePrimitivesNum(int value){
00276 _freePrimitivesNum=value;
00277 };
00278
00280 void decreaseFreePrimitivesNum(){
00281 _freePrimitivesNum--;
00282 };
00283
00285 void setParentID(int value){
00286 _parentID=value;
00287 };
00288
00290 void setEdgeType(int value){
00291 _edgeType=value;
00292 };
00293
00295 void setCSpaceDist(Decimal value){
00296 _CSpaceDist=value;
00297 };
00298
00300 void setCell(Cell* value){
00301 _cell=value;
00302 };
00303
00305 void setStraight(Segment value){
00306 _straight=value;
00307 };
00308
00310 void setLeft(Ellipse value){
00311 _left=value;
00312 };
00313
00315 void setRight(Ellipse value){
00316 _right=value;
00317 };
00318 };
00319
00326 class RRTparams{
00327 private:
00328 int _maxIterations;
00329 Decimal _deltaT;
00330 Decimal _threshold;
00331 Decimal _probBiasedExpansion;
00332 vector<VelVec> _motionPrimitives;
00333 public:
00335 RRTparams(){}
00336
00338 RRTparams(int maxIterations,Decimal delta,Decimal threshold,Decimal probBiasedExpansion){
00339 setMaxIterations(maxIterations);
00340 setDeltaT(delta);
00341 setThreshold(threshold);
00342 setProbBiasedExpansion(probBiasedExpansion);
00343 VelVec straight=VelVec(0.1,0.0,Time(0,0));
00344 _motionPrimitives.push_back(straight);
00345 VelVec turnLeft=VelVec(0.1,0.2,Time(0,0));
00346 _motionPrimitives.push_back(turnLeft);
00347 VelVec turnRight=VelVec(0.1,-0.2,Time(0,0));
00348 _motionPrimitives.push_back(turnRight);
00349
00350 }
00351
00353 RRTparams(const RRTparams& params){
00354 _maxIterations=params._maxIterations;
00355 _deltaT=params._deltaT;
00356 _threshold=params._threshold;
00357 _probBiasedExpansion=params._probBiasedExpansion;
00358 _motionPrimitives=params._motionPrimitives;
00359 }
00361 RRTparams& operator=(const RRTparams& params){
00362 if (this != ¶ms){
00363 _maxIterations=params._maxIterations;
00364 _deltaT=params._deltaT;
00365 _threshold=params._threshold;
00366 _probBiasedExpansion=params._probBiasedExpansion;
00367 _motionPrimitives=params._motionPrimitives;
00368 }
00369 return *this;
00370 }
00371
00372
00373
00374
00375
00376
00377
00379 int maxIterations(){
00380 return _maxIterations;
00381 };
00382
00384 Decimal deltaT(){
00385 return _deltaT;
00386 };
00387
00389 Decimal threshold(){
00390 return _threshold;
00391 };
00392
00394 Decimal probBiasedExpansion (){
00395 return _probBiasedExpansion;
00396 };
00397
00399 vector<VelVec> motionPrimitives(){
00400 return _motionPrimitives;
00401 };
00402
00403
00404
00405
00407 void setMaxIterations(int value){
00408 _maxIterations=value;
00409 };
00410
00412 void setDeltaT(Decimal value){
00413 _deltaT=value;
00414 };
00415
00417 void setThreshold(Decimal value){
00418 _threshold=value;
00419 };
00420
00422 void setProbBiasedExpansion (Decimal value){
00423 _probBiasedExpansion=value;
00424 };
00425
00427 void setMotionPrimitives(vector<VelVec> value){
00428 _motionPrimitives=value;
00429 };
00430 };
00431
00432
00439 class RRT{
00440 private:
00441 public:
00442 RRTparams _params;
00443 Pose _qStart;
00444 Pose _qGoal;
00445 Pose _qRand;
00446 Pose _qNear;
00447
00448 RRTnode _tempNode;
00449
00450
00451 ExplorationNode* _expNode;
00452 vector<RRTnode> _tree;
00453 vector<int> _sortedByDistance;
00454 Decimal radius;
00455 Decimal lenght;
00456 Timer timer;
00457 Cell* _goalCell;
00458
00459 int _nearest;
00460 Pose _nearestPose;
00461 int selPrim;
00462
00463
00464 vector<int> _pathNodes;
00465
00467 RRT(){}
00468
00469
00470
00471 RRT(RRTparams params){
00472 setParams(params);
00473 }
00474
00476 RRT(RRTparams params,Pose qStart,Pose qGoal,ExplorationNode* expNode){
00477 setParams(params);
00478 setQStart(qStart);
00479 setQGoal(qGoal);
00480 setExpNode(expNode);
00481
00482 _tree.reserve(_params.maxIterations());
00483
00484
00485 initializeTree();
00486
00487
00488 lenght=0.1;
00489 radius=0.1*sqrt(2.0);
00490 _goalCell=_expNode->grid().getCell(_qGoal.pos());
00491 _nearest=0;
00492 }
00493
00494
00495
00497 RRT(const RRT& rrt){
00498 _params=rrt._params;
00499 _qStart=rrt._qStart;
00500 _qGoal=rrt._qGoal;
00501 _qRand=rrt._qRand;
00502 _qNear=rrt._qNear;
00503 _tempNode=rrt._tempNode;
00504 _expNode=rrt._expNode;
00505 _tree=rrt._tree;
00506 _sortedByDistance=rrt._sortedByDistance;
00507 radius=rrt.radius;
00508 lenght=rrt.lenght;
00509 timer=rrt.timer;
00510 _goalCell=rrt._goalCell;
00511 _nearest=rrt._nearest;
00512 selPrim=rrt.selPrim;
00513 _pathNodes=rrt._pathNodes;
00514 }
00516 RRT& operator=(const RRT& rrt){
00517 if (this != &rrt){
00518 _params=rrt._params;
00519 _qStart=rrt._qStart;
00520 _qGoal=rrt._qGoal;
00521 _qRand=rrt._qRand;
00522 _qNear=rrt._qNear;
00523 _tempNode=rrt._tempNode;
00524 _expNode=rrt._expNode;
00525 _tree=rrt._tree;
00526 _sortedByDistance=rrt._sortedByDistance;
00527 radius=rrt.radius;
00528 lenght=rrt.lenght;
00529 timer=rrt.timer;
00530 _goalCell=rrt._goalCell;
00531 _nearest=rrt._nearest;
00532 selPrim=rrt.selPrim;
00533 _pathNodes=rrt._pathNodes;
00534 }
00535 return *this;
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00549 void initializeTree();
00550
00552 void findQrand();
00553
00555 Decimal CSpaceDist(Pose q1, Pose q2);
00556
00558 void findQnear();
00559
00560
00561
00562 PrimitivesType findQnew();
00563
00564 bool isCollisionFree(RRTnode node,Grid* grid);
00565
00566 void update();
00567
00569 bool buildRRT(vector<Path*> &path, int &complIter);
00570
00572 bool buildRRT2(vector<Path*> &path, vector<Path*> &tree, int &complIter);
00573
00574
00575
00576
00577
00579 RRTparams params(){
00580 return _params;
00581 };
00582
00584 Pose qStart(){
00585 return _qStart;
00586 };
00587
00589 Pose qGoal(){
00590 return _qGoal;
00591 };
00592
00594 Pose qRand(){
00595 return _qRand;
00596 };
00597
00599 ExplorationNode* expNode(){
00600 return _expNode;
00601 };
00602
00603
00604
00605
00607 void setParams(RRTparams value){
00608 _params=value;
00609 };
00610
00612 void setQStart(Pose value){
00613 _qStart=value;
00614 };
00615
00617 void setQGoal(Pose value){
00618 _qGoal=value;
00619 };
00620
00622 void setQRand(Pose value){
00623 _qRand=value;
00624 };
00625
00627 void setExpNode (ExplorationNode* value){
00628 _expNode=value;
00629 };
00630
00632 string print(){
00633 stringstream s;
00634 s.precision(3);
00635 s.setf(ios::fixed,ios::floatfield);
00636 unsigned int treeSize=_tree.size();
00637 for(unsigned int i=0;i<treeSize;i++){
00638 s<<_tree[i].print()<<endl;
00639 }
00640 return s.str();
00641 }
00642
00643 };
00644
00645
00646
00647
00648
00649
00653 class RTR{
00654 public:
00656 RTR();
00657
00663 bool buildRTR(Angle &rotation, Segment &segment, Pose start, Pose goal);
00664 };
00665
00666
00667
00668 #endif
00669
00670