navigation updated with completed dijkstra's algo
Dependents: R5 2016 Robotics Team 1
navigation.cpp@6:d2da4d4b5112, 2016-04-06 (annotated)
- Committer:
- j_j205
- Date:
- Wed Apr 06 22:01:59 2016 +0000
- Revision:
- 6:d2da4d4b5112
- Parent:
- 5:d0954e0aecc9
- Child:
- 7:f7489797746b
changes to localization;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j_j205 | 2:17bd430aeca1 | 1 | #include "StepperDrive.h" |
j_j205 | 6:d2da4d4b5112 | 2 | #include "LongRangeSensor.h" |
j_j205 | 0:fd72f6df078c | 3 | #include "navigation.h" |
j_j205 | 0:fd72f6df078c | 4 | #include "stdint.h" |
j_j205 | 2:17bd430aeca1 | 5 | #include <cmath> |
j_j205 | 0:fd72f6df078c | 6 | #include <fstream> |
j_j205 | 0:fd72f6df078c | 7 | #include <stack> |
j_j205 | 0:fd72f6df078c | 8 | #include <set> |
j_j205 | 0:fd72f6df078c | 9 | |
j_j205 | 0:fd72f6df078c | 10 | // FUNCTION: |
j_j205 | 0:fd72f6df078c | 11 | // Navigation(int size = DEFAULT_SIZE) |
j_j205 | 0:fd72f6df078c | 12 | // IN-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 13 | // Number of initial vertices (DEFAULT = 1) |
j_j205 | 0:fd72f6df078c | 14 | // OUT-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 15 | // None |
j_j205 | 0:fd72f6df078c | 16 | // DESCRIPTION: |
j_j205 | 0:fd72f6df078c | 17 | // Default and 1-parameter constructor. |
j_j205 | 6:d2da4d4b5112 | 18 | Navigation::Navigation(Serial &_pc, StepperDrive &_drive, |
j_j205 | 6:d2da4d4b5112 | 19 | LongRangeSensor &_longRangeL, LongRangeSensor &_longRangeR, DigitalOut &_led_red, DigitalOut &_led_green, |
j_j205 | 6:d2da4d4b5112 | 20 | int size = DEFAULT_SIZE) : pc(_pc), drive(_drive), |
j_j205 | 6:d2da4d4b5112 | 21 | longRangeL(_longRangeL), longRangeR(_longRangeR), led_red(_led_red), led_green(_led_green), |
j_j205 | 6:d2da4d4b5112 | 22 | vertex(DEFAULT_VERTEX), angle(DEFAULT_ANGLE) |
j_j205 | 0:fd72f6df078c | 23 | { |
j_j205 | 0:fd72f6df078c | 24 | graph.resize(size); |
j_j205 | 6:d2da4d4b5112 | 25 | distLocalL = 0.0; |
j_j205 | 0:fd72f6df078c | 26 | } |
j_j205 | 0:fd72f6df078c | 27 | |
j_j205 | 0:fd72f6df078c | 28 | // FUNCTION: |
j_j205 | 0:fd72f6df078c | 29 | // addGraphNode(uint16_t src, uint16_t target, uint16_t dist, |
j_j205 | 0:fd72f6df078c | 30 | // uint16_t angle) |
j_j205 | 0:fd72f6df078c | 31 | // IN-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 32 | // Source node (uint16_t), target node (uint16_t), edge distance |
j_j205 | 0:fd72f6df078c | 33 | // (uint16_t), and angle (uint16_t) |
j_j205 | 0:fd72f6df078c | 34 | // OUT-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 35 | // None |
j_j205 | 0:fd72f6df078c | 36 | // DESCRIPTION: |
j_j205 | 0:fd72f6df078c | 37 | // Adds a graph node to the graph. |
j_j205 | 6:d2da4d4b5112 | 38 | void Navigation::addGraphNode(uint8_t src, uint8_t target, float dist, |
j_j205 | 2:17bd430aeca1 | 39 | float angle) |
j_j205 | 0:fd72f6df078c | 40 | { |
j_j205 | 0:fd72f6df078c | 41 | graph[src].push_back(graphNode(target, dist, angle)); |
j_j205 | 0:fd72f6df078c | 42 | } |
j_j205 | 0:fd72f6df078c | 43 | |
j_j205 | 0:fd72f6df078c | 44 | // FUNCTION: |
j_j205 | 0:fd72f6df078c | 45 | // addGraphNode(uint16_t src, uint16_t target, uint16_t dist, |
j_j205 | 0:fd72f6df078c | 46 | // uint16_t angle) |
j_j205 | 0:fd72f6df078c | 47 | // IN-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 48 | // Source node (uint16_t) |
j_j205 | 0:fd72f6df078c | 49 | // OUT-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 50 | // None |
j_j205 | 0:fd72f6df078c | 51 | // DESCRIPTION: |
j_j205 | 0:fd72f6df078c | 52 | // Adds a graph node to the graph, with target, dist, and angle |
j_j205 | 0:fd72f6df078c | 53 | // initialized to infinity |
j_j205 | 6:d2da4d4b5112 | 54 | void Navigation::addGraphNode(uint8_t src) |
j_j205 | 0:fd72f6df078c | 55 | { |
j_j205 | 0:fd72f6df078c | 56 | graph[src].push_back(graphNode()); |
j_j205 | 0:fd72f6df078c | 57 | } |
j_j205 | 0:fd72f6df078c | 58 | |
j_j205 | 0:fd72f6df078c | 59 | // FUNCTION: |
j_j205 | 0:fd72f6df078c | 60 | // int loadMap(char* infile); |
j_j205 | 0:fd72f6df078c | 61 | // IN-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 62 | // Input file name (*char[]) |
j_j205 | 0:fd72f6df078c | 63 | // OUT-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 64 | // Returns 1 if file doesn't open |
j_j205 | 0:fd72f6df078c | 65 | // Returns 0 if file opens succesfully |
j_j205 | 0:fd72f6df078c | 66 | // DESCRIPTION: |
j_j205 | 0:fd72f6df078c | 67 | // Loads contents of a file as a weighted graph. First entry in the file |
j_j205 | 0:fd72f6df078c | 68 | // should be the number of vertices. All subsequent entries indicate edges |
j_j205 | 0:fd72f6df078c | 69 | // by listing a source vertex, a target vertex, the distance between them |
j_j205 | 0:fd72f6df078c | 70 | // and the angle from source to target (0 - 359 degrees) . Only edges |
j_j205 | 0:fd72f6df078c | 71 | // are listed. Assumes that inverse angles/edges are included in file. |
j_j205 | 0:fd72f6df078c | 72 | int Navigation::loadMap(char* inputFile) |
j_j205 | 0:fd72f6df078c | 73 | { |
j_j205 | 0:fd72f6df078c | 74 | std::ifstream inFile; |
j_j205 | 0:fd72f6df078c | 75 | inFile.open(inputFile); |
j_j205 | 0:fd72f6df078c | 76 | if (!inFile) |
j_j205 | 0:fd72f6df078c | 77 | return 1; // returns 1 if file fails to open |
j_j205 | 0:fd72f6df078c | 78 | |
j_j205 | 0:fd72f6df078c | 79 | int vertices; |
j_j205 | 0:fd72f6df078c | 80 | inFile >> vertices; // reads first entry as number of vertices |
j_j205 | 0:fd72f6df078c | 81 | graph.resize(vertices); // resizes vector as necessary |
j_j205 | 0:fd72f6df078c | 82 | |
j_j205 | 6:d2da4d4b5112 | 83 | uint8_t src; |
j_j205 | 6:d2da4d4b5112 | 84 | uint8_t target; |
j_j205 | 2:17bd430aeca1 | 85 | float dist; |
j_j205 | 2:17bd430aeca1 | 86 | float angle; |
j_j205 | 0:fd72f6df078c | 87 | |
j_j205 | 0:fd72f6df078c | 88 | while (!inFile.eof() ) |
j_j205 | 0:fd72f6df078c | 89 | { |
j_j205 | 0:fd72f6df078c | 90 | inFile >> src; |
j_j205 | 0:fd72f6df078c | 91 | inFile >> target; |
j_j205 | 0:fd72f6df078c | 92 | inFile >> dist; |
j_j205 | 0:fd72f6df078c | 93 | inFile >> angle; |
j_j205 | 0:fd72f6df078c | 94 | |
j_j205 | 0:fd72f6df078c | 95 | addGraphNode(src, target, dist, angle); |
j_j205 | 0:fd72f6df078c | 96 | } // loads all data from file into graph |
j_j205 | 0:fd72f6df078c | 97 | |
j_j205 | 0:fd72f6df078c | 98 | return 0; |
j_j205 | 0:fd72f6df078c | 99 | } |
j_j205 | 0:fd72f6df078c | 100 | |
j_j205 | 0:fd72f6df078c | 101 | // FUNCTION: |
j_j205 | 0:fd72f6df078c | 102 | // void getShortestPath(uint16_t src, uint16_t destination) |
j_j205 | 0:fd72f6df078c | 103 | // IN-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 104 | // Target vertex (uint16_t) |
j_j205 | 0:fd72f6df078c | 105 | // OUT-PARAMETERS: |
j_j205 | 0:fd72f6df078c | 106 | // None |
j_j205 | 0:fd72f6df078c | 107 | // DESCRIPTION: |
j_j205 | 0:fd72f6df078c | 108 | // Implementation of Dijkstra's algorithm. Uses current vertex as source |
j_j205 | 0:fd72f6df078c | 109 | // and calculates shortest path information for all other vertices. |
j_j205 | 0:fd72f6df078c | 110 | // Subroutine ends when shortest path for target vertex has been found |
j_j205 | 0:fd72f6df078c | 111 | // and then loads the path into route. |
j_j205 | 6:d2da4d4b5112 | 112 | void Navigation::getShortestPath(uint8_t destination) |
j_j205 | 0:fd72f6df078c | 113 | { |
j_j205 | 6:d2da4d4b5112 | 114 | uint8_t src = vertex; |
j_j205 | 6:d2da4d4b5112 | 115 | uint8_t v; // stores temporary vertex |
j_j205 | 0:fd72f6df078c | 116 | int n = graph.size(); |
j_j205 | 0:fd72f6df078c | 117 | minDistance.clear(); |
j_j205 | 0:fd72f6df078c | 118 | minDistance.resize(n, MAX_DIST); |
j_j205 | 0:fd72f6df078c | 119 | minDistance[src] = 0; |
j_j205 | 0:fd72f6df078c | 120 | previous.clear(); |
j_j205 | 0:fd72f6df078c | 121 | previous.resize(n, -1); |
j_j205 | 6:d2da4d4b5112 | 122 | std::set<std::pair<float, uint8_t> > vertex_queue; |
j_j205 | 0:fd72f6df078c | 123 | vertex_queue.insert(std::make_pair(minDistance[src], src)); |
j_j205 | 0:fd72f6df078c | 124 | |
j_j205 | 0:fd72f6df078c | 125 | while (!vertex_queue.empty()) |
j_j205 | 0:fd72f6df078c | 126 | { |
j_j205 | 2:17bd430aeca1 | 127 | float dist = vertex_queue.begin()->first; |
j_j205 | 6:d2da4d4b5112 | 128 | uint8_t u = vertex_queue.begin()->second; |
j_j205 | 0:fd72f6df078c | 129 | vertex_queue.erase(vertex_queue.begin()); |
j_j205 | 0:fd72f6df078c | 130 | |
j_j205 | 0:fd72f6df078c | 131 | // Visit each edge exiting u |
j_j205 | 0:fd72f6df078c | 132 | const std::vector<graphNode> &neighbors = graph[u]; |
j_j205 | 0:fd72f6df078c | 133 | for (std::vector<graphNode>::const_iterator neighbor_iter = neighbors.begin(); |
j_j205 | 0:fd72f6df078c | 134 | neighbor_iter != neighbors.end(); |
j_j205 | 0:fd72f6df078c | 135 | neighbor_iter++) |
j_j205 | 0:fd72f6df078c | 136 | { |
j_j205 | 0:fd72f6df078c | 137 | v = neighbor_iter->neighbor; |
j_j205 | 2:17bd430aeca1 | 138 | float weight = neighbor_iter->distance; |
j_j205 | 2:17bd430aeca1 | 139 | float distance_through_u = dist + weight; |
j_j205 | 0:fd72f6df078c | 140 | |
j_j205 | 0:fd72f6df078c | 141 | if (distance_through_u < minDistance[v]) |
j_j205 | 0:fd72f6df078c | 142 | { |
j_j205 | 0:fd72f6df078c | 143 | vertex_queue.erase(std::make_pair(minDistance[v], v)); |
j_j205 | 0:fd72f6df078c | 144 | |
j_j205 | 0:fd72f6df078c | 145 | minDistance[v] = distance_through_u; |
j_j205 | 0:fd72f6df078c | 146 | previous[v] = u; |
j_j205 | 0:fd72f6df078c | 147 | vertex_queue.insert(std::make_pair(minDistance[v], v)); |
j_j205 | 0:fd72f6df078c | 148 | } |
j_j205 | 0:fd72f6df078c | 149 | if (v == destination) |
j_j205 | 0:fd72f6df078c | 150 | break; |
j_j205 | 0:fd72f6df078c | 151 | } |
j_j205 | 0:fd72f6df078c | 152 | if (v == destination) |
j_j205 | 0:fd72f6df078c | 153 | break; |
j_j205 | 0:fd72f6df078c | 154 | } |
j_j205 | 0:fd72f6df078c | 155 | while (v != src) |
j_j205 | 0:fd72f6df078c | 156 | { |
j_j205 | 0:fd72f6df078c | 157 | route.push(v); |
j_j205 | 0:fd72f6df078c | 158 | v = previous[v]; |
j_j205 | 0:fd72f6df078c | 159 | } |
j_j205 | 0:fd72f6df078c | 160 | } |
j_j205 | 0:fd72f6df078c | 161 | |
j_j205 | 1:a53d97b74fab | 162 | // FUNCTION: |
j_j205 | 1:a53d97b74fab | 163 | // void executeRoute() |
j_j205 | 1:a53d97b74fab | 164 | // IN-PARAMETERS: |
j_j205 | 1:a53d97b74fab | 165 | // Address of left and right steppers (StepperMotor &) |
j_j205 | 1:a53d97b74fab | 166 | // OUT-PARAMETERS: |
j_j205 | 1:a53d97b74fab | 167 | // None |
j_j205 | 1:a53d97b74fab | 168 | // DESCRIPTION: |
j_j205 | 1:a53d97b74fab | 169 | // Sends command to steppers to execute route. |
j_j205 | 6:d2da4d4b5112 | 170 | void Navigation::executeRoute() |
j_j205 | 1:a53d97b74fab | 171 | { |
j_j205 | 6:d2da4d4b5112 | 172 | uint8_t target; |
j_j205 | 2:17bd430aeca1 | 173 | float targetAngle; |
j_j205 | 2:17bd430aeca1 | 174 | float targetDist; |
j_j205 | 2:17bd430aeca1 | 175 | float differenceAngle; |
j_j205 | 4:a5d44517c65c | 176 | |
j_j205 | 1:a53d97b74fab | 177 | while (!route.empty() ) |
j_j205 | 1:a53d97b74fab | 178 | { |
j_j205 | 1:a53d97b74fab | 179 | target = route.top(); |
j_j205 | 1:a53d97b74fab | 180 | route.pop(); |
j_j205 | 4:a5d44517c65c | 181 | |
j_j205 | 2:17bd430aeca1 | 182 | for (int i = 0; i < graph[vertex].size(); i++) |
j_j205 | 2:17bd430aeca1 | 183 | { |
j_j205 | 2:17bd430aeca1 | 184 | if (graph[vertex][i].neighbor == target) |
j_j205 | 2:17bd430aeca1 | 185 | { |
j_j205 | 2:17bd430aeca1 | 186 | targetAngle = graph[vertex][i].angle; |
j_j205 | 2:17bd430aeca1 | 187 | targetDist = graph[vertex][i].distance; |
j_j205 | 2:17bd430aeca1 | 188 | break; |
j_j205 | 2:17bd430aeca1 | 189 | } |
j_j205 | 2:17bd430aeca1 | 190 | } |
j_j205 | 4:a5d44517c65c | 191 | |
j_j205 | 2:17bd430aeca1 | 192 | if (targetAngle != angle) |
j_j205 | 2:17bd430aeca1 | 193 | { |
j_j205 | 2:17bd430aeca1 | 194 | differenceAngle = targetAngle - angle; |
j_j205 | 4:a5d44517c65c | 195 | |
j_j205 | 2:17bd430aeca1 | 196 | if (abs(differenceAngle) > 180) |
j_j205 | 2:17bd430aeca1 | 197 | { |
j_j205 | 6:d2da4d4b5112 | 198 | if (differenceAngle < 0) |
j_j205 | 2:17bd430aeca1 | 199 | differenceAngle = 360 - abs(differenceAngle); |
j_j205 | 2:17bd430aeca1 | 200 | else |
j_j205 | 2:17bd430aeca1 | 201 | differenceAngle = -(360 - differenceAngle); |
j_j205 | 2:17bd430aeca1 | 202 | } |
j_j205 | 4:a5d44517c65c | 203 | |
j_j205 | 2:17bd430aeca1 | 204 | else |
j_j205 | 2:17bd430aeca1 | 205 | { |
j_j205 | 2:17bd430aeca1 | 206 | /* do nothing */ |
j_j205 | 2:17bd430aeca1 | 207 | } |
j_j205 | 4:a5d44517c65c | 208 | |
j_j205 | 2:17bd430aeca1 | 209 | /* send -(differenceAngle) to motors */ |
j_j205 | 3:27cc2bacd6af | 210 | int returnVal = drive.move(0, ((-differenceAngle)*(PI / 180.0)) ); |
j_j205 | 4:a5d44517c65c | 211 | pc.printf("\nChanged angle %f degrees to %f", differenceAngle, targetAngle); |
j_j205 | 4:a5d44517c65c | 212 | |
j_j205 | 2:17bd430aeca1 | 213 | // wait for move to complete |
j_j205 | 2:17bd430aeca1 | 214 | while(!drive.isMoveDone()) |
j_j205 | 2:17bd430aeca1 | 215 | { |
j_j205 | 3:27cc2bacd6af | 216 | wait(1e-6); |
j_j205 | 2:17bd430aeca1 | 217 | } |
j_j205 | 6:d2da4d4b5112 | 218 | |
j_j205 | 6:d2da4d4b5112 | 219 | distLocalL = longRangeL.distInchesLOne(); |
j_j205 | 6:d2da4d4b5112 | 220 | distLocalR = longRangeR.distInchesROne(); |
j_j205 | 6:d2da4d4b5112 | 221 | pc.printf("\n distLocalL = %f", distLocalL); |
j_j205 | 6:d2da4d4b5112 | 222 | pc.printf("\n distLocalR = %f", distLocalR); |
j_j205 | 6:d2da4d4b5112 | 223 | if ((distLocalL <= 8.0) && (distLocalL >= 3.0)) // wall in range |
j_j205 | 6:d2da4d4b5112 | 224 | { |
j_j205 | 6:d2da4d4b5112 | 225 | localizeRight(); |
j_j205 | 6:d2da4d4b5112 | 226 | } |
j_j205 | 6:d2da4d4b5112 | 227 | |
j_j205 | 6:d2da4d4b5112 | 228 | else if ((distLocalR <= 10.5) && (distLocalR >= 5.0)) // wall in range |
j_j205 | 6:d2da4d4b5112 | 229 | { |
j_j205 | 6:d2da4d4b5112 | 230 | localizeLeft(); |
j_j205 | 6:d2da4d4b5112 | 231 | } |
j_j205 | 2:17bd430aeca1 | 232 | } |
j_j205 | 4:a5d44517c65c | 233 | |
j_j205 | 2:17bd430aeca1 | 234 | /* send targetDist to motors */ |
j_j205 | 3:27cc2bacd6af | 235 | int returnVal = drive.move(targetDist, 0); |
j_j205 | 2:17bd430aeca1 | 236 | pc.printf("\nMoved forward a distance of %f to node %i", targetDist, target); |
j_j205 | 3:27cc2bacd6af | 237 | |
j_j205 | 2:17bd430aeca1 | 238 | // wait for move to complete |
j_j205 | 2:17bd430aeca1 | 239 | while(!drive.isMoveDone()) |
j_j205 | 2:17bd430aeca1 | 240 | { |
j_j205 | 3:27cc2bacd6af | 241 | wait(1e-6); |
j_j205 | 2:17bd430aeca1 | 242 | } |
j_j205 | 4:a5d44517c65c | 243 | |
j_j205 | 2:17bd430aeca1 | 244 | vertex = target; |
j_j205 | 2:17bd430aeca1 | 245 | angle = targetAngle; |
j_j205 | 6:d2da4d4b5112 | 246 | |
j_j205 | 6:d2da4d4b5112 | 247 | distLocalL = longRangeL.distInchesLOne(); |
j_j205 | 6:d2da4d4b5112 | 248 | distLocalR = longRangeR.distInchesROne(); |
j_j205 | 6:d2da4d4b5112 | 249 | pc.printf("\n distLocalL = %f", distLocalL); |
j_j205 | 6:d2da4d4b5112 | 250 | pc.printf("\n distLocalR = %f", distLocalR); |
j_j205 | 6:d2da4d4b5112 | 251 | if ((distLocalL <= 8.5) && (distLocalL >= 3.0)) // wall in range |
j_j205 | 6:d2da4d4b5112 | 252 | { |
j_j205 | 6:d2da4d4b5112 | 253 | localizeRight(); |
j_j205 | 6:d2da4d4b5112 | 254 | } |
j_j205 | 6:d2da4d4b5112 | 255 | |
j_j205 | 6:d2da4d4b5112 | 256 | else if ((distLocalR <= 10.5) && (distLocalR >= 5.0)) // wall in range |
j_j205 | 6:d2da4d4b5112 | 257 | { |
j_j205 | 6:d2da4d4b5112 | 258 | localizeLeft(); |
j_j205 | 6:d2da4d4b5112 | 259 | } |
j_j205 | 1:a53d97b74fab | 260 | } |
j_j205 | 1:a53d97b74fab | 261 | } |
j_j205 | 1:a53d97b74fab | 262 | |
j_j205 | 0:fd72f6df078c | 263 | // utility function |
j_j205 | 0:fd72f6df078c | 264 | void Navigation::printPrevious(Serial &pc) |
j_j205 | 0:fd72f6df078c | 265 | { |
j_j205 | 0:fd72f6df078c | 266 | for(int i = 0; i < previous.size(); i++) |
j_j205 | 0:fd72f6df078c | 267 | { |
j_j205 | 2:17bd430aeca1 | 268 | pc.printf("Node %f is prev to node %f\n", previous[i], i); |
j_j205 | 0:fd72f6df078c | 269 | } |
j_j205 | 0:fd72f6df078c | 270 | } |
j_j205 | 0:fd72f6df078c | 271 | |
j_j205 | 0:fd72f6df078c | 272 | // utility function |
j_j205 | 0:fd72f6df078c | 273 | void Navigation::printRoute(Serial &pc) |
j_j205 | 0:fd72f6df078c | 274 | { |
j_j205 | 0:fd72f6df078c | 275 | while (!route.empty() ) |
j_j205 | 0:fd72f6df078c | 276 | { |
j_j205 | 0:fd72f6df078c | 277 | pc.printf("Go to node %i\n", route.top() ); |
j_j205 | 0:fd72f6df078c | 278 | route.pop(); |
j_j205 | 0:fd72f6df078c | 279 | } |
j_j205 | 0:fd72f6df078c | 280 | } |
j_j205 | 0:fd72f6df078c | 281 | |
j_j205 | 0:fd72f6df078c | 282 | // utility function |
j_j205 | 0:fd72f6df078c | 283 | void Navigation::printGraph(Serial &pc) |
j_j205 | 0:fd72f6df078c | 284 | { |
j_j205 | 0:fd72f6df078c | 285 | for (int i = 0; i < graph.size(); i++) |
j_j205 | 0:fd72f6df078c | 286 | for (int j = 0; j < graph[i].size(); j++) |
j_j205 | 0:fd72f6df078c | 287 | { |
j_j205 | 2:17bd430aeca1 | 288 | 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 | 289 | } |
j_j205 | 5:d0954e0aecc9 | 290 | } |
j_j205 | 6:d2da4d4b5112 | 291 | |
j_j205 | 6:d2da4d4b5112 | 292 | // FUNCTION: |
j_j205 | 6:d2da4d4b5112 | 293 | // void localizeRight() |
j_j205 | 6:d2da4d4b5112 | 294 | // IN-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 295 | // None |
j_j205 | 6:d2da4d4b5112 | 296 | // OUT-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 297 | // None |
j_j205 | 6:d2da4d4b5112 | 298 | // DESCRIPTION: |
j_j205 | 6:d2da4d4b5112 | 299 | // Using sensor longRangeL this method will localize to the wall |
j_j205 | 6:d2da4d4b5112 | 300 | // to the right |
j_j205 | 6:d2da4d4b5112 | 301 | void Navigation::localizeRight() |
j_j205 | 6:d2da4d4b5112 | 302 | { |
j_j205 | 6:d2da4d4b5112 | 303 | if (distLocalL >= 7.1) // too close left, turn right |
j_j205 | 6:d2da4d4b5112 | 304 | { |
j_j205 | 6:d2da4d4b5112 | 305 | drive.move(0, (2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 306 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 307 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 308 | { |
j_j205 | 6:d2da4d4b5112 | 309 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 310 | } |
j_j205 | 6:d2da4d4b5112 | 311 | pc.printf("\n Adjusted angle right, in localizeRight"); |
j_j205 | 6:d2da4d4b5112 | 312 | } |
j_j205 | 6:d2da4d4b5112 | 313 | |
j_j205 | 6:d2da4d4b5112 | 314 | else if (distLocalL <= 5.9) // too close right, turn left |
j_j205 | 6:d2da4d4b5112 | 315 | { |
j_j205 | 6:d2da4d4b5112 | 316 | drive.move(0, (-2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 317 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 318 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 319 | { |
j_j205 | 6:d2da4d4b5112 | 320 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 321 | } |
j_j205 | 6:d2da4d4b5112 | 322 | pc.printf("\n Adjusted angle left , in localizeRight"); |
j_j205 | 6:d2da4d4b5112 | 323 | } |
j_j205 | 6:d2da4d4b5112 | 324 | |
j_j205 | 6:d2da4d4b5112 | 325 | else |
j_j205 | 6:d2da4d4b5112 | 326 | { |
j_j205 | 6:d2da4d4b5112 | 327 | /* do nothing */ |
j_j205 | 6:d2da4d4b5112 | 328 | pc.printf("\n No need to adjust, in localizeRight"); |
j_j205 | 6:d2da4d4b5112 | 329 | } |
j_j205 | 6:d2da4d4b5112 | 330 | } |
j_j205 | 6:d2da4d4b5112 | 331 | |
j_j205 | 6:d2da4d4b5112 | 332 | // FUNCTION: |
j_j205 | 6:d2da4d4b5112 | 333 | // void localizeLeft() |
j_j205 | 6:d2da4d4b5112 | 334 | // IN-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 335 | // None |
j_j205 | 6:d2da4d4b5112 | 336 | // OUT-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 337 | // None |
j_j205 | 6:d2da4d4b5112 | 338 | // DESCRIPTION: |
j_j205 | 6:d2da4d4b5112 | 339 | // Using sensor longRangeR this method will localize to the wall |
j_j205 | 6:d2da4d4b5112 | 340 | // to the left during stretches of forward motion where robot |
j_j205 | 6:d2da4d4b5112 | 341 | // should remain xxx distance from the left wall. |
j_j205 | 6:d2da4d4b5112 | 342 | void Navigation::localizeLeft() |
j_j205 | 6:d2da4d4b5112 | 343 | { |
j_j205 | 6:d2da4d4b5112 | 344 | if (distLocalR >= 9.7) // too close right, turn left |
j_j205 | 6:d2da4d4b5112 | 345 | { |
j_j205 | 6:d2da4d4b5112 | 346 | drive.move(0.0, (-2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 347 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 348 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 349 | { |
j_j205 | 6:d2da4d4b5112 | 350 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 351 | } |
j_j205 | 6:d2da4d4b5112 | 352 | pc.printf("\n Adjusted angle left, in localizeLeft"); |
j_j205 | 6:d2da4d4b5112 | 353 | } |
j_j205 | 6:d2da4d4b5112 | 354 | |
j_j205 | 6:d2da4d4b5112 | 355 | else if (distLocalR <= 8.3) // too close left, turn right |
j_j205 | 6:d2da4d4b5112 | 356 | { |
j_j205 | 6:d2da4d4b5112 | 357 | drive.move(0.0, (2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 358 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 359 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 360 | { |
j_j205 | 6:d2da4d4b5112 | 361 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 362 | } |
j_j205 | 6:d2da4d4b5112 | 363 | pc.printf("\n Adjusted angle right, in localizeLeft"); |
j_j205 | 6:d2da4d4b5112 | 364 | } |
j_j205 | 6:d2da4d4b5112 | 365 | |
j_j205 | 6:d2da4d4b5112 | 366 | else |
j_j205 | 6:d2da4d4b5112 | 367 | { |
j_j205 | 6:d2da4d4b5112 | 368 | /* do nothing */ |
j_j205 | 6:d2da4d4b5112 | 369 | pc.printf("\n No need to adjust, in localizeLeft"); |
j_j205 | 6:d2da4d4b5112 | 370 | } |
j_j205 | 6:d2da4d4b5112 | 371 | } |
j_j205 | 6:d2da4d4b5112 | 372 | |
j_j205 | 6:d2da4d4b5112 | 373 | // FUNCTION: |
j_j205 | 6:d2da4d4b5112 | 374 | // void localizeRightReverse() |
j_j205 | 6:d2da4d4b5112 | 375 | // IN-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 376 | // None |
j_j205 | 6:d2da4d4b5112 | 377 | // OUT-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 378 | // None |
j_j205 | 6:d2da4d4b5112 | 379 | // DESCRIPTION: |
j_j205 | 6:d2da4d4b5112 | 380 | // Using sensor longRangeL this method will localize to the wall |
j_j205 | 6:d2da4d4b5112 | 381 | // to the right |
j_j205 | 6:d2da4d4b5112 | 382 | void Navigation::localizeRightReverse() |
j_j205 | 6:d2da4d4b5112 | 383 | { |
j_j205 | 6:d2da4d4b5112 | 384 | if (distLocalL >= 7.2) // too close left, turn left |
j_j205 | 6:d2da4d4b5112 | 385 | { |
j_j205 | 6:d2da4d4b5112 | 386 | drive.move(0, (-2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 387 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 388 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 389 | { |
j_j205 | 6:d2da4d4b5112 | 390 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 391 | } |
j_j205 | 6:d2da4d4b5112 | 392 | pc.printf("\n Adjusted angle left, in localizeRightReverse"); |
j_j205 | 6:d2da4d4b5112 | 393 | } |
j_j205 | 6:d2da4d4b5112 | 394 | |
j_j205 | 6:d2da4d4b5112 | 395 | else if (distLocalL <= 6.2) // too close right, turn right |
j_j205 | 6:d2da4d4b5112 | 396 | { |
j_j205 | 6:d2da4d4b5112 | 397 | drive.move(0, (2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 398 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 399 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 400 | { |
j_j205 | 6:d2da4d4b5112 | 401 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 402 | } |
j_j205 | 6:d2da4d4b5112 | 403 | pc.printf("\n Adjusted angle right, in localizeRightReverse"); |
j_j205 | 6:d2da4d4b5112 | 404 | } |
j_j205 | 6:d2da4d4b5112 | 405 | |
j_j205 | 6:d2da4d4b5112 | 406 | else |
j_j205 | 6:d2da4d4b5112 | 407 | { |
j_j205 | 6:d2da4d4b5112 | 408 | /* do nothing */ |
j_j205 | 6:d2da4d4b5112 | 409 | pc.printf("\n No need to adjust, in localizeRightReverse"); |
j_j205 | 6:d2da4d4b5112 | 410 | } |
j_j205 | 6:d2da4d4b5112 | 411 | } |
j_j205 | 6:d2da4d4b5112 | 412 | |
j_j205 | 6:d2da4d4b5112 | 413 | // FUNCTION: |
j_j205 | 6:d2da4d4b5112 | 414 | // void localizeLeftReverse() |
j_j205 | 6:d2da4d4b5112 | 415 | // IN-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 416 | // None |
j_j205 | 6:d2da4d4b5112 | 417 | // OUT-PARAMETERS: |
j_j205 | 6:d2da4d4b5112 | 418 | // None |
j_j205 | 6:d2da4d4b5112 | 419 | // DESCRIPTION: |
j_j205 | 6:d2da4d4b5112 | 420 | // Using sensor longRangeR this method will localize to the wall |
j_j205 | 6:d2da4d4b5112 | 421 | // to the left during stretches of forward motion where robot |
j_j205 | 6:d2da4d4b5112 | 422 | // should remain xxx distance from the left wall. |
j_j205 | 6:d2da4d4b5112 | 423 | void Navigation::localizeLeftReverse() |
j_j205 | 6:d2da4d4b5112 | 424 | { |
j_j205 | 6:d2da4d4b5112 | 425 | if (distLocalR >= 9.7) // toos close right, turn right |
j_j205 | 6:d2da4d4b5112 | 426 | { |
j_j205 | 6:d2da4d4b5112 | 427 | drive.move(0.0, (2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 428 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 429 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 430 | { |
j_j205 | 6:d2da4d4b5112 | 431 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 432 | } |
j_j205 | 6:d2da4d4b5112 | 433 | pc.printf("\n Adjusted angle right, in localizeLeftReverse"); |
j_j205 | 6:d2da4d4b5112 | 434 | } |
j_j205 | 6:d2da4d4b5112 | 435 | |
j_j205 | 6:d2da4d4b5112 | 436 | else if (distLocalR <= 8.3) // too close left, turn left |
j_j205 | 6:d2da4d4b5112 | 437 | { |
j_j205 | 6:d2da4d4b5112 | 438 | drive.move(0.0, (-2.0)*(3.14159 / 180.0)); |
j_j205 | 6:d2da4d4b5112 | 439 | // wait for move to complete |
j_j205 | 6:d2da4d4b5112 | 440 | while(!drive.isMoveDone()) |
j_j205 | 6:d2da4d4b5112 | 441 | { |
j_j205 | 6:d2da4d4b5112 | 442 | wait(1e-6); |
j_j205 | 6:d2da4d4b5112 | 443 | } |
j_j205 | 6:d2da4d4b5112 | 444 | pc.printf("\n Adjusted angle left, in localizeLeftReverse"); |
j_j205 | 6:d2da4d4b5112 | 445 | } |
j_j205 | 6:d2da4d4b5112 | 446 | |
j_j205 | 6:d2da4d4b5112 | 447 | else |
j_j205 | 6:d2da4d4b5112 | 448 | { |
j_j205 | 6:d2da4d4b5112 | 449 | /* do nothing */ |
j_j205 | 6:d2da4d4b5112 | 450 | pc.printf("\n No need to adjust, in localizeLeftReverse"); |
j_j205 | 6:d2da4d4b5112 | 451 | } |
j_j205 | 6:d2da4d4b5112 | 452 | } |