Go to the documentation of this file.00001 #include <iostream>
00002 #include <cstdio>
00003 #include "Roadmap.h"
00004 #include "RoadmapNode.h"
00005 #include "ConfigurationSpace.h"
00006 #include "PRM.h"
00007
00008 using namespace PathEngine;
00009
00010 PRM::PRM(PathPlanner* path)
00011 : Algorithm(path)
00012 {
00013
00014 properties_["max-dist"] = "1.0";
00015 properties_["max-points"] = "100";
00016 }
00017
00018 PRM::~PRM() {
00019 }
00020
00021 bool PRM::buildRoadmap()
00022 {
00023 std::cerr << "new Roadmap is created" << std::endl;
00024
00025
00026 unsigned long numPoints = 0, numTotalPoints = 0;
00027 ConfigurationSpace *cspace = planner_->getConfigurationSpace();
00028
00029 while (numPoints < maxPoints_) {
00030 if (!isRunning_) {
00031 return false;
00032 }
00033
00034
00035 Configuration pos = cspace->random();
00036 numTotalPoints++;
00037
00038
00039 if (!planner_->checkCollision(pos)) {
00040 RoadmapNodePtr node = RoadmapNodePtr(new RoadmapNode(pos));
00041 roadmap_->addNode(node);
00042 numPoints++;
00043 }
00044 printf("creating nodes, registered : %ld / tested : %ld\r", numPoints, numTotalPoints);
00045 }
00046 printf("\n");
00047
00048
00049 Mobility* mobility = planner_->getMobility();
00050 RoadmapNodePtr from, to;
00051 unsigned int n = roadmap_->nNodes();
00052 for (unsigned long i=0; i<n; i++) {
00053 if (!isRunning_) {
00054 return false;
00055 }
00056 from = roadmap_->node(i);
00057 for (unsigned long j=i+1; j<n; j++) {
00058 to = roadmap_->node(j);
00059 if (mobility->distance(from->position(), to->position()) < maxDist_) {
00060 roadmap_->tryConnection(from, to);
00061 }
00062 }
00063 }
00064
00065 return true;
00066 }
00067
00068 bool PRM::calcPath()
00069 {
00070 std::cout << "PRM::calcPath()" << std::endl;
00071
00072 maxDist_ = atof(properties_["max-dist"].c_str());
00073
00074
00075 maxPoints_ = atoi(properties_["max-points"].c_str());
00076
00077 std::cerr << "maxDist:" << maxDist_ << std::endl;
00078 std::cerr << "maxPoints:" << maxPoints_ << std::endl;
00079
00080 if (roadmap_->nNodes() == 0) buildRoadmap();
00081
00082
00083 Mobility *mobility = planner_->getMobility();
00084 RoadmapNodePtr startNode = RoadmapNodePtr(new RoadmapNode(start_));
00085 RoadmapNodePtr goalNode = RoadmapNodePtr(new RoadmapNode(goal_));
00086 roadmap_->addNode(startNode);
00087 roadmap_->addNode(goalNode);
00088
00089 RoadmapNodePtr node;
00090 for (unsigned long i=0; i<roadmap_->nNodes(); i++) {
00091 node = roadmap_->node(i);
00092 const Configuration& pos = node->position();
00093 if (mobility->distance(start_, pos) < maxDist_) {
00094 roadmap_->tryConnection(startNode, node);
00095 }
00096 if (mobility->distance(goal_, pos) < maxDist_) {
00097 roadmap_->tryConnection(goalNode, node);
00098 }
00099 }
00100
00101 std::cout << "start node has " << startNode->nChildren()
00102 << " childrens" << std::endl;
00103 std::cout << "goal node has " << goalNode->nParents()
00104 << " parents" << std::endl;
00105
00106 std::vector<RoadmapNodePtr> nodePath;
00107 nodePath = roadmap_->DFS(startNode, goalNode);
00108 for (unsigned int i=0; i<nodePath.size(); i++){
00109 path_.push_back(nodePath[i]->position());
00110 }
00111
00112 std::cout << "path_.size() = " << path_.size() << std::endl;
00113
00114 return path_.size() != 0;
00115 }
00116