Helvijs Kiselis
/
Micromouse
Algorithmus
main.cpp@32:e984b7959cb0, 2018-05-23 (annotated)
- Committer:
- Helvis
- Date:
- Wed May 23 01:34:36 2018 +0000
- Revision:
- 32:e984b7959cb0
- Parent:
- 31:2c54f8304ef5
- Child:
- 33:836ab2089565
v2.5 we hope
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helvis | 28:b7ce1e3bf08b | 1 | /*----------------------------------------------------------------------------* |
Helvis | 28:b7ce1e3bf08b | 2 | * Micromouse PES2 |
Helvis | 28:b7ce1e3bf08b | 3 | * |
Helvis | 28:b7ce1e3bf08b | 4 | * PES2 2018, TEAM 3 |
Helvis | 1:2b5f79285a3e | 5 | * |
Helvis | 28:b7ce1e3bf08b | 6 | * Code entwickelt von: |
Helvis | 28:b7ce1e3bf08b | 7 | * |
Helvis | 1:2b5f79285a3e | 8 | * |
Helvis | 28:b7ce1e3bf08b | 9 | *-----------------------------------------------------------------------------*/ |
Helvis | 1:2b5f79285a3e | 10 | |
Helvis | 0:8491169be8fc | 11 | #include <mbed.h> |
Helvis | 0:8491169be8fc | 12 | #include "EncoderCounter.h" |
Helvis | 0:8491169be8fc | 13 | #include "Controller.h" |
Helvis | 0:8491169be8fc | 14 | #include "IRSensor.h" |
Helvis | 0:8491169be8fc | 15 | #include "Motion.h" |
Helvis | 0:8491169be8fc | 16 | |
Helvis | 0:8491169be8fc | 17 | //User Button |
Helvis | 0:8491169be8fc | 18 | InterruptIn button(USER_BUTTON); |
Helvis | 0:8491169be8fc | 19 | |
Helvis | 0:8491169be8fc | 20 | //Sensors: |
Helvis | 0:8491169be8fc | 21 | |
Helvis | 19:6cd6cc5c8b4c | 22 | AnalogIn lineSensor(PC_5); |
Helvis | 0:8491169be8fc | 23 | AnalogIn distance2(PC_3); |
Helvis | 0:8491169be8fc | 24 | AnalogIn distance4(PB_1); |
Helvis | 0:8491169be8fc | 25 | AnalogIn distance1(PC_2); |
Helvis | 0:8491169be8fc | 26 | |
Helvis | 0:8491169be8fc | 27 | IRSensor irSensorL (distance2); |
Helvis | 0:8491169be8fc | 28 | IRSensor irSensorC (distance4); |
Helvis | 0:8491169be8fc | 29 | IRSensor irSensorR (distance1); |
Helvis | 16:c5b864804632 | 30 | |
Helvis | 16:c5b864804632 | 31 | Timer t1; |
Helvis | 0:8491169be8fc | 32 | |
Helvis | 0:8491169be8fc | 33 | //Motors: |
Helvis | 0:8491169be8fc | 34 | DigitalOut myled(LED1); |
Helvis | 0:8491169be8fc | 35 | |
Helvis | 0:8491169be8fc | 36 | DigitalOut enableMotorDriver(PB_2); |
Helvis | 0:8491169be8fc | 37 | |
Helvis | 0:8491169be8fc | 38 | DigitalIn motorDriverFault(PB_14); |
Helvis | 0:8491169be8fc | 39 | DigitalIn motorDriverWarning(PB_15); |
Helvis | 0:8491169be8fc | 40 | |
Helvis | 0:8491169be8fc | 41 | PwmOut pwmRight(PA_8); |
Helvis | 0:8491169be8fc | 42 | PwmOut pwmLeft(PA_10); |
Helvis | 0:8491169be8fc | 43 | |
Helvis | 0:8491169be8fc | 44 | EncoderCounter counterRight(PB_6, PB_7); |
Helvis | 0:8491169be8fc | 45 | EncoderCounter counterLeft(PA_0, PA_1); |
Helvis | 0:8491169be8fc | 46 | |
Helvis | 0:8491169be8fc | 47 | |
Helvis | 0:8491169be8fc | 48 | Controller controller(pwmLeft, pwmRight, counterLeft, counterRight); |
Helvis | 0:8491169be8fc | 49 | |
Helvis | 1:2b5f79285a3e | 50 | Motion motion(controller, counterLeft, counterRight, irSensorL, irSensorC, |
Helvis | 1:2b5f79285a3e | 51 | irSensorR, lineSensor, enableMotorDriver); |
Helvis | 0:8491169be8fc | 52 | |
Helvis | 0:8491169be8fc | 53 | //------------------------------------------------------------------------------ |
Helvis | 0:8491169be8fc | 54 | |
Helvis | 0:8491169be8fc | 55 | volatile int start = 0; |
Helvis | 1:2b5f79285a3e | 56 | |
Helvis | 32:e984b7959cb0 | 57 | const int debuging = 0; |
Helvis | 31:2c54f8304ef5 | 58 | |
Helvis | 1:2b5f79285a3e | 59 | const int MOVE = 1; |
Helvis | 1:2b5f79285a3e | 60 | const int LEFT = 2; |
Helvis | 1:2b5f79285a3e | 61 | const int RIGHT = 3; |
Helvis | 23:accd07ca2da7 | 62 | const int HALF_MOVE = 4; |
Helvis | 23:accd07ca2da7 | 63 | const int TURN_LEFT = 5; |
Helvis | 23:accd07ca2da7 | 64 | const int TURN_RIGHT = 6; |
Helvis | 23:accd07ca2da7 | 65 | const int EMPTY = 7; |
Helvis | 1:2b5f79285a3e | 66 | |
Helvis | 1:2b5f79285a3e | 67 | //Sensor tresholds [mm] |
Helvis | 31:2c54f8304ef5 | 68 | const float thresholdL = 70; |
Helvis | 31:2c54f8304ef5 | 69 | const float thresholdR = 70; |
Helvis | 31:2c54f8304ef5 | 70 | const float thresholdC = 80; |
Helvis | 0:8491169be8fc | 71 | |
Helvis | 0:8491169be8fc | 72 | //------------------------------------------------------------------------------ |
Helvis | 0:8491169be8fc | 73 | |
Helvis | 0:8491169be8fc | 74 | //User button toggle |
Helvis | 0:8491169be8fc | 75 | void press() { |
Helvis | 0:8491169be8fc | 76 | start = !start; |
Helvis | 0:8491169be8fc | 77 | } |
Helvis | 1:2b5f79285a3e | 78 | |
Helvis | 0:8491169be8fc | 79 | //------------------------------------------------------------------------------ |
Helvis | 0:8491169be8fc | 80 | |
Helvis | 0:8491169be8fc | 81 | int main() { |
Helvis | 1:2b5f79285a3e | 82 | //Init |
Helvis | 0:8491169be8fc | 83 | |
Helvis | 12:75d0291a9785 | 84 | int route[200] = {0}; |
Helvis | 12:75d0291a9785 | 85 | int r = 0; |
Helvis | 1:2b5f79285a3e | 86 | |
Helvis | 18:3309329d5f42 | 87 | int junction[20] = {0}; |
Helvis | 18:3309329d5f42 | 88 | int j = 0; |
Helvis | 0:8491169be8fc | 89 | |
Helvis | 1:2b5f79285a3e | 90 | short lWall; |
Helvis | 1:2b5f79285a3e | 91 | short cWall; |
Helvis | 1:2b5f79285a3e | 92 | short rWall; |
Helvis | 0:8491169be8fc | 93 | |
Helvis | 1:2b5f79285a3e | 94 | short Ziel = 0; |
Helvis | 1:2b5f79285a3e | 95 | |
Helvis | 12:75d0291a9785 | 96 | //infinite loop |
luethale | 9:a01f90f88920 | 97 | while(1) { |
Helvis | 0:8491169be8fc | 98 | |
luethale | 9:a01f90f88920 | 99 | button.fall(&press); //User button einlesen |
Helvis | 21:41997651337a | 100 | |
Helvis | 21:41997651337a | 101 | /*-----------------------------------------------------------------------------* |
Helvis | 21:41997651337a | 102 | * |
Helvis | 21:41997651337a | 103 | * Search run |
Helvis | 21:41997651337a | 104 | * |
Helvis | 21:41997651337a | 105 | *-----------------------------------------------------------------------------*/ |
Helvis | 1:2b5f79285a3e | 106 | |
luethale | 9:a01f90f88920 | 107 | while(start == 1 && Ziel == 0) { |
Helvis | 1:2b5f79285a3e | 108 | |
Helvis | 1:2b5f79285a3e | 109 | |
luethale | 9:a01f90f88920 | 110 | float distanceL = irSensorL.readL(); |
luethale | 9:a01f90f88920 | 111 | float distanceC = irSensorC.readC(); |
luethale | 9:a01f90f88920 | 112 | float distanceR = irSensorR.readR(); |
Helvis | 0:8491169be8fc | 113 | |
luethale | 9:a01f90f88920 | 114 | //Wall check |
luethale | 9:a01f90f88920 | 115 | if (distanceL < thresholdL) lWall = 1; |
luethale | 9:a01f90f88920 | 116 | else lWall = 0; |
luethale | 9:a01f90f88920 | 117 | if (distanceC < thresholdC) cWall = 1; |
luethale | 9:a01f90f88920 | 118 | else cWall = 0; |
luethale | 9:a01f90f88920 | 119 | if (distanceR < thresholdR) rWall = 1; |
luethale | 9:a01f90f88920 | 120 | else rWall = 0; |
Helvis | 0:8491169be8fc | 121 | |
luethale | 9:a01f90f88920 | 122 | //Junction Check |
luethale | 9:a01f90f88920 | 123 | if ((lWall + cWall + rWall) < 2) { |
Helvis | 11:2960fc540616 | 124 | |
Helvis | 11:2960fc540616 | 125 | if (junction[j] != r) { |
Helvis | 11:2960fc540616 | 126 | if (junction[j] > 0) { |
Helvis | 11:2960fc540616 | 127 | j += 1; |
Helvis | 11:2960fc540616 | 128 | junction[j] = r; |
Helvis | 11:2960fc540616 | 129 | }else{ |
Helvis | 11:2960fc540616 | 130 | junction[j] = r; |
Helvis | 11:2960fc540616 | 131 | } |
luethale | 9:a01f90f88920 | 132 | } |
Helvis | 8:8131269dc46e | 133 | |
Helvis | 31:2c54f8304ef5 | 134 | if (debuging == 1) printf("Kreuzung: %d Schritt: %d\n", j, r); |
luethale | 9:a01f90f88920 | 135 | } |
luethale | 20:20573f55a5fd | 136 | |
Helvis | 12:75d0291a9785 | 137 | //No wall left |
luethale | 9:a01f90f88920 | 138 | if (lWall == 0) { |
Helvis | 0:8491169be8fc | 139 | |
luethale | 9:a01f90f88920 | 140 | if (route[r] == LEFT) { |
Helvis | 1:2b5f79285a3e | 141 | |
luethale | 9:a01f90f88920 | 142 | route[r] = MOVE; |
Helvis | 31:2c54f8304ef5 | 143 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 144 | r++; |
Helvis | 1:2b5f79285a3e | 145 | |
luethale | 9:a01f90f88920 | 146 | motion.rotateL(); |
luethale | 9:a01f90f88920 | 147 | motion.scanMove(); |
luethale | 20:20573f55a5fd | 148 | |
luethale | 20:20573f55a5fd | 149 | }else if (route[r] == MOVE) { |
luethale | 20:20573f55a5fd | 150 | |
luethale | 20:20573f55a5fd | 151 | route[r] = RIGHT; |
Helvis | 31:2c54f8304ef5 | 152 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 20:20573f55a5fd | 153 | r++; |
luethale | 20:20573f55a5fd | 154 | route[r] = MOVE; |
Helvis | 31:2c54f8304ef5 | 155 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 20:20573f55a5fd | 156 | r++; |
luethale | 20:20573f55a5fd | 157 | |
luethale | 20:20573f55a5fd | 158 | motion.rotateL(); |
luethale | 20:20573f55a5fd | 159 | motion.scanMove(); |
Helvis | 1:2b5f79285a3e | 160 | |
luethale | 9:a01f90f88920 | 161 | }else if (route[r] == RIGHT) { |
luethale | 9:a01f90f88920 | 162 | // Kreuzung führt zu Sackgassen -> löschen |
luethale | 20:20573f55a5fd | 163 | junction[j] = 0; |
Helvis | 31:2c54f8304ef5 | 164 | printf("Kreuzung %d Schritt %d geloscht\n", j, r); |
Helvis | 11:2960fc540616 | 165 | j -= 1; |
Helvis | 32:e984b7959cb0 | 166 | |
Helvis | 32:e984b7959cb0 | 167 | bool looping = false; |
Helvis | 8:8131269dc46e | 168 | |
Helvis | 31:2c54f8304ef5 | 169 | while (junction[j] <= r ) { |
Helvis | 7:22392ed60534 | 170 | |
luethale | 9:a01f90f88920 | 171 | //invert rotation |
Helvis | 7:22392ed60534 | 172 | |
Helvis | 31:2c54f8304ef5 | 173 | if (junction[j] == r) { |
luethale | 9:a01f90f88920 | 174 | |
Helvis | 31:2c54f8304ef5 | 175 | switch (route[r]) { |
Helvis | 31:2c54f8304ef5 | 176 | case MOVE: |
Helvis | 32:e984b7959cb0 | 177 | if (looping == false) { |
Helvis | 31:2c54f8304ef5 | 178 | motion.runTask(route, r, true, junction[j]); |
Helvis | 31:2c54f8304ef5 | 179 | if (debuging == 1) printf("-Schritt: %d, Befehl: %d\n", r, route[r]); |
Helvis | 32:e984b7959cb0 | 180 | } |
Helvis | 31:2c54f8304ef5 | 181 | r--; |
Helvis | 31:2c54f8304ef5 | 182 | break; |
Helvis | 31:2c54f8304ef5 | 183 | case LEFT: |
Helvis | 31:2c54f8304ef5 | 184 | r--; |
Helvis | 31:2c54f8304ef5 | 185 | break; |
Helvis | 31:2c54f8304ef5 | 186 | case RIGHT: |
Helvis | 31:2c54f8304ef5 | 187 | r--; |
Helvis | 31:2c54f8304ef5 | 188 | break; |
Helvis | 31:2c54f8304ef5 | 189 | } |
Helvis | 31:2c54f8304ef5 | 190 | motion.stop(); |
Helvis | 31:2c54f8304ef5 | 191 | t1.reset(); |
Helvis | 31:2c54f8304ef5 | 192 | t1.start(); |
Helvis | 31:2c54f8304ef5 | 193 | while (t1 < 0.5f) {} |
Helvis | 31:2c54f8304ef5 | 194 | t1.stop(); |
Helvis | 31:2c54f8304ef5 | 195 | |
Helvis | 31:2c54f8304ef5 | 196 | }else{ |
Helvis | 31:2c54f8304ef5 | 197 | if (route[r] == LEFT) { |
Helvis | 31:2c54f8304ef5 | 198 | |
Helvis | 31:2c54f8304ef5 | 199 | route[r] = RIGHT; |
Helvis | 31:2c54f8304ef5 | 200 | |
Helvis | 31:2c54f8304ef5 | 201 | }else if (route[r] == RIGHT) { |
Helvis | 31:2c54f8304ef5 | 202 | |
Helvis | 31:2c54f8304ef5 | 203 | route[r] = LEFT; |
Helvis | 32:e984b7959cb0 | 204 | |
Helvis | 32:e984b7959cb0 | 205 | }else if (junction[j] == r-1 && route[r] == MOVE) { |
Helvis | 32:e984b7959cb0 | 206 | |
Helvis | 32:e984b7959cb0 | 207 | looping = true; |
Helvis | 32:e984b7959cb0 | 208 | |
Helvis | 32:e984b7959cb0 | 209 | }else if (junction[j] == r-2 && route[r] == MOVE && route[junction[j]] != MOVE) { |
Helvis | 32:e984b7959cb0 | 210 | |
Helvis | 32:e984b7959cb0 | 211 | route[r] = 0; |
Helvis | 32:e984b7959cb0 | 212 | r--; |
Helvis | 31:2c54f8304ef5 | 213 | } |
Helvis | 7:22392ed60534 | 214 | |
Helvis | 31:2c54f8304ef5 | 215 | motion.runTask(route, r, true, junction[j]); |
Helvis | 31:2c54f8304ef5 | 216 | if (debuging == 1) printf("-Schritt: %d, Befehl: %d\n", r, route[r]); |
Helvis | 31:2c54f8304ef5 | 217 | route[r] = 0; |
Helvis | 31:2c54f8304ef5 | 218 | r--; |
Helvis | 31:2c54f8304ef5 | 219 | } |
Helvis | 31:2c54f8304ef5 | 220 | } |
Helvis | 31:2c54f8304ef5 | 221 | r++; |
luethale | 9:a01f90f88920 | 222 | |
Helvis | 11:2960fc540616 | 223 | motion.stop(); |
Helvis | 31:2c54f8304ef5 | 224 | t1.reset(); |
Helvis | 31:2c54f8304ef5 | 225 | t1.start(); |
Helvis | 31:2c54f8304ef5 | 226 | while (t1 < 0.5f) {} |
Helvis | 31:2c54f8304ef5 | 227 | t1.stop(); |
Helvis | 31:2c54f8304ef5 | 228 | if (debuging == 1) printf("Loop stop\n"); |
Helvis | 11:2960fc540616 | 229 | |
luethale | 9:a01f90f88920 | 230 | }else{ |
Helvis | 11:2960fc540616 | 231 | |
luethale | 9:a01f90f88920 | 232 | route[r] = LEFT; |
Helvis | 31:2c54f8304ef5 | 233 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 234 | r++; |
luethale | 9:a01f90f88920 | 235 | route[r] = MOVE; |
Helvis | 31:2c54f8304ef5 | 236 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 237 | r++; |
Helvis | 1:2b5f79285a3e | 238 | |
Helvis | 4:932eb2d29206 | 239 | |
luethale | 9:a01f90f88920 | 240 | motion.rotateL(); |
luethale | 9:a01f90f88920 | 241 | motion.scanMove(); |
luethale | 9:a01f90f88920 | 242 | } |
luethale | 20:20573f55a5fd | 243 | |
luethale | 20:20573f55a5fd | 244 | //No wall center |
luethale | 9:a01f90f88920 | 245 | }else if (cWall == 0) { |
luethale | 9:a01f90f88920 | 246 | |
luethale | 9:a01f90f88920 | 247 | if (route[r] == LEFT) { |
luethale | 9:a01f90f88920 | 248 | |
luethale | 9:a01f90f88920 | 249 | route[r] = RIGHT; |
Helvis | 31:2c54f8304ef5 | 250 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 251 | r++; |
luethale | 9:a01f90f88920 | 252 | route[r] = MOVE; |
Helvis | 31:2c54f8304ef5 | 253 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 254 | r++; |
Helvis | 0:8491169be8fc | 255 | |
luethale | 9:a01f90f88920 | 256 | motion.scanMove(); |
luethale | 9:a01f90f88920 | 257 | |
luethale | 9:a01f90f88920 | 258 | }else if (route[r] == MOVE) { |
luethale | 9:a01f90f88920 | 259 | |
luethale | 20:20573f55a5fd | 260 | junction[j] = 0; |
Helvis | 31:2c54f8304ef5 | 261 | if (debuging == 1) printf("Kreuzung %d Schritt %d geloscht\n", j, r); |
Helvis | 11:2960fc540616 | 262 | j -= 1; |
Helvis | 32:e984b7959cb0 | 263 | bool looping = false; |
luethale | 9:a01f90f88920 | 264 | |
Helvis | 31:2c54f8304ef5 | 265 | while (junction[j] <= r ) { |
luethale | 9:a01f90f88920 | 266 | |
luethale | 9:a01f90f88920 | 267 | //invert rotation |
luethale | 9:a01f90f88920 | 268 | |
Helvis | 31:2c54f8304ef5 | 269 | if (junction[j] == r) { |
luethale | 9:a01f90f88920 | 270 | |
Helvis | 31:2c54f8304ef5 | 271 | switch (route[r]) { |
Helvis | 31:2c54f8304ef5 | 272 | case MOVE: |
Helvis | 32:e984b7959cb0 | 273 | if (looping == false) { |
Helvis | 31:2c54f8304ef5 | 274 | motion.runTask(route, r, true, junction[j]); |
Helvis | 31:2c54f8304ef5 | 275 | if (debuging == 1) printf("-Schritt: %d, Befehl: %d\n", r, route[r]); |
Helvis | 32:e984b7959cb0 | 276 | } |
Helvis | 31:2c54f8304ef5 | 277 | r--; |
Helvis | 31:2c54f8304ef5 | 278 | break; |
Helvis | 31:2c54f8304ef5 | 279 | case LEFT: |
Helvis | 31:2c54f8304ef5 | 280 | r--; |
Helvis | 31:2c54f8304ef5 | 281 | break; |
Helvis | 31:2c54f8304ef5 | 282 | case RIGHT: |
Helvis | 31:2c54f8304ef5 | 283 | r--; |
Helvis | 31:2c54f8304ef5 | 284 | break; |
Helvis | 31:2c54f8304ef5 | 285 | } |
Helvis | 31:2c54f8304ef5 | 286 | motion.stop(); |
Helvis | 31:2c54f8304ef5 | 287 | t1.reset(); |
Helvis | 31:2c54f8304ef5 | 288 | t1.start(); |
Helvis | 31:2c54f8304ef5 | 289 | while (t1 < 0.5f) {} |
Helvis | 31:2c54f8304ef5 | 290 | t1.stop(); |
Helvis | 31:2c54f8304ef5 | 291 | |
Helvis | 31:2c54f8304ef5 | 292 | }else{ |
Helvis | 31:2c54f8304ef5 | 293 | if (route[r] == LEFT) { |
Helvis | 31:2c54f8304ef5 | 294 | |
Helvis | 31:2c54f8304ef5 | 295 | route[r] = RIGHT; |
Helvis | 31:2c54f8304ef5 | 296 | |
Helvis | 31:2c54f8304ef5 | 297 | }else if (route[r] == RIGHT) { |
Helvis | 31:2c54f8304ef5 | 298 | |
Helvis | 31:2c54f8304ef5 | 299 | route[r] = LEFT; |
Helvis | 32:e984b7959cb0 | 300 | |
Helvis | 32:e984b7959cb0 | 301 | }else if (junction[j] == r-1 && route[r] == MOVE) { |
Helvis | 32:e984b7959cb0 | 302 | |
Helvis | 32:e984b7959cb0 | 303 | looping = true; |
Helvis | 32:e984b7959cb0 | 304 | |
Helvis | 32:e984b7959cb0 | 305 | }else if (junction[j] == r-2 && route[r] == MOVE && route[junction[j]] != MOVE) { |
Helvis | 32:e984b7959cb0 | 306 | |
Helvis | 32:e984b7959cb0 | 307 | route[r] = 0; |
Helvis | 32:e984b7959cb0 | 308 | r--; |
Helvis | 31:2c54f8304ef5 | 309 | } |
Helvis | 11:2960fc540616 | 310 | |
Helvis | 31:2c54f8304ef5 | 311 | motion.runTask(route, r, true, junction[j]); |
Helvis | 31:2c54f8304ef5 | 312 | if (debuging == 1) printf("-Schritt: %d, Befehl: %d\n", r, route[r]); |
Helvis | 31:2c54f8304ef5 | 313 | route[r] = 0; |
Helvis | 31:2c54f8304ef5 | 314 | r--; |
Helvis | 31:2c54f8304ef5 | 315 | } |
Helvis | 21:41997651337a | 316 | } |
Helvis | 31:2c54f8304ef5 | 317 | r++; |
Helvis | 11:2960fc540616 | 318 | motion.stop(); |
Helvis | 31:2c54f8304ef5 | 319 | t1.reset(); |
Helvis | 31:2c54f8304ef5 | 320 | t1.start(); |
Helvis | 31:2c54f8304ef5 | 321 | while (t1 < 0.5f) {} |
Helvis | 31:2c54f8304ef5 | 322 | t1.stop(); |
Helvis | 31:2c54f8304ef5 | 323 | if (debuging == 1) printf("Loop stop\n"); |
luethale | 9:a01f90f88920 | 324 | |
luethale | 9:a01f90f88920 | 325 | }else{ |
luethale | 9:a01f90f88920 | 326 | |
luethale | 9:a01f90f88920 | 327 | route[r] = MOVE; |
Helvis | 31:2c54f8304ef5 | 328 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 329 | r++; |
luethale | 9:a01f90f88920 | 330 | |
luethale | 9:a01f90f88920 | 331 | motion.scanMove(); |
luethale | 9:a01f90f88920 | 332 | } |
luethale | 20:20573f55a5fd | 333 | |
luethale | 9:a01f90f88920 | 334 | //No wall right |
luethale | 9:a01f90f88920 | 335 | }else if (rWall == 0) { |
luethale | 9:a01f90f88920 | 336 | |
Helvis | 1:2b5f79285a3e | 337 | route[r] = RIGHT; |
Helvis | 31:2c54f8304ef5 | 338 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
Helvis | 1:2b5f79285a3e | 339 | r++; |
Helvis | 7:22392ed60534 | 340 | route[r] = MOVE; |
Helvis | 31:2c54f8304ef5 | 341 | if (debuging == 1) printf("Schritt: %d, Befehl: %d\n", r, route[r]); |
Helvis | 7:22392ed60534 | 342 | r++; |
Helvis | 0:8491169be8fc | 343 | |
luethale | 9:a01f90f88920 | 344 | motion.rotateR(); |
Helvis | 1:2b5f79285a3e | 345 | motion.scanMove(); |
luethale | 9:a01f90f88920 | 346 | |
luethale | 9:a01f90f88920 | 347 | //Dead end routine |
luethale | 9:a01f90f88920 | 348 | }else if ((lWall + cWall + rWall) == 3) { |
luethale | 9:a01f90f88920 | 349 | |
luethale | 9:a01f90f88920 | 350 | motion.rotate180(); |
Helvis | 31:2c54f8304ef5 | 351 | if (debuging == 1) printf("Sackgasse Schritt: %d\n", r); |
luethale | 9:a01f90f88920 | 352 | r--; |
Helvis | 16:c5b864804632 | 353 | t1.reset(); |
Helvis | 16:c5b864804632 | 354 | t1.start(); |
Helvis | 17:8a8758bfe3c5 | 355 | while (t1 < 0.5f) {} |
Helvis | 16:c5b864804632 | 356 | t1.stop(); |
luethale | 9:a01f90f88920 | 357 | //Return to last junction |
Helvis | 11:2960fc540616 | 358 | while (junction[j] <= r ) { |
Helvis | 16:c5b864804632 | 359 | |
Helvis | 11:2960fc540616 | 360 | if (junction[j] == r) { |
luethale | 9:a01f90f88920 | 361 | |
luethale | 9:a01f90f88920 | 362 | switch (route[r]) { |
luethale | 9:a01f90f88920 | 363 | case MOVE: |
Helvis | 21:41997651337a | 364 | motion.runTask(route, r, true, junction[j]); |
Helvis | 31:2c54f8304ef5 | 365 | if (debuging == 1) printf("-Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 366 | r--; |
luethale | 9:a01f90f88920 | 367 | break; |
luethale | 9:a01f90f88920 | 368 | case LEFT: |
luethale | 9:a01f90f88920 | 369 | r--; |
luethale | 9:a01f90f88920 | 370 | break; |
luethale | 9:a01f90f88920 | 371 | case RIGHT: |
luethale | 9:a01f90f88920 | 372 | r--; |
luethale | 9:a01f90f88920 | 373 | break; |
luethale | 9:a01f90f88920 | 374 | } |
Helvis | 17:8a8758bfe3c5 | 375 | motion.stop(); |
Helvis | 17:8a8758bfe3c5 | 376 | t1.reset(); |
Helvis | 17:8a8758bfe3c5 | 377 | t1.start(); |
Helvis | 19:6cd6cc5c8b4c | 378 | while (t1 < 0.5f) {} |
Helvis | 17:8a8758bfe3c5 | 379 | t1.stop(); |
Helvis | 7:22392ed60534 | 380 | |
luethale | 9:a01f90f88920 | 381 | }else{ |
luethale | 9:a01f90f88920 | 382 | //invert rotation |
luethale | 9:a01f90f88920 | 383 | if (route[r] == LEFT) { |
luethale | 9:a01f90f88920 | 384 | |
luethale | 9:a01f90f88920 | 385 | route[r] = RIGHT; |
luethale | 9:a01f90f88920 | 386 | |
luethale | 9:a01f90f88920 | 387 | }else if (route[r] == RIGHT) { |
Helvis | 8:8131269dc46e | 388 | |
luethale | 9:a01f90f88920 | 389 | route[r] = LEFT; |
luethale | 9:a01f90f88920 | 390 | } |
luethale | 9:a01f90f88920 | 391 | //Run tasks in declining order |
Helvis | 21:41997651337a | 392 | motion.runTask(route, r, true, junction[j]); |
Helvis | 31:2c54f8304ef5 | 393 | if (debuging == 1) printf("-Schritt: %d, Befehl: %d\n", r, route[r]); |
luethale | 9:a01f90f88920 | 394 | route[r] = 0; |
luethale | 9:a01f90f88920 | 395 | r--; |
Helvis | 8:8131269dc46e | 396 | } |
luethale | 9:a01f90f88920 | 397 | } |
luethale | 9:a01f90f88920 | 398 | r++; |
luethale | 9:a01f90f88920 | 399 | } |
luethale | 9:a01f90f88920 | 400 | |
luethale | 20:20573f55a5fd | 401 | //finish line check |
luethale | 9:a01f90f88920 | 402 | if (motion.finish() == 1) { |
luethale | 9:a01f90f88920 | 403 | |
luethale | 9:a01f90f88920 | 404 | Ziel = 1; |
Helvis | 23:accd07ca2da7 | 405 | r = 1; |
Helvis | 23:accd07ca2da7 | 406 | //Convert rotations to smooth turns |
Helvis | 23:accd07ca2da7 | 407 | while (route[r] != 0) { |
Helvis | 23:accd07ca2da7 | 408 | //Convert pattern MOVE - ROTATE - MOVE |
Helvis | 23:accd07ca2da7 | 409 | if (route[r-1] == MOVE && (route[r] == LEFT || route[r] == RIGHT) && route[r+1] == MOVE) { |
Helvis | 23:accd07ca2da7 | 410 | |
Helvis | 23:accd07ca2da7 | 411 | route[r-1] = HALF_MOVE; |
Helvis | 23:accd07ca2da7 | 412 | route[r+1] = HALF_MOVE; |
Helvis | 23:accd07ca2da7 | 413 | |
Helvis | 23:accd07ca2da7 | 414 | if (route[r] == LEFT) route[r] = TURN_LEFT; |
Helvis | 23:accd07ca2da7 | 415 | else route[r] = TURN_RIGHT; |
Helvis | 23:accd07ca2da7 | 416 | //Convert consequent ROTATION to a smooth TURN |
Helvis | 23:accd07ca2da7 | 417 | }else if (route[r-1] == HALF_MOVE && (route[r] == LEFT || route[r] == RIGHT) && route[r+1] == MOVE) { |
Helvis | 23:accd07ca2da7 | 418 | |
Helvis | 23:accd07ca2da7 | 419 | route[r-1] = EMPTY; |
Helvis | 23:accd07ca2da7 | 420 | route[r+1] = HALF_MOVE; |
Helvis | 23:accd07ca2da7 | 421 | |
Helvis | 23:accd07ca2da7 | 422 | if (route[r] == LEFT) route[r] = TURN_LEFT; |
Helvis | 23:accd07ca2da7 | 423 | else route[r] = TURN_RIGHT; |
Helvis | 23:accd07ca2da7 | 424 | |
Helvis | 23:accd07ca2da7 | 425 | } |
Helvis | 23:accd07ca2da7 | 426 | |
Helvis | 23:accd07ca2da7 | 427 | r++; |
Helvis | 23:accd07ca2da7 | 428 | } |
Helvis | 23:accd07ca2da7 | 429 | r = 0; |
luethale | 9:a01f90f88920 | 430 | start = 0; |
luethale | 9:a01f90f88920 | 431 | controller.counterReset(); |
Helvis | 21:41997651337a | 432 | myled = 1; |
luethale | 9:a01f90f88920 | 433 | }else{ |
luethale | 9:a01f90f88920 | 434 | Ziel = 0; |
luethale | 9:a01f90f88920 | 435 | } |
Helvis | 0:8491169be8fc | 436 | |
Helvis | 0:8491169be8fc | 437 | |
luethale | 9:a01f90f88920 | 438 | } |
Helvis | 0:8491169be8fc | 439 | |
Helvis | 21:41997651337a | 440 | /*-------------------------------------------------------------------------* |
Helvis | 21:41997651337a | 441 | * |
Helvis | 21:41997651337a | 442 | * Speed run |
Helvis | 21:41997651337a | 443 | * |
Helvis | 21:41997651337a | 444 | *-------------------------------------------------------------------------*/ |
Helvis | 1:2b5f79285a3e | 445 | |
luethale | 9:a01f90f88920 | 446 | while ( start == 1 && Ziel == 1 ) { |
Helvis | 0:8491169be8fc | 447 | |
Helvis | 18:3309329d5f42 | 448 | motion.runTask(route,r,false,junction[j]); |
luethale | 9:a01f90f88920 | 449 | r++; |
Helvis | 0:8491169be8fc | 450 | |
luethale | 9:a01f90f88920 | 451 | if (route[r] == 0) { |
luethale | 9:a01f90f88920 | 452 | //Weg fertig |
luethale | 9:a01f90f88920 | 453 | motion.stop(); |
Helvis | 21:41997651337a | 454 | start = 0; |
Helvis | 21:41997651337a | 455 | myled = 0; |
luethale | 9:a01f90f88920 | 456 | } |
luethale | 9:a01f90f88920 | 457 | } |
Helvis | 0:8491169be8fc | 458 | |
Helvis | 1:2b5f79285a3e | 459 | |
Helvis | 1:2b5f79285a3e | 460 | |
Helvis | 1:2b5f79285a3e | 461 | |
luethale | 9:a01f90f88920 | 462 | } |
luethale | 9:a01f90f88920 | 463 | } |