-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_draft2.ino
602 lines (485 loc) · 13.2 KB
/
program_draft2.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include <Adafruit_MotorShield.h>
#include "Servo.h"
#include "Arduino.h"
#include "Wire.h"
#include "DFRobot_VL53L0X.h"
//TODO LIST
/*1. 180 U-turn at the end
2. Test turning and path with the arm
3. Write and test pickup and dropping mechanisms with arm
4. Magnet sensor and LED lighting test and box detection
5. Drop box in recycling center
6. Button to start stop reset, switch modes
6. Off-path algorithm (in between 3rd and 4th and after all)
7. Integrating everything together (build cover for line sensor)
8. Robustify the code, so not prone to noise changes, random sensor detections
9. Final fine tuning
*/
//Declare motors
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *MotorL = AFMS.getMotor(1);
Adafruit_DCMotor *MotorR = AFMS.getMotor(2);
Servo myservo;
int servoPin = 10;
int open = 30; // variable to store the servo position
int closed = 90;
//Declare line sensors and set readings to default
int leftLineSensorPin = 2;
int centerleftLineSensorPin = 3;
int centerrightLineSensorPin = 4;
int rightLineSensorPin = 5;
int L = 0;
int R = 0;
int CL = 0;
int CR = 0;
String line = "";
//Declare magnetic sensors
int magnetPin = 11;
int magnetVal = 0;
//declare distance sensor
int threshold = ;
int distance = 0;
//Declare LEDs and buttons
int blueLED = 7;
int redLED = 8;
int greenLED = 6;
int buttonPin = 9;
//Declare driving constants
int MOTOR_SPEED = 255;
int SLOW_SPEED = 191;
//Declare state variables
bool get_ready_to_stop_turning = false;
bool dropoff_mode = false;
bool turn_detection = true;
bool override = false;
bool recycling = false;
bool backward_mode = false;
bool box_on = false;
int main_path_num = -1; //index for the nth direction of main path
int dropoff_path_num = -1; //index for nth direction of dropoff path
int dropoff_index = 0; //index of which the next index corresponds to backtracking
unsigned long startTime = 0;
unsigned long dropoffTimer = 0;
unsigned long ledTimer = 0;
int turn_delay = 200;
int dropoff_time = 800;
int dropoff_reversal_time = 250;
int dropoff_backup_time = 250;
int blue_led_period = 500;
int pickup_time = 250;
//Define possible modes
enum Mode {ADVANCE, TURN};
enum Turn_dir {TURNING_LEFT, TURNING_RIGHT, NOTURN};
enum Direction {STRAIGHT, LEFT, RIGHT, DROPOFFL, DROPOFFR, BACKTRACK, NOTHING};
//Declare path arrays
Direction main_path[] = {STRAIGHT, LEFT, RIGHT, STRAIGHT, RIGHT, DROPOFFL, RIGHT, RIGHT, RIGHT, DROPOFFL, LEFT, LEFT, LEFT, DROPOFFR, LEFT, RIGHT, RIGHT, STRAIGHT, RIGHT, STRAIGHT, RIGHT, DROPOFFL, LEFT, RIGHT, RIGHT, LEFT, STRAIGHT};
Direction dropoff_path[] = {RIGHT, RIGHT, BACKTRACK, LEFT, NOTHING, NOTHING}; //pad array with nothings as dropoff paths have variable lengths
int paths_with_box[] = {0, 5, 10, 16};
int dropoffpathlength = 0;
//Note: boxes are on paths where main_path_num = 0, 5, 10, 17
//once path_num=27 (the end) we stop the robot after a bit
//Set default modes
Mode currentMode = ADVANCE;
Turn_dir turn_dir = NOTURN;
void go_forward() {
MotorL->setSpeed(MOTOR_SPEED);
MotorR->setSpeed(MOTOR_SPEED);
MotorL->run(BACKWARD);
MotorR->run(BACKWARD);
}
void go_backward() {
MotorL->setSpeed(MOTOR_SPEED);
MotorR->setSpeed(MOTOR_SPEED);
MotorL->run(FORWARD);
MotorR->run(FORWARD);
}
void slight_forward_right() {
MotorL->setSpeed(MOTOR_SPEED);
MotorR->setSpeed(SLOW_SPEED);
MotorL->run(BACKWARD);
MotorR->run(BACKWARD);
}
void slight_forward_left() {
MotorL->setSpeed(SLOW_SPEED);
MotorR->setSpeed(MOTOR_SPEED);
MotorL->run(BACKWARD);
MotorR->run(BACKWARD);
}
void slight_backward_right() {
MotorL->setSpeed(MOTOR_SPEED);
MotorR->setSpeed(SLOW_SPEED);
MotorL->run(FORWARD);
MotorR->run(FORWARD);
}
void slight_backward_left() {
MotorL->setSpeed(SLOW_SPEED);
MotorR->setSpeed(MOTOR_SPEED);
MotorL->run(FORWARD);
MotorR->run(FORWARD);
}
void forward_right() {
MotorL->setSpeed(MOTOR_SPEED);
MotorL->run(BACKWARD);
MotorR->run(RELEASE);
}
void forward_left() {
MotorR->setSpeed(MOTOR_SPEED);
MotorL->run(RELEASE);
MotorR->run(BACKWARD);
}
void backward_left() {
MotorR->setSpeed(MOTOR_SPEED);
MotorL->run(RELEASE);
MotorR->run(FORWARD);
}
void backward_right() {
MotorL->setSpeed(MOTOR_SPEED);
MotorL->run(FORWARD);
MotorR->run(RELEASE);
}
void stop_motors() {
MotorL->run(RELEASE);
MotorR->run(RELEASE);
}
bool in_array(int value, int array[]) {
for (int i = 0; i <= sizeof(array) / sizeof(array[0]); i++) {
if (array[i] == value) {
return true;
}
}
return false;
}
// int measure_distance() {
// sensor.getDistance();
// }
void navigate_junction(Direction direction) {
Serial.println(direction);
dropoff_time = 800;
switch (direction) {
case STRAIGHT:
startTime = millis();
currentMode = ADVANCE;
break;
case LEFT:
currentMode = TURN;
turn_dir = TURNING_LEFT;
startTime = millis();
break;
case RIGHT:
currentMode = TURN;
turn_dir = TURNING_RIGHT;
startTime = millis();
break;
case BACKTRACK:
dropoff();
backward_mode = true;
dropoffTimer = millis();
dropoff_time = dropoff_reversal_time;
break;
case DROPOFFL:
dropoff_path_num = -1;
dropoff_mode = true;
if (recycling) {
digitalWrite(redLED, HIGH)
dropoff_index = 1;
dropoffpathlength = 3;
dropoff_path[0] = RIGHT;
dropoff_path[1] = RIGHT;
dropoff_path[2] = BACKTRACK;
dropoff_path[3] = RIGHT;
dropoff_path[4] = NOTHING;
dropoff_path[5] = NOTHING;
} else {
digitalWrite(greenLED, HIGH)
dropoff_index = 1;
dropoffpathlength = 5;
dropoff_path[0] = STRAIGHT;
dropoff_path[1] = RIGHT;
dropoff_path[2] = BACKTRACK;
dropoff_path[3] = LEFT;
dropoff_path[4] = LEFT;
dropoff_path[5] = STRAIGHT;
}
break;
case DROPOFFR:
switch_recycling();
dropoff_path_num = -1;
dropoff_mode = true;
if (recycling) {
digitalWRite(redLED, HIGH)
dropoff_index = 2;
dropoffpathlength = 4;
dropoff_path[0] = STRAIGHT;
dropoff_path[1] = LEFT;
dropoff_path[2] = RIGHT;
dropoff_path[3] = BACKTRACK;
dropoff_path[4] = RIGHT;
dropoff_path[5] = NOTHING;
} else {
digitalWritye(greenLED, HIGH)
dropoff_index = 0;
dropoffpathlength = 4;
dropoff_path[0] = LEFT;
dropoff_path[1] = BACKTRACK;
dropoff_path[2] = LEFT;
dropoff_path[3] = LEFT;
dropoff_path[4] = STRAIGHT;
dropoff_path[5] = NOTHING;
}
break;
}
}
void read_line_sensor() {
L = digitalRead(leftLineSensorPin);
R = digitalRead(rightLineSensorPin);
CL = digitalRead(centerleftLineSensorPin);
CR = digitalRead(centerrightLineSensorPin);
line = String(L) + String(CL) + String(CR) + String(R);
Serial.println(line);
}
void follow_line() {
if (line == "0000") {
if (backward_mode) {
//go backward like normal
go_backward();
} else {
//go forward like normal
go_forward();
}
} else if (line == "0010") {
//Perform a slight right correction depending on mode
if (backward_mode) {
// slight_backward_right();
go_backward();
} else {
slight_forward_right();
}
} else if (line == "0100") {
//Perfom a slight left correction
if (backward_mode) {
// slight_backward_left();
go_backward();
} else {
slight_forward_left();
}
}
}
void pickup() {
stop_motors();
box_on = true;
for (int pos = closed; pos >= open; pos--) {
myservo.write(pos);
delay(10);
}
go_forward();
delay(pickup_time);
stop_motors();
for (int pos = open; pos <= closed; pos++) {
myservo.write(pos);
delay(10);
}
delay(100);
}
void dropoff() {
stop_motors();
box_on = false;
for (int pos = closed; pos >= open; pos--) {
myservo.write(pos);
delay(10);
}
go_backward();
delay(dropoff_backup_time);
stop_motors();
for (int pos = open; pos <= closed; pos++) {
myservo.write(pos);
delay(10);
}
}
bool is_there_box() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
return true;
} else {
return false;
}
// distance = measure_distance();
// if (box_on == false) {
// if (distance <= threshold) {
// return true;
// } else {
// return false;
// }
// }
}
void detect_magnetic() {
if (magnetVal = 1) {
recycling = true
}
}
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
myservo.attach(servoPin);
if (!AFMS.begin()) {
Serial.println("Could not find Motor Shield. Check wiring.");
while (1);
}
Serial.println("Running this program");
Serial.println("Motor Shield found.");
delay(2000); //Wait 2 seconds at start
// Wire.begin();
// sensor.begin(0x50);
// sensor.setMode(sensor.eContinuous, sensor.eHigh);
// sensor.start();
ledTimer = millis();
}
void loop() {
read_line_sensor();
if (box_on) {
detect_magnetic()
}
if ((millis() - ledTimer) > blue_led_period) {
digitalWrite(blueLED, HIGH);
delay(10);
ledTimer = millis();
} else {
digitalWrite(blueLED, LOW);
}
if (in_array(main_path_num, paths_with_box)) {
digitalWrite(redLED, HIGH);
if (is_there_box()) {
pickup();
digitalWrite(greenLED, HIGH);
} else {
digitalWrite(greenLED, LOW);
}
} else {
digitalWrite(redLED, LOW);
}
if ((millis() - dropoffTimer) < dropoff_time) {
// Serial.println("HERE");
follow_line();
return;
}
if ((millis() - startTime) < turn_delay) {
follow_line();
return; //skip current iteration
}
switch (currentMode) {
case ADVANCE:
//Detect a junction/turn or override sensor information
if (R == 1 || L == 1 || override) {
Serial.println("JUNCTION DETECTED");
//if ready to drop box off
if (dropoff_mode) {
dropoff_path_num += 1;
navigate_junction(dropoff_path[dropoff_path_num]);
if (dropoff_path_num == dropoffpathlength) {
//reached end of dropping off
override = false;
dropoff_mode = false;
}
if (dropoff_path_num == dropoff_index + 2 ) {
override = false;
}
} else {
//Navigate normal path, not ready to dropoff yet
main_path_num += 1;
navigate_junction(main_path[main_path_num]);
}
} else {
follow_line();
}
break;
case TURN:
switch (turn_dir) {
case TURNING_LEFT:
if (backward_mode) {
backward_left();
if (R == 1) {
get_ready_to_stop_turning = true;
}
if (line == "0100" && get_ready_to_stop_turning) {
backward_mode = false;
currentMode = ADVANCE;
get_ready_to_stop_turning = false;
if (dropoff_path_num == dropoff_index) { //next mode is backtracking
override = true;
dropoffTimer = millis();
//INSERT CODE
}
if (backward_mode) {
go_backward();
} else {
go_forward();
}
}
} else {
forward_left();
if (L == 1) {
get_ready_to_stop_turning = true;
}
if (line == "0010" && get_ready_to_stop_turning) {
backward_mode = false;
currentMode = ADVANCE;
get_ready_to_stop_turning = false;
if (dropoff_path_num == dropoff_index) {
override = true;
dropoffTimer = millis();
//INSERT CODE
}
if (backward_mode) {
go_backward();
} else {
go_forward();
}
}
}
break;
case TURNING_RIGHT:
if (backward_mode) {
backward_right();
if (L == 1) {
get_ready_to_stop_turning = true;
}
if (line == "0010" && get_ready_to_stop_turning) {
backward_mode = false;
currentMode = ADVANCE;
get_ready_to_stop_turning = false;
if (dropoff_path_num == dropoff_index) {
override = true;
dropoffTimer = millis();
//INSERT CODE
}
if (backward_mode) {
go_backward();
} else {
go_forward();
}
}
} else {
forward_right();
if (R == 1) {
get_ready_to_stop_turning = true;
}
if (line == "0100" && get_ready_to_stop_turning) {
backward_mode = false;
currentMode = ADVANCE;
get_ready_to_stop_turning = false;
if (dropoff_path_num == dropoff_index) { //next mode is backtracking
override = true;
dropoffTimer = millis();
//INSERT CODE
}
if (backward_mode) {
go_backward();
} else {
go_forward();
}
}
}
break;
}
break;
}
}