From f1cbb3171cdf9b5c2292510223a537bd1a1e09ee Mon Sep 17 00:00:00 2001 From: saurabhkgpee Date: Sun, 13 Aug 2017 02:44:11 +0530 Subject: [PATCH] RRT Connect --- include/RRT.hpp | 58 ++++++-- include/RRTConnect.hpp | 331 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 38 +++-- 3 files changed, 406 insertions(+), 21 deletions(-) create mode 100644 include/RRTConnect.hpp diff --git a/include/RRT.hpp b/include/RRT.hpp index e718bac..df00797 100644 --- a/include/RRT.hpp +++ b/include/RRT.hpp @@ -19,19 +19,19 @@ namespace rrt double stepLength; std::vector > pathPoints; int maxIterations; - std::vector< std::pair< Utils::Point, Utils::Point > > tree; + std::vector< std::pair< Utils::Point, Utils::Point > > tree; unsigned int biasParameter; - + public: RRT(){}; RRT(Utils::Point start,Utils::Point end) { - //srand(time(NULL)); + //srand(time(NULL)); startPoint=start; endPoint=end; } - + virtual bool plan(); virtual std::vector > getPointsOnPath(); @@ -59,13 +59,51 @@ namespace rrt void generatePath(Utils::Point first,Utils::Point last); double dist(Utils::Point a,Utils::Point b); }; + + template + class RRTConnect: public RRT{ + + public: + bool plan(); + std::vector > getPointsOnPath(); + + void setEndPoints(Utils::Point start, Utils::Point end); + void setCheckPointFunction(bool (*f)(Utils::Point)); + void setStepLength(double value); + void setOrigin(Utils::Point origin); + void setHalfDimensions(double x,double y); + void setBiasParameter(unsigned int); + void setMaxIterations(int); + private: + double halfDimensionX; + double halfDimensionY; + Utils::Point origin; + Utils::Point connectA; + Utils::Point connectB; + Utils::Point startPoint; + Utils::Point endPoint; + double stepLength; + std::vector > pathPoints; + int maxIterations; + std::vector< std::pair< Utils::Point, Utils::Point > > treeA; + std::vector< std::pair< Utils::Point, Utils::Point > > treeB; + std::vector< std::pair< Utils::Point, Utils::Point > > mainTree; + std::vector< std::pair< Utils::Point, Utils::Point > > supportTree; + + unsigned int biasParameter; + bool (*userCheck)(Utils::Point); + bool checkPoint(Utils::Point pt); + Utils::Point generatePoint(std::vector< std::pair< Utils::Point, Utils::Point > >); + Utils::Point generateBiasedPoint(Utils::Point ,std::vector< std::pair< Utils::Point, Utils::Point > >); + Utils::Point findClosestNode(Utils::Point,std::vector< std::pair< Utils::Point, Utils::Point > > tree); + void growTree(Utils::Point); + Utils::Point getParent(Utils::Point,std::vector< std::pair< Utils::Point, Utils::Point > > tree); + bool treeComplete(); + void generatePath(Utils::Point first,Utils::Point last); + double dist(Utils::Point a,Utils::Point b); + }; } -//************* ToDo ************* -// Implement derived classes for variants of RRT -// Optimize the generate path and other -// Tweak with the parameters to check their effects on runtime and path -// Implement Pruning function to keep a check on size of tree -#endif +#endif \ No newline at end of file diff --git a/include/RRTConnect.hpp b/include/RRTConnect.hpp new file mode 100644 index 0000000..b5ca20d --- /dev/null +++ b/include/RRTConnect.hpp @@ -0,0 +1,331 @@ +#include +#include "./RRT.hpp" +#include "./RRT_implementation.hpp" +#include "./utils.hpp" + +using namespace std; +namespace rrt +{ + template + void RRTConnect::setEndPoints(Utils::Point start, Utils::Point end) + { + startPoint=start; + endPoint=end; + } + + template + void RRTConnect::setHalfDimensions(double a, double b) + { + halfDimensionX=a; + halfDimensionY=b; + } + + template + void RRTConnect::setOrigin(Utils::Point pt) + { + origin=pt; + } + + template + std::vector > RRTConnect::getPointsOnPath() + { + return pathPoints; + } + + template + void RRTConnect::setStepLength(double value) + { + stepLength=value; + } + + template + void RRTConnect::setMaxIterations(int value) + { + maxIterations=value; + } + + template + bool RRTConnect:: plan() + { + int count=0; + int check=0; + treeA.push_back(std::pair< Utils::Point, Utils::Point > (startPoint,startPoint)); + treeB.push_back(std::pair< Utils::Point, Utils::Point > (endPoint,endPoint)); + + int start_time=clock(); + while( check < maxIterations) + { + //std::cout<<"In Planning Loop"< next; + + + if (check%2 == 0) + { + mainTree = treeA; + supportTree = treeB; + } + else{ + mainTree = treeB; + supportTree = treeA; + } + + if(treeComplete()==true) + { + std::cout<<"Tree complete!!"< + + void RRTConnect::growTree(Utils::Point temp) + { + //growing the tree by adding node + //std::cout<<"finding parent in tree of size = "< closest; + closest = findClosestNode(temp,treeA); + double theta = atan2(temp.y-closest.y,temp.x-closest.x); + double dis = dist(temp,closest); + if (dis > stepLength) + { + temp.x=closest.x+stepLength*cos(theta); + temp.y=closest.y+stepLength*sin(theta); + } + treeA.push_back( std::pair< Utils::Point, Utils::Point > (temp,closest)); + + closest = findClosestNode(temp,treeB); + theta = atan2(temp.y - closest.y , temp.x - closest.x); + dis = dist(temp,closest); + if (dis > stepLength) + { + temp.x=closest.x+stepLength*cos(theta); + temp.y=closest.y+stepLength*sin(theta); + } + treeB.push_back( std::pair< Utils::Point, Utils::Point > (temp,closest)); + //std::cout<<"Point Generated = "< + void RRTConnect::setCheckPointFunction(bool (*f)(Utils::Point)) + { + userCheck=f; + } + + template + Utils::Point RRTConnect::findClosestNode(Utils::Point cur,std::vector< std::pair< Utils::Point, Utils::Point > >tree) + { + double min_dist=INT_MAX; + Utils::Point closest; + for(int i=0;i + Utils::Point RRTConnect::generatePoint(std::vector< std::pair< Utils::Point, Utils::Point > > tree) + { + Utils::Point temp; + int signX,signY; + + if(rand()%2==0) + signX=-1; + else + signX=1; + if(rand()%2==0) + signY=-1; + else + signY=1; + temp.x = origin.x + signX* ( rand() % (int)halfDimensionX ); + temp.y = origin.y + signY* ( rand() % (int)halfDimensionY ); + + + return temp; + Utils::Point closest=findClosestNode(temp,tree); + double theta = atan2(temp.y-closest.y,temp.x-closest.x); + double dis = dist(temp,closest); + if (dis < stepLength) + { + return temp; + } + Utils::Point next; + + next.x=closest.x+stepLength*cos(theta); + next.y=closest.y+stepLength*sin(theta); + // std::cout<<"origin,"< + Utils::Point RRTConnect::generateBiasedPoint(Utils::Point endPoint,std::vector< std::pair< Utils::Point, Utils::Point > >tree) + { + Utils::Point closest=findClosestNode(endPoint,tree); + double theta = atan2(endPoint.y-closest.y,endPoint.x-closest.x); + + Utils::Point next; + next.x=closest.x+stepLength*cos(theta); + next.y=closest.y+stepLength*sin(theta); + //std::cout<<"Biased Point Generated = "< + bool RRTConnect::checkPoint(Utils::Point next) + { + return userCheck(next); + } + + template + bool RRTConnect::treeComplete() + { + std::vector< std::pair< Utils::Point, Utils::Point > > conn; + //std::cout<<"Checking if tree is complete? "<, Utils::Point > (connectA,connectB)); + // std::cout<<"distance = "< + void RRTConnect::setBiasParameter(unsigned int param) + { + biasParameter=param; + } + + template + void RRTConnect::generatePath(Utils::Point first,Utils::Point last) + { + + Utils::Point cur=connectA; + while(cur!=startPoint){ + pathPoints.insert(pathPoints.begin(),cur); + cur = getParent(cur,treeA); + } + + pathPoints.insert(pathPoints.begin(),cur); + + cur = connectB; + + // std::cout< parent= getParent(cur); + // std::cout<<"current : "< + Utils::Point RRTConnect::getParent(Utils::Point cur,std::vector< std::pair< Utils::Point, Utils::Point > >tree) + { + for(int i=0;i + double RRTConnect::dist(Utils::Point a,Utils::Point b) + { + return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); + } +} + diff --git a/src/main.cpp b/src/main.cpp index 8e5a1d1..0e712f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,18 @@ #include -#include "./RRT_implementation.hpp" +#include "./../include/RRTConnect.hpp" using namespace std; using namespace rrt; bool check(Utils::Point cur) { - if(abs(cur.x)<2000 && abs(cur.y)<2000) + if(abs(cur.x)<2000 && abs(cur.y)<2000){ + // if (cur.x < 550 && cur.x > 450 && cur.y < 550 & cur.y > 450) + // { + // return false; + // } return true; + } return false; } @@ -15,24 +20,35 @@ int main() { srand(time(NULL)); Utils::Point start,finish,origin; - start.x=-10; - start.y=10; - finish.x=1000; - finish.y=730; + start.x=30; + start.y=30; + finish.x=1500; + finish.y=1730; origin.x=0; origin.y=0; - RRT test; + RRTConnect test; test.setEndPoints(start,finish); test.setCheckPointFunction(*(check)); - test.setStepLength(200); + test.setStepLength(50); test.setHalfDimensions(2000.0,2000.0); test.setBiasParameter(100); test.setOrigin(origin); test.setMaxIterations(10000); test.plan(); - cout<<"#################################################"< > path=test.getPointsOnPath(); + cout<<"#################################################"<