-
Notifications
You must be signed in to change notification settings - Fork 1
/
zippy_the_fox.ino
558 lines (441 loc) · 11 KB
/
zippy_the_fox.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Included Libraries
#include <Motor.h>
#include <QTRSensors_teensy3.h>
#include <Encoder.h>
#include "config.h"
/*
Inline Functions
*/
// -- Status LEDs --
#define GREEN_LED_LEFT_ON digitalWrite(GREEN_LED_LEFT, HIGH)
#define GREEN_LED_LEFT_OFF digitalWrite(GREEN_LED_LEFT, LOW)
#define GREEN_LED_RIGHT_ON digitalWrite(GREEN_LED_RIGHT, HIGH)
#define GREEN_LED_RIGHT_OFF digitalWrite(GREEN_LED_RIGHT, LOW)
#define RED_LED_LEFT_ON digitalWrite(RED_LED_LEFT, HIGH)
#define RED_LED_LEFT_OFF digitalWrite(RED_LED_LEFT, LOW)
#define RED_LED_RIGHT_ON digitalWrite(RED_LED_RIGHT, HIGH)
#define RED_LED_RIGHT_OFF digitalWrite(RED_LED_RIGHT, LOW)
// -- Buttons --
#define LEFT_BUTTON_ON() digitalRead(LEFT_BUTTON)
#define RIGHT_BUTTON_ON() digitalRead(RIGHT_BUTTON)
// -- Motor Driver --
#define ENABLE_STANDBY digitalWrite(MOTOR_STANDBY_PIN, HIGH)
#define DISABLE_STANDBY digitalWrite(MOTOR_STANDBY_PIN, LOW)
// -- Sensors --
#define ON_LINE(sensor) (sensor<COMPARE)
#define FOUND_LEFT() (ON_LINE(lineSensorValues[0]) && ON_LINE(lineSensorValues[1]) && ON_LINE(lineSensorValues[2]) && ON_LINE(lineSensorValues[3]) && ON_LINE(lineSensorValues[4]))
#define FOUND_RIGHT() (ON_LINE(lineSensorValues[7]) && ON_LINE(lineSensorValues[6]) && ON_LINE(lineSensorValues[5]) && ON_LINE(lineSensorValues[4]) && ON_LINE(lineSensorValues[3]))
#define FOUND_STRAIGHT() ((ON_LINE(lineSensorValues[2]) || ON_LINE(lineSensorValues[3])) && (ON_LINE(lineSensorValues[4]) || ON_LINE(lineSensorValues[5])))
#define FOUND_FRONT() (ON_LINE(frontSensorValues[0]))
/*
Object Declarations
*/
// -- Motors --
motor motorLeft, motorRight;
// -- Encoders --
Encoder motorEncoder(11, 12);
// -- Line Sensor --
unsigned char lineSensorPins[] = { 33, 32, 31, 30, 29, 28, 27, 26 };
unsigned short lineSensorValues[LINE_NUMBER_OF_SENSORS];
QTRSensorsRC lineSensor(lineSensorPins, LINE_NUMBER_OF_SENSORS, TIMEOUT, LINE_EMITTER_PIN);
// -- Front Sensor --
unsigned char frontSensorPins[] = { 24 };
unsigned short frontSensorValues[FRONT_NUMBER_OF_SENSORS];
QTRSensorsRC frontSensor(frontSensorPins, FRONT_NUMBER_OF_SENSORS, TIMEOUT);
/*
Variables
*/
// -- PID --
float position = 0, proportional = 0, derivative = 0, integral = 0, lastProportional = 0;
float control = 0;
// -- Path Record --
char path[30], simplifiedPath[30];
unsigned int pathCounter = 0;
unsigned int stringCounter = 0;
unsigned char foundLeft = 0, foundStraight = 0, foundRight = 0;
// -- Robot Status --
char robotState = IDLE;
char restartExitMaze = 0;
// -- Encoder --
long entryTurnDistance[30], exitTurnDistance[30], nextTurnDistance = -999;
// decides which direction should the root turn
char selectTurn(unsigned char _foundLeft, unsigned char _foundRight, unsigned char _foundStraight)
{
if (_foundLeft)
return 'L';
else if (_foundRight)
return 'R';
else if (_foundStraight)
return 'S';
}
void turn(char _direction, unsigned int _speed, unsigned short _delayTime, unsigned char _automatic = 0)
{
if (_automatic)
{
}
else
{
switch (_direction)
{
case 'L':
GREEN_LED_LEFT_ON;
GREEN_LED_RIGHT_OFF;
motorLeft.write(-_speed);
motorRight.write(_speed);
delay(_delayTime);
GREEN_LED_LEFT_OFF;
GREEN_LED_RIGHT_OFF;
break;
case 'R':
GREEN_LED_RIGHT_ON;
GREEN_LED_LEFT_OFF;
motorLeft.write(_speed);
motorRight.write(-_speed);
delay(_delayTime);
GREEN_LED_LEFT_OFF;
GREEN_LED_RIGHT_OFF;
break;
case 'S':
motorLeft.write(_speed);
motorRight.write(_speed);
delay(_delayTime);
break;
}
}
}
unsigned int readLineSensor()
{
if (WHITE_ON_BLACK)
return lineSensor.readLine(lineSensorValues, QTR_EMITTERS_ON, true);
else
return lineSensor.readLine(lineSensorValues);
}
unsigned int readFrontSensor()
{
if (WHITE_ON_BLACK)
return frontSensor.readLine(frontSensorValues, QTR_EMITTERS_ON, true);
else
return frontSensor.readLine(frontSensorValues);
}
void setup()
{
analogWriteResolution(12);
if (DEBUG_CHANNEL == 1 || DEBUG_CHANNEL == 3)
Serial.begin(115200);
if (DEBUG_CHANNEL == 2 || DEBUG_CHANNEL == 3)
BLUETOOTH.begin(9600);
// pinMode Declarations
pinMode(MOTOR_STANDBY_PIN, OUTPUT);
pinMode(GREEN_LED_LEFT, OUTPUT);
pinMode(GREEN_LED_RIGHT, OUTPUT);
pinMode(RED_LED_LEFT, OUTPUT);
pinMode(RED_LED_RIGHT, OUTPUT);
pinMode(LEFT_BUTTON, INPUT);
pinMode(RIGHT_BUTTON, INPUT);
attachInterrupt(LEFT_BUTTON, leftButtonControl, RISING);
attachInterrupt(RIGHT_BUTTON, rightButtonControl, RISING);
// motor initialisation
motorLeft.setPins(LEFT_MOTOR_A, LEFT_MOTOR_B, LEFT_MOTOR_PWM);
motorRight.setPins(RIGHT_MOTOR_A, RIGHT_MOTOR_B, RIGHT_MOTOR_PWM);
motorLeft.setMaxSpeed(SPEED_MAX);
motorRight.setMaxSpeed(SPEED_MAX);
motorLeft.initialise();
motorRight.initialise();
delay(500);
ENABLE_STANDBY;
initialiseBot();
DISABLE_STANDBY;
robotState = IDLE;
}
void leftButtonControl()
{
unsigned int counter = 0;
while (LEFT_BUTTON_ON())
{
delay(10);
counter++;
}
if (counter > 5)
{
initialiseBot();
}
}
void rightButtonControl()
{
int counter = 0;
while (RIGHT_BUTTON_ON())
{
delay(10);
counter++;
}
if (counter > 5)
{
DISABLE_STANDBY;
RED_LED_LEFT_OFF;
RED_LED_RIGHT_OFF;
delay(500);
ENABLE_STANDBY;
RED_LED_RIGHT_ON;
RED_LED_LEFT_ON;
restartExitMaze = 1;
}
}
void initialiseBot()
{
RED_LED_LEFT_ON;
RED_LED_RIGHT_ON;
// Calibrate sensors
for (unsigned int i = 0; i < 67; i++)
{
if (i == 0 || i == 50)
turn('L', SPEED_CALIBRATE, 10);
if (i == 18)
turn('R', SPEED_CALIBRATE, 10);
frontSensor.calibrate();
lineSensor.calibrate();
}
RED_LED_LEFT_OFF;
RED_LED_RIGHT_OFF;
}
void showSensorValues()
{
if (DEBUG_CHANNEL == 1 || DEBUG_CHANNEL == 3)
{
Serial.print("Front : ");
Serial.print(frontSensorValues[0]);
Serial.print(" ");
for (unsigned char i = 0; i < LINE_NUMBER_OF_SENSORS; i++)
{
Serial.print(lineSensorValues[i]);
Serial.print(' ');
}
Serial.println();
}
if (DEBUG_CHANNEL == 2 || DEBUG_CHANNEL == 3)
{
BLUETOOTH.print("Front : ");
BLUETOOTH.print(frontSensorValues[0]);
for (unsigned char i = 0; i < LINE_NUMBER_OF_SENSORS; i++)
{
BLUETOOTH.print(lineSensorValues[i]);
BLUETOOTH.print(' ');
}
BLUETOOTH.println();
}
}
void runPID(int _maxSpeed, unsigned char _adaptiveSpeed = 0)
{
int _runSpeed = _maxSpeed;
while (true)
{
if (restartExitMaze)
{
exitMaze();
}
if (_adaptiveSpeed)
{
if (stringCounter == 0)
{
_runSpeed = TURN_APPROACH_SPEED;
}
else
{
if (exitTurnDistance[stringCounter - 1] - motorEncoder.read() < DISTANCE_TO_TURN)
{
_runSpeed = TURN_APPROACH_SPEED;
}
else
{
_runSpeed = _maxSpeed;
}
}
}
position = readLineSensor();
if (FOUND_LEFT() || FOUND_RIGHT())
break;
if (DEBUG_MODE == 1)
showSensorValues();
proportional = position - 3500;
derivative = proportional - lastProportional;
lastProportional = proportional;
integral += proportional;
if (_adaptiveSpeed )
control = KP_OUT * proportional + KI_OUT * integral + KD_OUT * derivative;
else
control = KP_IN * proportional + KI_IN * integral + KD_IN * derivative;
if (DEBUG_MODE == 2)
{
Serial.print("Control ");
Serial.print(control);
Serial.print(" Position ");
Serial.print(proportional);
Serial.println(" ");
}
if (control > _runSpeed)
control = _runSpeed;
if (control < -_runSpeed)
control = -_runSpeed;
if (control < 0)
{
motorLeft.write(_runSpeed + control);
motorRight.write(_runSpeed);
}
else
{
motorLeft.write(_runSpeed);
motorRight.write(_runSpeed - control);
}
}
}
void loop()
{
ENABLE_STANDBY;
enterMaze();
RED_LED_LEFT_ON;
RED_LED_RIGHT_ON;
reducePath();
exitMaze();
}
void enterMaze()
{
motorEncoder.write(0);
while (1)
{
char junctionFlag = 0;
foundLeft = 0, foundStraight = 0, foundRight = 0;
runPID(SPEED_MAX_ENTRY);
entryTurnDistance[pathCounter] = motorEncoder.read();
turn('S', 512, 0);
readFrontSensor();
readLineSensor();
do
{
if (!ON_LINE(lineSensorValues[0]) && !ON_LINE(lineSensorValues[7]))
{
junctionFlag = 1;
break;
}
readFrontSensor();
readLineSensor();
if (FOUND_LEFT())
foundLeft = 1;
if (FOUND_RIGHT())
foundRight = 1;
} while (FOUND_FRONT());
if (junctionFlag && foundLeft && foundRight)
{
turn('S', 1024, 100);
turn('S', 1024, 20);
path[pathCounter++] = 'J';
path[pathCounter] = '\0';
break;
}
char direction = selectTurn(foundLeft, foundRight, foundStraight);
turn(direction, SPEED_TURN, DELAY_TURN);
motorEncoder.write(0);
turn('S', 1024, 50);
path[pathCounter++] = direction;
}
}
void reducePath()
{
stringCounter = 0;
int i;
// Add the last square's turns as it is
for (i = 1; i <= 3; i++)
{
exitTurnDistance[stringCounter] = entryTurnDistance[pathCounter - 1 - i];
simplifiedPath[stringCounter++] = path[pathCounter - 1 - i];
}
for (i = pathCounter - 5; i >= 0; i--)
{
exitTurnDistance[stringCounter] = entryTurnDistance[i];
switch (path[i])
{
case 'L':
simplifiedPath[stringCounter++] = 'R';
break;
case 'R':
simplifiedPath[stringCounter++] = 'L';
break;
}
}
simplifiedPath[stringCounter++] = 'J';
simplifiedPath[stringCounter] = '\0';
for (i = 3; i < stringCounter; i++)
{
if (simplifiedPath[i] == 'L' && simplifiedPath[i + 1] == 'L' && simplifiedPath[i + 2] == 'L' && simplifiedPath[i + 3] == 'L' && simplifiedPath[i + 4] == 'R')
{
simplifiedPath[i] = 'R';
simplifiedPath[i + 1] = 'R';
simplifiedPath[i + 2] = 'L';
simplifiedPath[i + 3] = 'O';
simplifiedPath[i + 4] = 'O';
exitTurnDistance[i + 1] /= 2;
exitTurnDistance[i + 2] = exitTurnDistance[i + 4];
exitTurnDistance[i + 3] = 0;
exitTurnDistance[i + 4] = 0;
}
if (simplifiedPath[i] == 'R' && simplifiedPath[i + 1] == 'R' && simplifiedPath[i + 2] == 'R' && simplifiedPath[i + 3] == 'R' && simplifiedPath[i + 4] == 'L')
{
simplifiedPath[i] = 'L';
simplifiedPath[i + 1] = 'L';
simplifiedPath[i + 2] = 'R';
simplifiedPath[i + 3] = 'O';
simplifiedPath[i + 4] = 'O';
exitTurnDistance[i + 1] /= 2;
exitTurnDistance[i + 2] = exitTurnDistance[i + 4];
exitTurnDistance[i + 3] = 0;
exitTurnDistance[i + 4] = 0;
}
}
stringCounter = 0;
}
void exitMaze()
{
while (true)
{
if (restartExitMaze)
{
stringCounter = 0;
restartExitMaze = 0;
}
foundLeft = 0, foundStraight = 0, foundRight = 0;
motorEncoder.write(0);
runPID(SPEED_MAX_EXIT, 1);
for (unsigned int i = 0; i < 3; i++)
{
position = readLineSensor();
if (FOUND_LEFT())
foundLeft = 1;
if (FOUND_RIGHT())
foundRight = 1;
}
if (foundLeft || foundRight)
{
while (simplifiedPath[stringCounter] == 'O')
stringCounter++;
if (simplifiedPath[stringCounter] == 'J')
{
turn('S', 1024, 50);
DISABLE_STANDBY;
while (1)
{
if (restartExitMaze)
{
RED_LED_LEFT_ON;
RED_LED_RIGHT_ON;
exitMaze();
}
RED_LED_LEFT_OFF;
RED_LED_RIGHT_OFF;
GREEN_LED_LEFT_ON;
GREEN_LED_RIGHT_ON;
}
}
else
turn(simplifiedPath[stringCounter++], SPEED_TURN, DELAY_TURN);
turn('S', 1024, 50);
}
}
}