SHARE
TWEET

Untitled

a guest Mar 18th, 2019 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <fstream>
  5. #include <sstream>
  6. #include "serialport.h"
  7. #include "wdigraph.h"
  8. #include "dijkstra.h"
  9. #include "server_util.h"
  10.  
  11. int main() {
  12.   SerialPort Serial("/dev/ttyACM0");
  13.  
  14.   WDigraph graph;
  15.   unordered_map<int, Point> points;
  16.   string line;
  17.   // char delimiter = " ";
  18.  
  19.     readGraph("edmonton-roads-2.0.1.txt", graph, points);
  20.   while (true){
  21.       do{
  22.         line = Serial.readline(1000);
  23.       } while(line[0] != 'R');
  24.       //get the flag and erase it from string
  25.     string flag = line.substr(0, line.find(" "));
  26.     line.erase(0, line.find(" ") + 1);
  27.     // Serial.writeline(line);
  28.  
  29.     // get the first latitude and erase it from string
  30.     int first_lat = stoi(line.substr(0, line.find(" ")));
  31.     line.erase(0, line.find(" ") + 1);
  32.  
  33.     // get the first longitude and erase it from string
  34.     int first_lon = stoi(line.substr(0, line.find(" ")));
  35.     line.erase(0, line.find(" ") + 1);
  36.    
  37.     // get the second latitude and erase it from string
  38.     int second_lat = stoi(line.substr(0, line.find(" ")));
  39.     line.erase(0, line.find(" ") + 1);
  40.  
  41.     // the rest is the second longitude
  42.     int second_lon = stoi(line);
  43.  
  44.   // build the graph
  45.  
  46.  
  47.   // read a request
  48.   Point sPoint, ePoint;
  49.   // cin >> c >> sPoint.lat >> sPoint.lon >> ePoint.lat >> ePoint.lon;
  50.   sPoint.lat = static_cast<long long> (first_lat);
  51.   sPoint.lon = static_cast<long long> (first_lon);
  52.   ePoint.lat = static_cast<long long> (second_lat);
  53.   ePoint.lon = static_cast<long long> (second_lon);
  54.  
  55.   // c will be 'R' in part 1, no need to check until part 2
  56.  
  57.   // get the points closest to the two input points
  58.   int start = findClosest(sPoint, points), end = findClosest(ePoint, points);
  59.  
  60.   // run dijkstra's to compute a shortest path
  61.   unordered_map<int, PLI> tree;
  62.   dijkstra(graph, start, tree);
  63.  
  64.   if (tree.find(end) == tree.end()) {
  65.       // no path
  66.       // cout << "N 0" << endl;
  67.     Serial.writeline("N 0\n");
  68.   }
  69.   else {
  70.     // read off the path by stepping back through the search tree
  71.     list<int> path;
  72.     while (end != start) {
  73.       path.push_front(end);
  74.       end = tree[end].second;
  75.     }
  76.     path.push_front(start);
  77.  
  78.     // output the path
  79.     // cout << "N " << path.size() << endl;
  80.     string send = "N " + path.size();
  81.     Serial.writeline(send);
  82.     // Serial.writeline(path.size());
  83.     Serial.writeline("\n");
  84.     // line = Serial.readline(1000);
  85.  
  86.     for (auto v : path) {
  87.       // cout << "W " << points[v].lat << ' ' << points[v].lon << endl;
  88.  
  89.     // send the position to the arduino
  90.     string next_send = "W " + points[v].lat;
  91.     Serial.writeline(next_send);
  92.     Serial.writeline(" ");
  93.  
  94.     string next2_send = points[v].lon + "\n";
  95.     Serial.writeline(next2_send);
  96.     // Serial.writeline("W ");
  97.     // Serial.writeline(points[v].lat);
  98.     // Serial.writeline(" ");
  99.     // Serial.writeline(points[v].lon);
  100.     // Serial.writeline("\n");
  101.     line = Serial.readline(1000);
  102.     if (line[0] == 'A')
  103.         continue;
  104.     else {break;}
  105.     }
  106.     // cout << "E" << endl;
  107.     // line = Serial.readline();
  108.     Serial.writeline("E\n");
  109.   }
  110.  
  111.   }
  112.  
  113.   return 0;
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top