-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_machine.cc
704 lines (638 loc) · 19.3 KB
/
state_machine.cc
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#include <iostream>
using namespace std;
#include <robot_instr.h>
#include <robot_link.h>
#include <stopwatch.h>
#include "line_following.h"
#include "pin.h"
robot_link rlink;
#define INITIAL_SPEED 60 //Some intial speed for the robot motors
#define TIME_FOR_PICKUP 1000 //time to pick up a cracker. NEEDS TESTING
#define TIME_FOR_DROP 3000 //time for dropping the cracker. NEEDS TESTING
#define TIME_FOR_ID 5000 //time to ID a cracker
#define TIME_FOR_RAMP 10000 //max time to climb black ramp
#define TIME_FOR_WHITE_RAMP 7200 //time to climb white ramp
#define TIME_TO_WHITE_RAMP 1500 //time to reach white ramp from D3
#define JUNCTION_DELAY 800 //delay before incrementing junction count
//Thresholds for the cracker id - TODO SET THESE
#define THRESHOLD_1 20
#define THRESHOLD_2 40
#define THRESHOLD_3 60
//LED indicator codes
#define LED_MASK 0xE9
#define CRACKER_1 0x06
#define CRACKER_2 0x04
#define CRACKER_3 0x02
#define CRACKER_4 0x00
#define REPLACE_CRACKER 0x08
// This file defines the main function and the state machine
// for the IDP robot.
typedef enum
{
STATE_START = 0, STATE_S_TO_P, STATE_COLLECT_CRACKER, STATE_ID_CRACKER,
STATE_P_TO_D1, STATE_P_TO_D2, STATE_P_TO_D3, STATE_P_TO_D4, STATE_DROP_CRACKER,
STATE_D1_TO_P, STATE_D2_TO_P, STATE_D3_TO_P, STATE_D4_TO_P,
STATE_RETURN_TO_START, NUM_STATES
} state_t;
static state_t current_state;
static int cracker_carried;
//light the right LEDs corresponding to each cracker.
//if the cracker can't be identified, flash LEDs on/off a couple of times
//to indicate detection failure
void cracker_led(int a)
{
int val = rlink.request(READ_ACTUATOR_AND_LED);
val = val & LED_MASK;
if (a != -1)
{
switch(a)
{
case(1):
val = val | CRACKER_1;
break;
case(2):
val = val | CRACKER_2;
break;
case(3):
val = val | CRACKER_3;
break;
case(4):
val = val | CRACKER_4;
break;
}
rlink.command(WRITE_ACTUATOR_AND_LED, val);
return;
}
//we couldn't find out what cracker we've got, so indicate error
val = val | CRACKER_1;
rlink.command(WRITE_ACTUATOR_AND_LED, val);
delay(1000);
val = val & LED_MASK;
val = val | CRACKER_4;
rlink.command(WRITE_ACTUATOR_AND_LED, val);
delay(1000);
val = val | CRACKER_1;
rlink.command(WRITE_ACTUATOR_AND_LED, val);
return;
}
//use ADC to find cracker type.
//Read ADC for 5 seconds or something similar, and take an average.
int id_cracker(void)
{
stopwatch s;
int adc_val = 0;
s.start();
while(s.read() < TIME_FOR_ID)
{
adc_val += rlink.request(ADC0);
}
//Take the average of the ADC readings over the time period.
adc_val /= TIME_FOR_ID;
//Now we need to do stuff with the value.
if (adc_val <= THRESHOLD_1)
{
cracker_led(1);
return 1;
}
else if (adc_val <= THRESHOLD_2 && adc_val > THRESHOLD_1)
{
cracker_led(2);
return 2;
}
else if (adc_val <= THRESHOLD_3 && adc_val > THRESHOLD_2)
{
cracker_led(3);
return 3;
}
else if (adc_val > THRESHOLD_3)
{
cracker_led(4);
return 4;
}
else
//test failed
{
cracker_led(-1);
return -1;
}
}
//typedef for all the state functions we need
typedef state_t state_func_t(void);
state_t run_state(state_t cur_state);
static state_t do_state_start(void);
static state_t do_state_s_to_p(void);
static state_t do_state_collect_cracker(void);
static state_t do_state_id_cracker(void);
static state_t do_state_p_to_d1(void);
static state_t do_state_p_to_d2(void);
static state_t do_state_p_to_d3(void);
static state_t do_state_p_to_d4(void);
static state_t do_state_drop_cracker(void);
static state_t do_state_d1_to_p(void);
static state_t do_state_d2_to_p(void);
static state_t do_state_d3_to_p(void);
static state_t do_state_d4_to_p(void);
static state_t do_state_return_to_start(void);
//Table of the state functions
state_func_t* const state_table[NUM_STATES] =
{
do_state_start, do_state_s_to_p, do_state_collect_cracker,
do_state_id_cracker, do_state_p_to_d1, do_state_p_to_d2,
do_state_p_to_d3, do_state_p_to_d4, do_state_drop_cracker,
do_state_d1_to_p, do_state_d2_to_p, do_state_d3_to_p,
do_state_d4_to_p, do_state_return_to_start
};
//function that runs the state machine.
//Calls the next state
state_t run_state(state_t cur_state)
{
return state_table[cur_state]();
}
static state_t do_state_start(void)
{
//TODO: When does the machine start?
//Is it on power on?
//if so, doesn't need this state at all.
//Can go straight to the 'move to P stage'
return STATE_S_TO_P;
}
//Operation: set motor speed at start, and follow the line out of S
//Use line following algorithm until we've reached P. Repeatedly call this!
//Set a stopwatch? Maybe to just ensure that we haven't messed up. ERROR DETECTION
//Wait for the front microswitch to make contact with the support at P.
//This happens when the value read from the specific I2C chip goes high
//When it does, reset the emergency stop mode (ie reenable).
//Then move to the next state
static state_t do_state_s_to_p(void)
{
rlink.command(MOTOR_1_GO, MOTOR1_OFFSET);
rlink.command(MOTOR_4_GO, MOTOR4_OFFSET);
int junct_count = 0;
while((rlink.request(READ_ARM_MICROSWITCH) & PIN_ARM_MICROSWITCH) != PIN_ARM_MICROSWITCH)
{
int val = 0;
if (junct_count == 3)
//reached the final junction before P. Probably best to go slowly to ensure alignment is ok
//for cracker pickup.
//call white_line_straight with argument 0 to ensure we go slow for correct alignment.
val = white_line_straight(0);
else
val = white_line_straight();
if(val == LINE_LOST)
find_a_line();
else if (val == LINE_JUNCTION)
{
junct_count++;
delay(JUNCTION_DELAY);
}
}
//now we've reached P, go to the next state
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_COLLECT_CRACKER;
}
//Start a stopwatch at the beginning because this will tell us whether
//we've collected the cracker or not.
//Start the motor/mechanism to close the claws
//wait until the timer has elapsed...
//then move to ID cracker.
//Need some error detection here to see if we've actually picked up the cracker or not?
static state_t do_state_collect_cracker(void)
{
stopwatch s = stopwatch();
s.start();
//Assume that the actuator is in right now (mechanism is open)
int val = rlink.request(READ_ACTUATOR_AND_LED);
val = val ^ ACTUATOR_TRIGGER;
rlink.command(WRITE_ACTUATOR_AND_LED, val);
while(s.read() < TIME_FOR_PICKUP)
{
;
}
//We've now successfully picked up the cracker. Can start to id it
return STATE_ID_CRACKER;
}
//complicated state. Call a id_cracker to find out what cracker we're dealing with
//Having found this, call the relevant function
static state_t do_state_id_cracker(void)
{
switch (id_cracker())
{
case 1:
cracker_carried = 1;
return STATE_P_TO_D1;
case 2:
cracker_carried = 2;
return STATE_P_TO_D2;
case 3:
cracker_carried = 3;
return STATE_P_TO_D3;
case 4:
cracker_carried = 4;
return STATE_P_TO_D4;
default:
cracker_carried = 1;
return STATE_P_TO_D1;
}
}
static state_t do_state_p_to_d1(void)
{
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
turn_180();
while(true)
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
turn_right();
while((rlink.request(READ_FRONT_MICROSWITCH) & PIN_FRONT_MICROSWITCH) != PIN_FRONT_MICROSWITCH)
{
if(white_line_straight() == LINE_LOST)
{
find_a_line();
}
}
//we've made contact with the box at D1. Move to next state.
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_DROP_CRACKER;
}
static state_t do_state_p_to_d2(void)
{
//Flash some LEDs here to indicate which cracker we're carrying.
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
watch.stop();
turn_180();
int junct_number = 0;
while (junct_number < 3)
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
{
++junct_number;
//delay the next call to white_line_straight to ensure we've crossed the junction fully.
delay(JUNCTION_DELAY);
}
}
turn_right();
watch.start();
while (watch.read() < TIME_FOR_RAMP)
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
turn_left();
while((rlink.request(READ_FRONT_MICROSWITCH) & PIN_FRONT_MICROSWITCH) != PIN_FRONT_MICROSWITCH)
{
if(white_line_straight() == LINE_LOST)
{
find_a_line();
}
}
//we've made contact with the box at D2. Move to next state.
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_DROP_CRACKER;
}
static state_t do_state_p_to_d3(void)
{
//Flash some LEDs here to indicate which cracker we're carrying.
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
watch.stop();
turn_left(); //configured by timing conditions
//error correction here- this may have gone wrong.
while (true) //TODO FIX THIS, add some timing!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_BLACK)
break;
}
//we've now reached the white ramp...
watch.start();
while(watch.read() < TIME_FOR_WHITE_RAMP)
{
line_type_t ret = black_line_straight();
if (ret == LINE_LOST)
find_a_black_line();
else if (ret == LINE_OK)
break;
}
rlink.command(MOTOR_1_GO,MOTOR1_OFFSET+50);
rlink.command(MOTOR_4_GO, MOTOR4_OFFSET+50);
delay(800);
//reached the top of the white ramp now, hopefully. Need ERROR CHECKING HERE!!!!
while((rlink.request(READ_FRONT_MICROSWITCH) & PIN_FRONT_MICROSWITCH) != PIN_FRONT_MICROSWITCH)
{
if(white_line_straight() == LINE_LOST)
{
find_a_line();
}
}
//we've made contact with the box at D3. Move to next state.
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_DROP_CRACKER;
}
static state_t do_state_p_to_d4(void)
{
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
turn_180();
while(true) //this will take longer. May need a better system here!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
turn_right();
while(true) //this will take longer. May need a better system here!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
turn_right();
while((rlink.request(READ_FRONT_MICROSWITCH) & PIN_FRONT_MICROSWITCH) != PIN_FRONT_MICROSWITCH)
{
if(white_line_straight() == LINE_LOST)
{
find_a_line();
}
}
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_DROP_CRACKER;
}
//activate actuator (release the cracker)
//activate replace cracker LED, wait for TIME_FOR_DROP, then shut it off.
static state_t do_state_drop_cracker(void)
{
int val = rlink.request(READ_ACTUATOR_AND_LED);
val = ((val ^ ACTUATOR_TRIGGER) ^ REPLACE_CRACKER);
rlink.command(WRITE_ACTUATOR_AND_LED, val);
delay(TIME_FOR_DROP);
rlink.command(WRITE_ACTUATOR_AND_LED, (val ^ REPLACE_CRACKER));
switch(cracker_carried)
{
case 1:
cracker_carried = -1; //to show that the cracker has been dropped.
return STATE_D1_TO_P;
case 2:
cracker_carried = -1; //to show that the cracker has been dropped.
return STATE_D2_TO_P;
case 3:
cracker_carried = -1; //to show that the cracker has been dropped.
return STATE_D3_TO_P;
case 4:
cracker_carried = -1; //to show that the cracker has been dropped.
return STATE_D4_TO_P;
}
cracker_carried = -1;
return STATE_D1_TO_P;
}
static state_t do_state_d1_to_p(void)
{
//going to have to be pseudocode for now, until line detection algorithms are done
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
turn_180();
while(true) //this will take longer. May need a better system here!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
turn_left();
while(true) //this will take longer. May need a better system here!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
while((rlink.request(READ_ARM_MICROSWITCH) & PIN_ARM_MICROSWITCH) != PIN_ARM_MICROSWITCH)
{
if(white_line_straight(0) == LINE_LOST)
{
find_a_line();
}
}
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_COLLECT_CRACKER;
}
static state_t do_state_d2_to_p(void)
{
//going to have to be pseudocode for now, until line detection algorithms are done
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
watch.stop();
turn_left(); //configured by timing conditions
//error correction here- this may have gone wrong.
int junct_number = 0;
while(junct_number < 2) //this will take longer. May need a better system here!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
{
++count;
watch.start();
while(watch.read() < JUNCTION_DELAY)
;
watch.stop();
}
}
turn_left();
while((rlink.request(READ_ARM_MICROSWITCH) & PIN_ARM_MICROSWITCH) != PIN_ARM_MICROSWITCH)
{
if(white_line_straight(0) == LINE_LOST)
{
find_a_line();
}
}
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_COLLECT_CRACKER;
}
static state_t do_state_d3_to_p(void)
{
//going to have to be pseudocode for now, until line detection algorithms are done
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
turn_180();
watch.stop();
//error correction here- this may have gone wrong.
watch.start();
while (watch.read() < TIME_TO_WHITE_RAMP) //TODO FIX THIS, add some timing!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_BLACK)
break;
}
rlink.command(MOTOR_1_GO,MOTOR1_OFFSET + 30);
rlink.command(MOTOR_4_GO,MOTOR4_OFFSET + 30);
//we've now reached the white ramp...
watch.stop();
watch.start();
while(watch.read() < TIME_FOR_WHITE_RAMP)
{
line_type_t ret = black_line_straight();
if (ret == LINE_LOST)
find_a_black_line();
else if (ret == LINE_OK)
break;
}
//reached the bottom of the white ramp now, hopefully. Need ERROR CHECKING HERE!!!!
turn_left();
while((rlink.request(READ_ARM_MICROSWITCH) & PIN_ARM_MICROSWITCH) != PIN_ARM_MICROSWITCH)
{
if(white_line_straight(0) == LINE_LOST)
{
find_a_line();
}
}
//we've made contact with P. Move to next state.
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_COLLECT_CRACKER;
}
static state_t do_state_d4_to_p(void)
{
//going to have to be pseudocode for now, until line detection algorithms are done
stopwatch watch;
watch.start();
while(watch.read() < 5000) //better to have a time bound here.
{
line_type_t ret = reverse_straight();
if (ret == LINE_JUNCTION)
break;
}
turn_left(); //configured by timing conditions
//error correction here- this may have gone wrong.
while(true) //this will take longer. May need a better system here!
{
line_type_t ret = white_line_straight();
if (ret == LINE_LOST)
find_a_line();
else if (ret == LINE_JUNCTION)
break;
}
turn_left();
while((rlink.request(READ_ARM_MICROSWITCH) & PIN_ARM_MICROSWITCH) != PIN_ARM_MICROSWITCH)
{
if(white_line_straight() == LINE_LOST)
{
find_a_line();
}
}
//we've made contact with P. Move to next state.
rlink.command(MOTOR_1_GO,0);
rlink.command(MOTOR_4_GO,0);
return STATE_COLLECT_CRACKER;
}
static state_t do_state_return_to_start(void)
{
//sort this one out later
return STATE_RETURN_TO_START;
}
//initialize the link/robot.
//set ramp speed? Not sure if necessary here. Ask others.
static int init(void)
{
if (!rlink.initialise (ROBOT_NUM))
{ // setup the link
cout << "Cannot initialise link" << endl;
rlink.print_errs(" ");
return -1;
}
else
{
return 1;
}
}
int main(int argc, char* argv[])
{
if (init() == -1)
{ // setup the link
cout << "Cannot initialise link" << endl;
rlink.print_errs(" ");
return -1;
}
current_state = STATE_START;
state_t new_state;
//rlink.command(RAMP_TIME, 50); //for now, a default ramp time.
while(true)
{
new_state = run_state(current_state);
if (new_state != current_state)
{
current_state = new_state;
}
}
}