navigation updated with completed dijkstra's algo

Dependents:   R5 2016 Robotics Team 1

Committer:
j_j205
Date:
Wed Feb 17 16:47:15 2016 +0000
Revision:
3:27cc2bacd6af
Parent:
2:17bd430aeca1
Child:
4:a5d44517c65c
2/17/16 JJ

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j_j205 2:17bd430aeca1 1 #include "StepperDrive.h"
j_j205 0:fd72f6df078c 2 #include "navigation.h"
j_j205 0:fd72f6df078c 3 #include "stdint.h"
j_j205 2:17bd430aeca1 4 #include <cmath>
j_j205 0:fd72f6df078c 5 #include <fstream>
j_j205 0:fd72f6df078c 6 #include <stack>
j_j205 0:fd72f6df078c 7 #include <set>
j_j205 0:fd72f6df078c 8
j_j205 0:fd72f6df078c 9 // FUNCTION:
j_j205 0:fd72f6df078c 10 // Navigation(int size = DEFAULT_SIZE)
j_j205 0:fd72f6df078c 11 // IN-PARAMETERS:
j_j205 0:fd72f6df078c 12 // Number of initial vertices (DEFAULT = 1)
j_j205 0:fd72f6df078c 13 // OUT-PARAMETERS:
j_j205 0:fd72f6df078c 14 // None
j_j205 0:fd72f6df078c 15 // DESCRIPTION:
j_j205 0:fd72f6df078c 16 // Default and 1-parameter constructor.
j_j205 0:fd72f6df078c 17 Navigation::Navigation(int size = DEFAULT_SIZE) : vertex(DEFAULT_VERTEX),
j_j205 0:fd72f6df078c 18 angle(DEFAULT_ANGLE)
j_j205 0:fd72f6df078c 19 {
j_j205 0:fd72f6df078c 20 graph.resize(size);
j_j205 0:fd72f6df078c 21 }
j_j205 0:fd72f6df078c 22
j_j205 0:fd72f6df078c 23 // FUNCTION:
j_j205 0:fd72f6df078c 24 // addGraphNode(uint16_t src, uint16_t target, uint16_t dist,
j_j205 0:fd72f6df078c 25 // uint16_t angle)
j_j205 0:fd72f6df078c 26 // IN-PARAMETERS:
j_j205 0:fd72f6df078c 27 // Source node (uint16_t), target node (uint16_t), edge distance
j_j205 0:fd72f6df078c 28 // (uint16_t), and angle (uint16_t)
j_j205 0:fd72f6df078c 29 // OUT-PARAMETERS:
j_j205 0:fd72f6df078c 30 // None
j_j205 0:fd72f6df078c 31 // DESCRIPTION:
j_j205 0:fd72f6df078c 32 // Adds a graph node to the graph.
j_j205 2:17bd430aeca1 33 void Navigation::addGraphNode(uint16_t src, uint16_t target, float dist,
j_j205 2:17bd430aeca1 34 float angle)
j_j205 0:fd72f6df078c 35 {
j_j205 0:fd72f6df078c 36 graph[src].push_back(graphNode(target, dist, angle));
j_j205 0:fd72f6df078c 37 }
j_j205 0:fd72f6df078c 38
j_j205 0:fd72f6df078c 39 // FUNCTION:
j_j205 0:fd72f6df078c 40 // addGraphNode(uint16_t src, uint16_t target, uint16_t dist,
j_j205 0:fd72f6df078c 41 // uint16_t angle)
j_j205 0:fd72f6df078c 42 // IN-PARAMETERS:
j_j205 0:fd72f6df078c 43 // Source node (uint16_t)
j_j205 0:fd72f6df078c 44 // OUT-PARAMETERS:
j_j205 0:fd72f6df078c 45 // None
j_j205 0:fd72f6df078c 46 // DESCRIPTION:
j_j205 0:fd72f6df078c 47 // Adds a graph node to the graph, with target, dist, and angle
j_j205 0:fd72f6df078c 48 // initialized to infinity
j_j205 0:fd72f6df078c 49 void Navigation::addGraphNode(uint16_t src)
j_j205 0:fd72f6df078c 50 {
j_j205 0:fd72f6df078c 51 graph[src].push_back(graphNode());
j_j205 0:fd72f6df078c 52 }
j_j205 0:fd72f6df078c 53
j_j205 0:fd72f6df078c 54 // FUNCTION:
j_j205 0:fd72f6df078c 55 // int loadMap(char* infile);
j_j205 0:fd72f6df078c 56 // IN-PARAMETERS:
j_j205 0:fd72f6df078c 57 // Input file name (*char[])
j_j205 0:fd72f6df078c 58 // OUT-PARAMETERS:
j_j205 0:fd72f6df078c 59 // Returns 1 if file doesn't open
j_j205 0:fd72f6df078c 60 // Returns 0 if file opens succesfully
j_j205 0:fd72f6df078c 61 // DESCRIPTION:
j_j205 0:fd72f6df078c 62 // Loads contents of a file as a weighted graph. First entry in the file
j_j205 0:fd72f6df078c 63 // should be the number of vertices. All subsequent entries indicate edges
j_j205 0:fd72f6df078c 64 // by listing a source vertex, a target vertex, the distance between them
j_j205 0:fd72f6df078c 65 // and the angle from source to target (0 - 359 degrees) . Only edges
j_j205 0:fd72f6df078c 66 // are listed. Assumes that inverse angles/edges are included in file.
j_j205 0:fd72f6df078c 67 int Navigation::loadMap(char* inputFile)
j_j205 0:fd72f6df078c 68 {
j_j205 0:fd72f6df078c 69 std::ifstream inFile;
j_j205 0:fd72f6df078c 70 inFile.open(inputFile);
j_j205 0:fd72f6df078c 71 if (!inFile)
j_j205 0:fd72f6df078c 72 return 1; // returns 1 if file fails to open
j_j205 0:fd72f6df078c 73
j_j205 0:fd72f6df078c 74 int vertices;
j_j205 0:fd72f6df078c 75 inFile >> vertices; // reads first entry as number of vertices
j_j205 0:fd72f6df078c 76 graph.resize(vertices); // resizes vector as necessary
j_j205 0:fd72f6df078c 77
j_j205 0:fd72f6df078c 78 uint16_t src;
j_j205 0:fd72f6df078c 79 uint16_t target;
j_j205 2:17bd430aeca1 80 float dist;
j_j205 2:17bd430aeca1 81 float angle;
j_j205 0:fd72f6df078c 82
j_j205 0:fd72f6df078c 83 while (!inFile.eof() )
j_j205 0:fd72f6df078c 84 {
j_j205 0:fd72f6df078c 85 inFile >> src;
j_j205 0:fd72f6df078c 86 inFile >> target;
j_j205 0:fd72f6df078c 87 inFile >> dist;
j_j205 0:fd72f6df078c 88 inFile >> angle;
j_j205 0:fd72f6df078c 89
j_j205 0:fd72f6df078c 90 addGraphNode(src, target, dist, angle);
j_j205 0:fd72f6df078c 91 } // loads all data from file into graph
j_j205 0:fd72f6df078c 92
j_j205 0:fd72f6df078c 93 return 0;
j_j205 0:fd72f6df078c 94 }
j_j205 0:fd72f6df078c 95
j_j205 0:fd72f6df078c 96 // FUNCTION:
j_j205 0:fd72f6df078c 97 // void getShortestPath(uint16_t src, uint16_t destination)
j_j205 0:fd72f6df078c 98 // IN-PARAMETERS:
j_j205 0:fd72f6df078c 99 // Target vertex (uint16_t)
j_j205 0:fd72f6df078c 100 // OUT-PARAMETERS:
j_j205 0:fd72f6df078c 101 // None
j_j205 0:fd72f6df078c 102 // DESCRIPTION:
j_j205 0:fd72f6df078c 103 // Implementation of Dijkstra's algorithm. Uses current vertex as source
j_j205 0:fd72f6df078c 104 // and calculates shortest path information for all other vertices.
j_j205 0:fd72f6df078c 105 // Subroutine ends when shortest path for target vertex has been found
j_j205 0:fd72f6df078c 106 // and then loads the path into route.
j_j205 0:fd72f6df078c 107 void Navigation::getShortestPath(uint16_t destination)
j_j205 0:fd72f6df078c 108 {
j_j205 0:fd72f6df078c 109 uint16_t src = vertex;
j_j205 0:fd72f6df078c 110 uint16_t v; // stores temporary vertex
j_j205 0:fd72f6df078c 111 int n = graph.size();
j_j205 0:fd72f6df078c 112 minDistance.clear();
j_j205 0:fd72f6df078c 113 minDistance.resize(n, MAX_DIST);
j_j205 0:fd72f6df078c 114 minDistance[src] = 0;
j_j205 0:fd72f6df078c 115 previous.clear();
j_j205 0:fd72f6df078c 116 previous.resize(n, -1);
j_j205 2:17bd430aeca1 117 std::set<std::pair<float, uint16_t> > vertex_queue;
j_j205 0:fd72f6df078c 118 vertex_queue.insert(std::make_pair(minDistance[src], src));
j_j205 0:fd72f6df078c 119
j_j205 0:fd72f6df078c 120 while (!vertex_queue.empty())
j_j205 0:fd72f6df078c 121 {
j_j205 2:17bd430aeca1 122 float dist = vertex_queue.begin()->first;
j_j205 0:fd72f6df078c 123 uint16_t u = vertex_queue.begin()->second;
j_j205 0:fd72f6df078c 124 vertex_queue.erase(vertex_queue.begin());
j_j205 0:fd72f6df078c 125
j_j205 0:fd72f6df078c 126 // Visit each edge exiting u
j_j205 0:fd72f6df078c 127 const std::vector<graphNode> &neighbors = graph[u];
j_j205 0:fd72f6df078c 128 for (std::vector<graphNode>::const_iterator neighbor_iter = neighbors.begin();
j_j205 0:fd72f6df078c 129 neighbor_iter != neighbors.end();
j_j205 0:fd72f6df078c 130 neighbor_iter++)
j_j205 0:fd72f6df078c 131 {
j_j205 0:fd72f6df078c 132 v = neighbor_iter->neighbor;
j_j205 2:17bd430aeca1 133 float weight = neighbor_iter->distance;
j_j205 2:17bd430aeca1 134 float distance_through_u = dist + weight;
j_j205 0:fd72f6df078c 135
j_j205 0:fd72f6df078c 136 if (distance_through_u < minDistance[v])
j_j205 0:fd72f6df078c 137 {
j_j205 0:fd72f6df078c 138 vertex_queue.erase(std::make_pair(minDistance[v], v));
j_j205 0:fd72f6df078c 139
j_j205 0:fd72f6df078c 140 minDistance[v] = distance_through_u;
j_j205 0:fd72f6df078c 141 previous[v] = u;
j_j205 0:fd72f6df078c 142 vertex_queue.insert(std::make_pair(minDistance[v], v));
j_j205 0:fd72f6df078c 143 }
j_j205 0:fd72f6df078c 144 if (v == destination)
j_j205 0:fd72f6df078c 145 break;
j_j205 0:fd72f6df078c 146 }
j_j205 0:fd72f6df078c 147 if (v == destination)
j_j205 0:fd72f6df078c 148 break;
j_j205 0:fd72f6df078c 149 }
j_j205 0:fd72f6df078c 150 while (v != src)
j_j205 0:fd72f6df078c 151 {
j_j205 0:fd72f6df078c 152 route.push(v);
j_j205 0:fd72f6df078c 153 v = previous[v];
j_j205 0:fd72f6df078c 154 }
j_j205 0:fd72f6df078c 155 }
j_j205 0:fd72f6df078c 156
j_j205 1:a53d97b74fab 157 // FUNCTION:
j_j205 1:a53d97b74fab 158 // void executeRoute()
j_j205 1:a53d97b74fab 159 // IN-PARAMETERS:
j_j205 1:a53d97b74fab 160 // Address of left and right steppers (StepperMotor &)
j_j205 1:a53d97b74fab 161 // OUT-PARAMETERS:
j_j205 1:a53d97b74fab 162 // None
j_j205 1:a53d97b74fab 163 // DESCRIPTION:
j_j205 1:a53d97b74fab 164 // Sends command to steppers to execute route.
j_j205 2:17bd430aeca1 165 void Navigation::executeRoute(Serial &pc, StepperDrive &drive)
j_j205 1:a53d97b74fab 166 {
j_j205 1:a53d97b74fab 167 uint16_t target;
j_j205 2:17bd430aeca1 168 float targetAngle;
j_j205 2:17bd430aeca1 169 float targetDist;
j_j205 2:17bd430aeca1 170 float differenceAngle;
j_j205 1:a53d97b74fab 171
j_j205 1:a53d97b74fab 172 while (!route.empty() )
j_j205 1:a53d97b74fab 173 {
j_j205 1:a53d97b74fab 174 target = route.top();
j_j205 1:a53d97b74fab 175 route.pop();
j_j205 1:a53d97b74fab 176
j_j205 2:17bd430aeca1 177 for (int i = 0; i < graph[vertex].size(); i++)
j_j205 2:17bd430aeca1 178 {
j_j205 2:17bd430aeca1 179 if (graph[vertex][i].neighbor == target)
j_j205 2:17bd430aeca1 180 {
j_j205 2:17bd430aeca1 181 targetAngle = graph[vertex][i].angle;
j_j205 2:17bd430aeca1 182 targetDist = graph[vertex][i].distance;
j_j205 2:17bd430aeca1 183 break;
j_j205 2:17bd430aeca1 184 }
j_j205 2:17bd430aeca1 185 }
j_j205 2:17bd430aeca1 186
j_j205 2:17bd430aeca1 187 if (targetAngle != angle)
j_j205 2:17bd430aeca1 188 {
j_j205 2:17bd430aeca1 189 differenceAngle = targetAngle - angle;
j_j205 2:17bd430aeca1 190
j_j205 2:17bd430aeca1 191 if (abs(differenceAngle) > 180)
j_j205 2:17bd430aeca1 192 {
j_j205 2:17bd430aeca1 193 if (differenceAngle < 0) //
j_j205 2:17bd430aeca1 194 differenceAngle = 360 - abs(differenceAngle);
j_j205 2:17bd430aeca1 195 else
j_j205 2:17bd430aeca1 196 differenceAngle = -(360 - differenceAngle);
j_j205 2:17bd430aeca1 197 }
j_j205 2:17bd430aeca1 198
j_j205 2:17bd430aeca1 199 else
j_j205 2:17bd430aeca1 200 {
j_j205 2:17bd430aeca1 201 /* do nothing */
j_j205 2:17bd430aeca1 202 }
j_j205 3:27cc2bacd6af 203
j_j205 2:17bd430aeca1 204 /* send -(differenceAngle) to motors */
j_j205 3:27cc2bacd6af 205 int returnVal = drive.move(0, ((-differenceAngle)*(PI / 180.0)) );
j_j205 3:27cc2bacd6af 206 pc.printf("\nChanged angle %f degrees to %f", differenceAngle, targetAngle);
j_j205 3:27cc2bacd6af 207
j_j205 2:17bd430aeca1 208 // wait for move to complete
j_j205 2:17bd430aeca1 209 while(!drive.isMoveDone())
j_j205 2:17bd430aeca1 210 {
j_j205 3:27cc2bacd6af 211 wait(1e-6);
j_j205 2:17bd430aeca1 212 }
j_j205 2:17bd430aeca1 213 }
j_j205 3:27cc2bacd6af 214
j_j205 2:17bd430aeca1 215 /* send targetDist to motors */
j_j205 3:27cc2bacd6af 216 int returnVal = drive.move(targetDist, 0);
j_j205 2:17bd430aeca1 217 pc.printf("\nMoved forward a distance of %f to node %i", targetDist, target);
j_j205 3:27cc2bacd6af 218
j_j205 2:17bd430aeca1 219 // wait for move to complete
j_j205 2:17bd430aeca1 220 while(!drive.isMoveDone())
j_j205 2:17bd430aeca1 221 {
j_j205 3:27cc2bacd6af 222 wait(1e-6);
j_j205 2:17bd430aeca1 223 }
j_j205 3:27cc2bacd6af 224
j_j205 2:17bd430aeca1 225 vertex = target;
j_j205 2:17bd430aeca1 226 angle = targetAngle;
j_j205 1:a53d97b74fab 227 }
j_j205 1:a53d97b74fab 228 }
j_j205 1:a53d97b74fab 229
j_j205 0:fd72f6df078c 230 // utility function
j_j205 0:fd72f6df078c 231 void Navigation::printPrevious(Serial &pc)
j_j205 0:fd72f6df078c 232 {
j_j205 0:fd72f6df078c 233 for(int i = 0; i < previous.size(); i++)
j_j205 0:fd72f6df078c 234 {
j_j205 2:17bd430aeca1 235 pc.printf("Node %f is prev to node %f\n", previous[i], i);
j_j205 0:fd72f6df078c 236 }
j_j205 0:fd72f6df078c 237 }
j_j205 0:fd72f6df078c 238
j_j205 0:fd72f6df078c 239 // utility function
j_j205 0:fd72f6df078c 240 void Navigation::printRoute(Serial &pc)
j_j205 0:fd72f6df078c 241 {
j_j205 0:fd72f6df078c 242 while (!route.empty() )
j_j205 0:fd72f6df078c 243 {
j_j205 0:fd72f6df078c 244 pc.printf("Go to node %i\n", route.top() );
j_j205 0:fd72f6df078c 245 route.pop();
j_j205 0:fd72f6df078c 246 }
j_j205 0:fd72f6df078c 247 }
j_j205 0:fd72f6df078c 248
j_j205 0:fd72f6df078c 249 // utility function
j_j205 0:fd72f6df078c 250 void Navigation::printGraph(Serial &pc)
j_j205 0:fd72f6df078c 251 {
j_j205 0:fd72f6df078c 252 for (int i = 0; i < graph.size(); i++)
j_j205 0:fd72f6df078c 253 for (int j = 0; j < graph[i].size(); j++)
j_j205 0:fd72f6df078c 254 {
j_j205 2:17bd430aeca1 255 pc.printf("Node %i to %i = dist - %f, angle - %f\n", i, j, graph[i][j].distance, graph[i][j].angle);
j_j205 0:fd72f6df078c 256 }
j_j205 0:fd72f6df078c 257 }