-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDP_program_draft1.ino
488 lines (390 loc) · 10.6 KB
/
IDP_program_draft1.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
#include <Adafruit_MotorShield.h>
#include <Servo.h>
//Declare motors
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *MotorL = AFMS.getMotor(1);
Adafruit_DCMotor *MotorR = AFMS.getMotor(2);
//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 LEDs
//Declare Servo
Servo myservo;
int open = 0; // variable to store the servo position
int closed = 90;
void setup() {
myservo.attach(10); // attaches the servo on pin 10
}
//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;
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;
unsigned long startTime = 0;
unsigned long dropoffTimer = 0;
int turn_delay = 300;
int dropoff_time = 1000;
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};
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);
}
void switch_recycling() {
if (recycling) {
recycling = false;
} else {
recycling = true;
}
}
void navigate_junction(Direction direction) {
Serial.println(direction);
dropoff_time = 1000;
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:
backward_mode = true;
dropoffTimer = millis();
dropoff_time = 650;
break;
case DROPOFFL:
dropoff_path_num = -1;
dropoff_mode = true;
switch_recycling();
if (recycling) {
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 {
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) {
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 {
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);
}
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();
;
} else {
slight_forward_right();
}
} else if (line == "0100") {
//Perfom a slight left correction
if (backward_mode) {
// slight_backward_left();
;
} else {
slight_forward_left();
}
}
}
void pickup() {
stop_motors();
myservo.write(open);
go_forwards();
delay(100);
stop_motors();
myservo.write(closed);
}
void dropoff() {
stop_motors();
myservo.write(open);
delay(200)
myservo.write(closed)
}
bool is_there_box() {
;
}
void setup() {
Serial.begin(9600);
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
}
void loop() {
read_line_sensor();
if (main_path_num == 0) {
//INSERT CODE
//SORT THIS OUT FURTHER, MAKE SURE ONCE IT PICKS, IT NO LOOK
if (is_there_box()) {
pickup();
}
}
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;
}
}