navigation updated with completed dijkstra's algo

Dependents:   R5 2016 Robotics Team 1

Committer:
j_j205
Date:
Fri Apr 08 01:35:28 2016 +0000
Revision:
7:f7489797746b
Parent:
6:d2da4d4b5112
Child:
8:290a110bcf0e
latest localization

Who changed what in which revision?

UserRevisionLine numberNew 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 7:f7489797746b 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 7:f7489797746b 223 if ((distLocalL <= 8.7) && (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 7:f7489797746b 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 7:f7489797746b 251 if ((distLocalL <= 8.7) && (distLocalR <= 10.5)) // both walls in range
j_j205 7:f7489797746b 252 {
j_j205 7:f7489797746b 253 newLocalize();
j_j205 7:f7489797746b 254 }
j_j205 7:f7489797746b 255
j_j205 7:f7489797746b 256 else if ((distLocalL <= 8.7) && (distLocalL >= 3.0)) // wall in range
j_j205 7:f7489797746b 257 {
j_j205 7:f7489797746b 258 localizeRight();
j_j205 7:f7489797746b 259 }
j_j205 6:d2da4d4b5112 260
j_j205 6:d2da4d4b5112 261 else if ((distLocalR <= 10.5) && (distLocalR >= 5.0)) // wall in range
j_j205 7:f7489797746b 262 {
j_j205 7:f7489797746b 263 localizeLeft();
j_j205 7:f7489797746b 264 }
j_j205 1:a53d97b74fab 265 }
j_j205 1:a53d97b74fab 266 }
j_j205 1:a53d97b74fab 267
j_j205 0:fd72f6df078c 268 // utility function
j_j205 0:fd72f6df078c 269 void Navigation::printPrevious(Serial &pc)
j_j205 0:fd72f6df078c 270 {
j_j205 0:fd72f6df078c 271 for(int i = 0; i < previous.size(); i++)
j_j205 0:fd72f6df078c 272 {
j_j205 2:17bd430aeca1 273 pc.printf("Node %f is prev to node %f\n", previous[i], i);
j_j205 0:fd72f6df078c 274 }
j_j205 0:fd72f6df078c 275 }
j_j205 0:fd72f6df078c 276
j_j205 0:fd72f6df078c 277 // utility function
j_j205 0:fd72f6df078c 278 void Navigation::printRoute(Serial &pc)
j_j205 0:fd72f6df078c 279 {
j_j205 0:fd72f6df078c 280 while (!route.empty() )
j_j205 0:fd72f6df078c 281 {
j_j205 0:fd72f6df078c 282 pc.printf("Go to node %i\n", route.top() );
j_j205 0:fd72f6df078c 283 route.pop();
j_j205 0:fd72f6df078c 284 }
j_j205 0:fd72f6df078c 285 }
j_j205 0:fd72f6df078c 286
j_j205 0:fd72f6df078c 287 // utility function
j_j205 0:fd72f6df078c 288 void Navigation::printGraph(Serial &pc)
j_j205 0:fd72f6df078c 289 {
j_j205 0:fd72f6df078c 290 for (int i = 0; i < graph.size(); i++)
j_j205 0:fd72f6df078c 291 for (int j = 0; j < graph[i].size(); j++)
j_j205 0:fd72f6df078c 292 {
j_j205 2:17bd430aeca1 293 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 294 }
j_j205 5:d0954e0aecc9 295 }
j_j205 6:d2da4d4b5112 296
j_j205 6:d2da4d4b5112 297 // FUNCTION:
j_j205 6:d2da4d4b5112 298 // void localizeRight()
j_j205 6:d2da4d4b5112 299 // IN-PARAMETERS:
j_j205 6:d2da4d4b5112 300 // None
j_j205 6:d2da4d4b5112 301 // OUT-PARAMETERS:
j_j205 6:d2da4d4b5112 302 // None
j_j205 6:d2da4d4b5112 303 // DESCRIPTION:
j_j205 6:d2da4d4b5112 304 // Using sensor longRangeL this method will localize to the wall
j_j205 6:d2da4d4b5112 305 // to the right
j_j205 6:d2da4d4b5112 306 void Navigation::localizeRight()
j_j205 6:d2da4d4b5112 307 {
j_j205 7:f7489797746b 308 if (distLocalL >= 7.8) // very close left, turn more right
j_j205 7:f7489797746b 309 {
j_j205 7:f7489797746b 310 drive.move(0, (5.0)*(3.14159 / 180.0));
j_j205 7:f7489797746b 311 // wait for move to complete
j_j205 7:f7489797746b 312 while(!drive.isMoveDone())
j_j205 7:f7489797746b 313 {
j_j205 7:f7489797746b 314 wait(1e-6);
j_j205 7:f7489797746b 315 }
j_j205 7:f7489797746b 316 pc.printf("\n Adjusted angle more right, in localizeRight");
j_j205 7:f7489797746b 317 }
j_j205 7:f7489797746b 318
j_j205 7:f7489797746b 319 else if (distLocalL >= 6.9) // too close left, turn right
j_j205 6:d2da4d4b5112 320 {
j_j205 6:d2da4d4b5112 321 drive.move(0, (2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 322 // wait for move to complete
j_j205 6:d2da4d4b5112 323 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 324 {
j_j205 6:d2da4d4b5112 325 wait(1e-6);
j_j205 6:d2da4d4b5112 326 }
j_j205 6:d2da4d4b5112 327 pc.printf("\n Adjusted angle right, in localizeRight");
j_j205 6:d2da4d4b5112 328 }
j_j205 6:d2da4d4b5112 329
j_j205 7:f7489797746b 330 else if (distLocalL <= 5.5) // too close right, turn left
j_j205 6:d2da4d4b5112 331 {
j_j205 6:d2da4d4b5112 332 drive.move(0, (-2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 333 // wait for move to complete
j_j205 6:d2da4d4b5112 334 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 335 {
j_j205 6:d2da4d4b5112 336 wait(1e-6);
j_j205 6:d2da4d4b5112 337 }
j_j205 6:d2da4d4b5112 338 pc.printf("\n Adjusted angle left , in localizeRight");
j_j205 6:d2da4d4b5112 339 }
j_j205 6:d2da4d4b5112 340
j_j205 6:d2da4d4b5112 341 else
j_j205 6:d2da4d4b5112 342 {
j_j205 6:d2da4d4b5112 343 /* do nothing */
j_j205 6:d2da4d4b5112 344 pc.printf("\n No need to adjust, in localizeRight");
j_j205 6:d2da4d4b5112 345 }
j_j205 6:d2da4d4b5112 346 }
j_j205 6:d2da4d4b5112 347
j_j205 6:d2da4d4b5112 348 // FUNCTION:
j_j205 6:d2da4d4b5112 349 // void localizeLeft()
j_j205 6:d2da4d4b5112 350 // IN-PARAMETERS:
j_j205 6:d2da4d4b5112 351 // None
j_j205 6:d2da4d4b5112 352 // OUT-PARAMETERS:
j_j205 6:d2da4d4b5112 353 // None
j_j205 6:d2da4d4b5112 354 // DESCRIPTION:
j_j205 6:d2da4d4b5112 355 // Using sensor longRangeR this method will localize to the wall
j_j205 6:d2da4d4b5112 356 // to the left during stretches of forward motion where robot
j_j205 6:d2da4d4b5112 357 // should remain xxx distance from the left wall.
j_j205 6:d2da4d4b5112 358 void Navigation::localizeLeft()
j_j205 6:d2da4d4b5112 359 {
j_j205 6:d2da4d4b5112 360 if (distLocalR >= 9.7) // too close right, turn left
j_j205 6:d2da4d4b5112 361 {
j_j205 6:d2da4d4b5112 362 drive.move(0.0, (-2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 363 // wait for move to complete
j_j205 6:d2da4d4b5112 364 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 365 {
j_j205 6:d2da4d4b5112 366 wait(1e-6);
j_j205 6:d2da4d4b5112 367 }
j_j205 6:d2da4d4b5112 368 pc.printf("\n Adjusted angle left, in localizeLeft");
j_j205 6:d2da4d4b5112 369 }
j_j205 6:d2da4d4b5112 370
j_j205 6:d2da4d4b5112 371 else if (distLocalR <= 8.3) // too close left, turn right
j_j205 6:d2da4d4b5112 372 {
j_j205 6:d2da4d4b5112 373 drive.move(0.0, (2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 374 // wait for move to complete
j_j205 6:d2da4d4b5112 375 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 376 {
j_j205 6:d2da4d4b5112 377 wait(1e-6);
j_j205 6:d2da4d4b5112 378 }
j_j205 6:d2da4d4b5112 379 pc.printf("\n Adjusted angle right, in localizeLeft");
j_j205 6:d2da4d4b5112 380 }
j_j205 6:d2da4d4b5112 381
j_j205 6:d2da4d4b5112 382 else
j_j205 6:d2da4d4b5112 383 {
j_j205 6:d2da4d4b5112 384 /* do nothing */
j_j205 6:d2da4d4b5112 385 pc.printf("\n No need to adjust, in localizeLeft");
j_j205 6:d2da4d4b5112 386 }
j_j205 6:d2da4d4b5112 387 }
j_j205 6:d2da4d4b5112 388
j_j205 6:d2da4d4b5112 389 // FUNCTION:
j_j205 6:d2da4d4b5112 390 // void localizeRightReverse()
j_j205 6:d2da4d4b5112 391 // IN-PARAMETERS:
j_j205 6:d2da4d4b5112 392 // None
j_j205 6:d2da4d4b5112 393 // OUT-PARAMETERS:
j_j205 6:d2da4d4b5112 394 // None
j_j205 6:d2da4d4b5112 395 // DESCRIPTION:
j_j205 6:d2da4d4b5112 396 // Using sensor longRangeL this method will localize to the wall
j_j205 6:d2da4d4b5112 397 // to the right
j_j205 6:d2da4d4b5112 398 void Navigation::localizeRightReverse()
j_j205 6:d2da4d4b5112 399 {
j_j205 6:d2da4d4b5112 400 if (distLocalL >= 7.2) // too close left, turn left
j_j205 6:d2da4d4b5112 401 {
j_j205 6:d2da4d4b5112 402 drive.move(0, (-2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 403 // wait for move to complete
j_j205 6:d2da4d4b5112 404 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 405 {
j_j205 6:d2da4d4b5112 406 wait(1e-6);
j_j205 6:d2da4d4b5112 407 }
j_j205 6:d2da4d4b5112 408 pc.printf("\n Adjusted angle left, in localizeRightReverse");
j_j205 6:d2da4d4b5112 409 }
j_j205 6:d2da4d4b5112 410
j_j205 6:d2da4d4b5112 411 else if (distLocalL <= 6.2) // too close right, turn right
j_j205 6:d2da4d4b5112 412 {
j_j205 6:d2da4d4b5112 413 drive.move(0, (2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 414 // wait for move to complete
j_j205 6:d2da4d4b5112 415 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 416 {
j_j205 6:d2da4d4b5112 417 wait(1e-6);
j_j205 6:d2da4d4b5112 418 }
j_j205 6:d2da4d4b5112 419 pc.printf("\n Adjusted angle right, in localizeRightReverse");
j_j205 6:d2da4d4b5112 420 }
j_j205 6:d2da4d4b5112 421
j_j205 6:d2da4d4b5112 422 else
j_j205 6:d2da4d4b5112 423 {
j_j205 6:d2da4d4b5112 424 /* do nothing */
j_j205 6:d2da4d4b5112 425 pc.printf("\n No need to adjust, in localizeRightReverse");
j_j205 6:d2da4d4b5112 426 }
j_j205 6:d2da4d4b5112 427 }
j_j205 6:d2da4d4b5112 428
j_j205 6:d2da4d4b5112 429 // FUNCTION:
j_j205 6:d2da4d4b5112 430 // void localizeLeftReverse()
j_j205 6:d2da4d4b5112 431 // IN-PARAMETERS:
j_j205 6:d2da4d4b5112 432 // None
j_j205 6:d2da4d4b5112 433 // OUT-PARAMETERS:
j_j205 6:d2da4d4b5112 434 // None
j_j205 6:d2da4d4b5112 435 // DESCRIPTION:
j_j205 6:d2da4d4b5112 436 // Using sensor longRangeR this method will localize to the wall
j_j205 6:d2da4d4b5112 437 // to the left during stretches of forward motion where robot
j_j205 6:d2da4d4b5112 438 // should remain xxx distance from the left wall.
j_j205 6:d2da4d4b5112 439 void Navigation::localizeLeftReverse()
j_j205 6:d2da4d4b5112 440 {
j_j205 6:d2da4d4b5112 441 if (distLocalR >= 9.7) // toos close right, turn right
j_j205 6:d2da4d4b5112 442 {
j_j205 6:d2da4d4b5112 443 drive.move(0.0, (2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 444 // wait for move to complete
j_j205 6:d2da4d4b5112 445 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 446 {
j_j205 6:d2da4d4b5112 447 wait(1e-6);
j_j205 6:d2da4d4b5112 448 }
j_j205 6:d2da4d4b5112 449 pc.printf("\n Adjusted angle right, in localizeLeftReverse");
j_j205 6:d2da4d4b5112 450 }
j_j205 6:d2da4d4b5112 451
j_j205 6:d2da4d4b5112 452 else if (distLocalR <= 8.3) // too close left, turn left
j_j205 6:d2da4d4b5112 453 {
j_j205 6:d2da4d4b5112 454 drive.move(0.0, (-2.0)*(3.14159 / 180.0));
j_j205 6:d2da4d4b5112 455 // wait for move to complete
j_j205 6:d2da4d4b5112 456 while(!drive.isMoveDone())
j_j205 6:d2da4d4b5112 457 {
j_j205 6:d2da4d4b5112 458 wait(1e-6);
j_j205 6:d2da4d4b5112 459 }
j_j205 6:d2da4d4b5112 460 pc.printf("\n Adjusted angle left, in localizeLeftReverse");
j_j205 6:d2da4d4b5112 461 }
j_j205 6:d2da4d4b5112 462
j_j205 6:d2da4d4b5112 463 else
j_j205 6:d2da4d4b5112 464 {
j_j205 6:d2da4d4b5112 465 /* do nothing */
j_j205 6:d2da4d4b5112 466 pc.printf("\n No need to adjust, in localizeLeftReverse");
j_j205 6:d2da4d4b5112 467 }
j_j205 6:d2da4d4b5112 468 }
j_j205 7:f7489797746b 469
j_j205 7:f7489797746b 470 // FUNCTION:
j_j205 7:f7489797746b 471 // void newLocalize()
j_j205 7:f7489797746b 472 // IN-PARAMETERS:
j_j205 7:f7489797746b 473 // None
j_j205 7:f7489797746b 474 // OUT-PARAMETERS:
j_j205 7:f7489797746b 475 // None
j_j205 7:f7489797746b 476 // DESCRIPTION:
j_j205 7:f7489797746b 477 // Uses difference between sensors to adjust angle
j_j205 7:f7489797746b 478 void Navigation::newLocalize()
j_j205 7:f7489797746b 479 {
j_j205 7:f7489797746b 480 float k = 2.5;
j_j205 7:f7489797746b 481 float angleAdjust = k*(distLocalL - distLocalR);
j_j205 7:f7489797746b 482
j_j205 7:f7489797746b 483 drive.move(0, angleAdjust);
j_j205 7:f7489797746b 484 // wait for move to complete
j_j205 7:f7489797746b 485 while(!drive.isMoveDone())
j_j205 7:f7489797746b 486 {
j_j205 7:f7489797746b 487 wait(1e-6);
j_j205 7:f7489797746b 488 }
j_j205 7:f7489797746b 489 pc.printf("\n Adjusted angle by %f in newLocalize", angleAdjust);
j_j205 7:f7489797746b 490 }