Advertisement
schlayer

planner_xy.cpp

Feb 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include "ros/ros.h"
  3. #include "planner/planner_xy.h"
  4. #include "planner/node.h"
  5. #include <math.h>
  6. #include <vector>
  7. #include <functional>
  8. #include <queue>
  9.  
  10. using namespace std;
  11.  
  12. PlannerXY::
  13. PlannerXY() {}
  14.  
  15. PlannerXY::
  16. ~PlannerXY() {}
  17.  
  18. vector< Node* > openList;
  19. vector< Node* > closedList;
  20. vector< Node* > path;
  21.  
  22.  
  23. bool sortByCost (Node* lhs, Node* rhs) { return (lhs->f > rhs->f); }
  24.  
  25.  
  26. std::vector< Node* >
  27. PlannerXY::
  28. findPath( void ) {
  29.     cout << "Found path:\n";
  30.    
  31.     Node* current = closedList.at( closedList.size()-1 );
  32.    
  33.     int c = 0;
  34.     while (current->id != "start") { // While current node has a 'last' node that isn't the start node
  35.         path.push_back(current); // add current to path
  36.         cout << current->id << endl;
  37.        
  38.         current = current->last;
  39.         c++;
  40.         if (c > 5000) { break; cout << "FAIL\n"; }
  41.     }
  42.     path.push_back(current); // add start to path
  43.     cout << current->toString() << endl;
  44.    
  45.     return path;
  46.  
  47. }
  48.  
  49. bool
  50. PlannerXY::
  51. search( const geometry_msgs::Pose& start, const geometry_msgs::Pose& goal, const double& w ) {
  52.     cout << "Weight " << w << endl;
  53.     cout << "\nStart: (" << start.position.x << ", " << start.position.y << ")";
  54.     cout << "\nGoal: (" << goal.position.x << ", " << goal.position.y << ")" << endl;
  55.    
  56.     string startID = "start";
  57.     string goalID = "goal";
  58.    
  59.     //      Node(int ID, double posX, double posY, double G, double H, double F, double weight, Node Goal, Node Last) {
  60.     Node* startNode = new Node(startID, start.position.x, start.position.y, 0, 0, 0, w, NULL, NULL);
  61.     Node* goalNode = new Node(goalID, goal.position.x, goal.position.y, 0, 0, 0, w, NULL, NULL);
  62.    
  63.     startNode->goal = goalNode;
  64.     startNode->last = startNode;
  65.     startNode->recalculate(); // establishes g = 0, h = distToGoal, f = g+hw
  66.    
  67.    
  68.     double distToGoal = startNode->h;
  69.     cout << "Distance from goal: " << distToGoal << endl;
  70.    
  71.     openList.push_back(startNode);
  72.    
  73.     int count = 0;
  74.     // A* time!
  75.     while (!openList.empty()) { // while open list is not empty
  76.         sort  (openList.begin(), openList.end(), sortByCost); // sort the open list
  77.         Node* q = openList.back();  // get cheapest node
  78.         openList.pop_back();    // remove cheapest node
  79.         closedList.push_back(q); // add to closed list
  80.        
  81.         if (q->sameAs(q->goal)) {
  82.             break;
  83.         } // Reached the goal!
  84.        
  85.        
  86.         // get all neighboring nodes, loop through
  87.         for (Node* n: q->expand(w)) {
  88.             //if (n.sameAs(q.last)) { continue; } // skip if same as prev node
  89.             if (n->sameAs(n->goal)) {
  90.                 //cout << "Found goal!\n"; //Almost Done!
  91.             }
  92.            
  93.             bool skip = false;
  94.             for (Node* p: openList) {
  95.                 if (n->sameAs(p) && p->cost() < n->cost()) { skip = true; } // if n already in open list and not cheaper, skip
  96.             }
  97.             for (Node* c: closedList) {
  98.                 if (n->sameAs(c) && c->cost() < n->cost()) { skip = true; } // if n already in closed list and not cheaper, skip
  99.             }
  100.             if (!skip) { openList.push_back(n); } // otherwise, add to open list
  101.            
  102.             //cout << n->toString() << endl;
  103.            
  104.         }
  105.        
  106.         count ++;
  107.        
  108.         if (count > 100000) {
  109.             cout << "Fail!!!" << endl;
  110.             break;
  111.         }
  112.         //exit(0);
  113.     }
  114.    
  115.     cout << "finished" << endl;
  116.     return true;
  117.    
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement