Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / traffic_navigation / main-new.c @ 1998

History | View | Annotate | Download (13.5 KB)

1
/*
2
 * main.c for Traffic Navigation
3
 * Runs the highest level behavior for the Dynamic Traffic Navigation (DTN) SURG
4
 *
5
 * Author: Colony Project, CMU Robotics Club
6
 */
7

    
8
#define MAIN_NEW
9

    
10
#include "traffic_navigation.h"
11
#include "../linefollowing/lineDrive.h"
12
#define ORB_INTERSECTION
13
#define DEBUG_INTERSECTION
14
#ifdef MAIN_NEW
15

    
16
#ifdef ORB_INTERSECTION
17
  #define ORB1_DBG_CLR(color) orb1_set_color(color)
18
  #define ORB2_DBG_CLR(color) orb2_set_color(color)
19
#else
20
  #define ORB1_DBG_CLR(color)
21
  #define ORB2_DBG_CLR(color)
22
#endif
23

    
24
#ifdef DEBUG_INTERSECTION
25
  #define DBG_USBS(str) usb_puts(str)
26
#else
27
  #define DBG_USBS(str)
28
#endif
29

    
30
        static int state, sign, turnDir;
31
        static char sendBuffer[PACKET_LENGTH], queuePrevBot, queueNextBot, id, nextDir, nextPath, intersectionNum, resolvPrevBotID = -3;
32
        unsigned char resolvSeed = 0xC9, resolvDraw = 0, resolvPrevBotDraw = 0;
33
        bool done;
34

    
35
        int wirelessPacketHandle(int state);
36
        void enterIntersection(void);
37
        void sendResolv(bool override);
38
        unsigned char resolvRandomNumberGen();
39

    
40
int main (void) {
41
        
42
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
43
        dragonfly_init(ALL_ON);
44
        xbee_init();
45
        encoders_init();
46
        lineDrive_init();
47
        rtc_init(SIXTEENTH_SECOND, NULL);        
48
        wl_basic_init_default();
49
        wl_set_channel(13);
50
        initializeData();
51
        
52
        id = get_robotid();
53
        sign = 0;
54
        ORB1_DBG_CLR(GREEN);
55
        delay_ms(500);
56
        ORB1_DBG_CLR(ORB_OFF);
57
        
58
        //Test code
59
        state = SROAD;
60

    
61
        sendBuffer[1] = id;
62

    
63
        /*
64
        doDrive(180);
65
        turn(DOUBLE, ILEFT);
66
        */
67
        /*
68
        while(1)
69
                doDrive(255);
70
        */
71
        
72
        while (1) {
73
                /*DTN Finite State Machine*/
74
                switch(state){
75
                case SROAD:/*Following a normal road*/
76
                        /* implement other road behaviors?
77
                         *    -tailgating
78
                         */
79
      ORB1_DBG_CLR(WHITE);
80
      ORB2_DBG_CLR(ORB_OFF);
81
                        start();
82
                        done = false;
83
                        while(!done){
84
                                sign = doDrive(180);
85
                                switch(sign){
86
                                        case NORMAL:
87
                                        case FINISHED:
88
                                        case LOST:
89
                                        case ERROR:
90
                                                break;
91
                                        default:
92
                                                //we have a barcode!
93
                                                state = SINTERSECTION_ENTER;
94
                                                usb_puts("Read Barcode #:");
95
                                                usb_puti(sign);
96
                                                usb_putc('\n');
97
                                                done = true;
98
                                                break;
99

    
100
                                }
101
                        }
102
                        break;
103
                case SINTERSECTION_ENTER:/*Entering, and in intersection*/
104
                        stop();
105
                        doDrive(0);
106
      ORB1_DBG_CLR(RED);
107
      ORB2_DBG_CLR(ORB_OFF);
108

    
109
                        DBG_USBS("STATE: SINTERSECTION_ENTER\n");
110

    
111
                        /*Intersection queue:
112
                         *Each robot when entering the intersection will check for other robots
113
                         *in the intersection, and insert itself in a queue to go through.
114
                         */
115
                        queuePrevBot = -1; //the bot that will drive before this bot
116
                        queueNextBot = -1; //the bot that will drive after this bot
117
                        resolvPrevBotID = -3; //in the case of a race, the bot that is
118
                                              //to enter the queue before this bot
119
                        resolvPrevBotDraw = 0; //the random priority number that bot has
120
                        resolvDraw = 0; //my random priority number
121
                        
122
                        intersectionNum = getIntersectNum(sign);
123
                        if(intersectionNum == (char) -1){ //invalid
124
                                state = SROAD;
125
                                usb_puts("Barcode has invalid intersectionNum\n");
126
                                break;
127
                        }
128
                        turnDir = validateTurn(sign, getTurnType(sign));
129
                        if(turnDir == (char) -1){ //invalid
130
                                state = SROAD;
131
                                usb_puts("Barcode has invalid turn\n");
132
                                break;
133
                        }
134

    
135
                        enterIntersection(); //sends wireless packet for entry
136
                        state = SINTERSECTION_WAIT;
137

    
138
                        rtc_reset(); //reset rtc for timeout wait for reply
139
                        done = false;
140
                        char retried = 0;
141
                        while(rtc_get() < 16 && !done){//waits for a reply, otherwise assumes it is first in queue
142
                                int ret = wirelessPacketHandle(SINTERSECTION_ENTER);
143
                                if(rtc_get() > 12 && !retried){//by now all resolvs should be done from bots that arrived earlier...
144
                                        ORB2_DBG_CLR(PURPLE);
145
                                        enterIntersection();
146
                                        retried = 1;
147
                                }
148
                                switch (ret) {
149
                                        case KPLACEDINQUEUE:
150
                                                ORB2_DBG_CLR(GREEN);
151
                                                done = true;
152
                                                break;
153
                                        case KFAILEDTOQUEUE:
154
                                                DBG_USBS("Failed to queue\n");
155
                                                ORB2_DBG_CLR(RED);
156
                                                enterIntersection();
157
                                                rtc_reset();
158
                                                break;
159
                                        case KRESOLVINGENTER:
160

    
161
                                                ORB2_DBG_CLR(ORANGE);
162
                                                state = SINTERSECTION_ENTER_RESOLV;
163
                                                done = true;
164
                                                break;
165
                                }
166
                        }
167
                        break;
168

    
169
                case SINTERSECTION_ENTER_RESOLV:
170
                        ORB1_DBG_CLR(PURPLE);
171
                        ORB2_DBG_CLR(ORB_OFF);
172
                        DBG_USBS("STATE: SINTERSECTION_ENTER_RESOLV\n");
173

    
174
                        rtc_reset();
175
                        done = false;
176
                        retried = 0;
177
                        while(rtc_get() < 9 && !done){
178
                                int ret = wirelessPacketHandle(SINTERSECTION_ENTER_RESOLV);
179
                                switch (ret) {
180
                                        case KRESOLVINGENTER:
181

    
182
                                                ORB2_DBG_CLR(YELLOW);
183

    
184
                                                break;
185
                                        case KPLACEDINQUEUE:
186

    
187
                                                ORB2_DBG_CLR(GREEN);
188

    
189
                                                done = true;
190
                                                break;
191
                                        case KFAILEDTOQUEUE:
192
                                                usb_puts("Failed to queue\n");
193
                                                orb2_set_color(RED);
194
                                                enterIntersection();
195
                                                rtc_reset();
196
                                                break;
197
                                }
198
                                //if resolvPrevBotID == -1, this indicates that
199
                                //there was a prevbot before, but it has entered
200
                                //the queue so it's our turn.
201
                                if(!done && resolvPrevBotID == (char) -1 && !retried){
202
                                        enterIntersection();
203
                                        rtc_reset();
204
                                        retried = 1;
205
                                //if resolvPrevBotID == -2, we have been
206
                                //resolving but never have seen a bot with lower
207
                                //priority than us. after the 6/16ths sec
208
                                //timeout, assume we are first.
209
                                } else if(!done && resolvPrevBotID == (char) -2 && rtc_get() > 6){
210
                                        //send a intersection reply to myself to
211
                                        //trigger other bots to enter queue.
212
                                        sendBuffer[0] = WINTERSECTIONREPLY;
213
                                        sendBuffer[2] = intersectionNum;
214
                                        sendBuffer[3] = id;
215
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
216

    
217
                                        done = true;
218
                                        break;
219
                                }
220
                        }
221
                        state = SINTERSECTION_WAIT;
222
                        break;
223
                case SINTERSECTION_WAIT:/*Waiting in intersection */
224

    
225
                        ORB1_DBG_CLR(YELLOW);
226
                        ORB2_DBG_CLR(ORB_OFF);
227

    
228
                        DBG_USBS("STATE: SINTERSECTION_WAIT\n");
229

    
230
                        done = false;
231
                        while(queuePrevBot != (char) -1){
232
                                done = true;
233
                                int ret = wirelessPacketHandle(state);
234
                                switch (ret){
235
                                        case KFIRSTINQUEUE:
236

    
237
                                                ORB2_DBG_CLR(GREEN);
238

    
239
                                                state = SINTERSECTION_DRIVE;
240
                                                break;
241
                                        case KREPLIEDTOENTER:
242

    
243
                                                ORB2_DBG_CLR(BLUE);
244

    
245
                                                break;
246
                                        case KRESOLVINGENTER:
247

    
248
                                                ORB2_DBG_CLR(ORANGE);
249

    
250
                                                break;
251
                                }
252
                        }
253
                        //hack to make sure bot that just left intersection is
254
                        //really out of the intersection.
255
                        rtc_reset();
256
                        while(rtc_get() < 4 && done){//wait one second
257
                                wirelessPacketHandle(state);
258
                        }
259

    
260
                        state = SINTERSECTION_DRIVE;
261
                        break;
262
                case SINTERSECTION_DRIVE:
263
                        DBG_USBS("STATE: SINTERSECTION_DRIVE\n");
264

    
265

    
266
                        ORB1_DBG_CLR(GREEN);
267
                        ORB2_DBG_CLR(ORB_OFF);
268

    
269
                        start();
270
                        turn(getIntersectType(sign), turnDir);
271
                        while(doDrive(180) != FINISHED){
272
                        //while(!button2_click()){
273
                                 int ret = wirelessPacketHandle(state);
274
                                 switch (ret){
275
                                         case KREPLIEDTOENTER:
276

    
277
                                                ORB2_DBG_CLR(BLUE);
278

    
279
                                                break;
280
                                         case KRESOLVINGENTER:
281

    
282
                                                 ORB2_DBG_CLR(ORANGE);
283

    
284
                                                break;
285
                                 }
286
                         }
287
                        
288
                        //Exits intersection
289

    
290
                        ORB1_DBG_CLR(WHITE);
291

    
292

    
293
                        sendBuffer[0] = WINTERSECTIONEXIT;
294
                        sendBuffer[2] = intersectionNum;//Intersection #
295
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
296

    
297
                        //Exits intersection
298
                        /*
299
                        while(1){
300
                                if(button1_click()){
301
                                        start();
302
                                        state = SHIGHWAY;
303
                                        break;
304
                                }
305
                                if(button2_click()){
306
                                        start();
307
                                        state = SROAD;
308
                                        break;
309
                                }
310
                        }*/
311
                        state = SROAD;
312
                        break;
313
                case SHIGHWAY:/*On highway*/
314

    
315
                        ORB1_DBG_CLR(CYAN);
316

    
317
                        while(!button1_click()){
318
                                highwayFSM();
319
                        }
320
                        state = SINTERSECTION_ENTER;
321
                        break;
322
                default:
323
                        usb_puts("I got stuck in an unknown state! My state is ");
324
                        usb_puti(state);
325
                }
326
        }
327

    
328
}
329

    
330
int wirelessPacketHandle(int state){
331
        int dataLength = 0;
332
        unsigned char *packet = NULL;
333
        packet = wl_basic_do_default(&dataLength);
334
        
335
        /* sanity check */
336
        if(dataLength == 0 || packet == NULL) //no packet
337
                return ENOPACKET;
338

    
339
        if(dataLength != PACKET_LENGTH)
340
                return EPACKETLEN;
341

    
342
        usb_puts("Recieved Wireless Packet: ");
343
        for(int i = 0; i < dataLength; i++){
344
                usb_puti(packet[i]);
345
                usb_putc(' ');
346
        }
347
        usb_putc('\n');
348

    
349
        switch (packet[0]){
350
                case WROADENTRY: //[type, bot, road]
351
                        return ENOACTION;
352
                        break;
353
                case WROADEXIT: //[type, bot, road]
354
                        return ENOACTION;
355
                        break;
356
                case WROADSTOP: //[type, bot, road]
357
                        return ENOACTION;
358
                        break;
359
                case WINTERSECTIONENTRY: //[type, bot, intersection, fromDir, toDir]
360
                        if (packet[2] == intersectionNum){
361
                                switch (state){
362
                                        case SINTERSECTION_ENTER:
363
                                                sendResolv(false);
364
                                                resolvPrevBotID = -2;
365
                                                return KRESOLVINGENTER;
366
                                                break;
367
                                        case SINTERSECTION_ENTER_RESOLV:
368
                                                return ENOACTION;
369
                                        case SINTERSECTION_WAIT:
370
                                        case SINTERSECTION_DRIVE:
371
                                                if(queueNextBot == (char) -1){
372
                                                        sendBuffer[0] = WINTERSECTIONREPLY;
373
                                                        sendBuffer[2] = intersectionNum;
374
                                                        sendBuffer[3] = packet[1];
375
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
376
                                                        queueNextBot = packet[1];
377
                                                        return KREPLIEDTOENTER;
378
                                                }
379
                                                break;
380

    
381
                                }
382
                        }
383
                        break;
384
                case WINTERSECTIONREPLY: //[type, fromBot, intersection, toBot]
385
                        if (packet[2] == intersectionNum){
386
                                switch (state){
387
                                        case SINTERSECTION_ENTER:
388
                                        case SINTERSECTION_ENTER_RESOLV:
389
                                                if(packet[3] == id){ //Reply for me
390
                                                        queuePrevBot = packet[1];
391
                                                        return KPLACEDINQUEUE;
392
                                                } else {
393
                                                        if(packet[3] == resolvPrevBotID)
394
                                                                resolvPrevBotID = -1;
395
                                                        return KFAILEDTOQUEUE;
396
                                                }
397
                                                break;
398
                                        default:
399
                                                return ENOACTION;
400
                                }
401
                        }
402
                        break;
403
                case WINTERSECTIONEXIT: //[type, bot, intersection]
404
                        if (packet[2] == intersectionNum){
405
                                switch (state){
406
                                        case SINTERSECTION_WAIT:
407
                                                if(packet[1]==queuePrevBot){
408
                                                        queuePrevBot=-1;
409
                                                        return KFIRSTINQUEUE;
410
                                                }
411
                                }
412
                        }
413
                        break;
414
                case WINTERSECTIONGO: //[type, bot, intersection]
415
                        break;
416
                case WINTERSECTIONPOLICEENTRY: //[?]
417
                        return ENOACTION;
418
                        break;
419
                case WINTERSECTIONRESOLVERACE: //[type, bot, intersection, num]
420
                        //in the case robots draw the same number these will be
421
                        //arbitrated using the sending robot's id, prefering
422
                        //lower ids
423
                        usb_puts("Now in wireless WINTERSECTIONRESOLVERACE handler: resolvPrevBotID: ");
424
                        usb_puti((int) resolvPrevBotID);
425
                        usb_putc('\n');
426
                        if (packet[2] == intersectionNum){
427
                                switch (state){
428
                                        case SINTERSECTION_ENTER:
429
                                        case SINTERSECTION_ENTER_RESOLV:
430
                                                if(resolvPrevBotID == (char) -3){
431
                                                        usb_puts("resolvPrevBotID == -3; sending a resolv packet and setting to -2\n");
432
                                                        sendResolv(false);
433
                                                        resolvPrevBotID = -2;
434
                                                }
435
                                                if((unsigned char) packet[3] == resolvDraw && id > packet[1]){
436
                                                        //other bot drew same number as me,
437
                                                        //and i have a higher id, so it goes first.
438
                                                        usb_puts("bot ");
439
                                                        usb_puti(packet[1]);
440
                                                        usb_puts(" Drew the same number as me and I have a higher id, so it goes first\n");
441
                                                        resolvPrevBotID = packet[1];
442
                                                } else if((unsigned char) packet[3] == resolvPrevBotDraw && resolvPrevBotID > packet[1]){
443
                                                        //other bot drew same number as the bot before me,
444
                                                        //so if it has a higher id than the bot before me,
445
                                                        //it is going to go in between that one and me.
446
                                                        usb_puts("bot ");
447
                                                        usb_puti(packet[1]);
448
                                                        usb_puts(" Drew the same number as the bot before me, and the bot before me has have a higher id, so this goes first\n");
449
                                                        resolvPrevBotID = packet[1];
450
                                                } else if((unsigned char)packet[3] < resolvDraw && (unsigned char)packet[3] > resolvPrevBotDraw){
451
                                                        //found a bot that goes in between the bot before me
452
                                                        //and me, so now it is before me.
453
                                                        usb_puts("bot ");
454
                                                        usb_puti(packet[1]);
455
                                                        usb_puts(" Drew a lower number bot before me, and the bot before me has have a higher id, so this goes first\n");
456
                                                        resolvPrevBotDraw = packet[3];
457
                                                        resolvPrevBotID = packet[1];
458
                                                }
459
                                                return KRESOLVINGENTER;
460
                                                break;
461
                                        case SINTERSECTION_WAIT:
462
                                        case SINTERSECTION_DRIVE:
463
                                                usb_puts("Trying to resolv in non resolv case...queueNextbot = "); usb_puti(queueNextBot); usb_puts("\n");
464
                                                if(queueNextBot == (char) -1){
465
                                                        sendResolv(true);
466
                                                }
467
                                                return KRESOLVINGENTER;
468
                                                break;
469
                                }
470
                                
471
                        }
472
                        break;
473
                case WHIGHWAYENTRY: //[type, bot, highway]
474
                        return ENOACTION;
475
                        break;
476
                case WHIGHWAYREPLY: //[type, fromBot, highway, toBot]
477
                        return ENOACTION;
478
                        break;
479
                case WHIGHWAYEXIT: //[type, bot, highway]
480
                        return ENOACTION;
481
                        break;
482
                case WPINGGLOBAL: //[type, bot]
483
                        return ENOACTION;
484
                        break;
485
                case WPINGBOT: //[type, fromBot, toBot]
486
                        return ENOACTION;
487
                        break;
488
                case WPINGQUEUE: //[type, fromBot, toBot]
489
                        return ENOACTION;
490
                        break;
491
                case WPINGREPLY: //[type, fromBot, toBot]
492
                        return ENOACTION;
493
                        break;
494
                case WCOLLISIONAVOID: //[type, bot, intersection, collision-int] //Note: collision is an int and thus takes two spaces
495
                        return ENOACTION;
496
                        break;
497
                default:
498
                        return ENOACTION;
499
                        break;
500
        }
501
}
502

    
503
unsigned char resolvRandomNumberGen(){
504
        if ((resolvSeed *= (rtc_get() + encoder_read(LEFT))%9) == 0){
505
                return resolvSeed + 1; //0 is a reseved priority value for the last
506
                                 //bot that is already in the queue.
507
        }
508
        return resolvSeed;
509
}
510
void sendResolv(bool override){
511
        if(!override)
512
                resolvDraw = resolvRandomNumberGen();
513
        else
514
                resolvDraw = 0;
515
        sendBuffer[0] = WINTERSECTIONRESOLVERACE;
516
        sendBuffer[2] = intersectionNum;
517
        sendBuffer[3] = resolvDraw;
518
        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
519
}
520
void enterIntersection(void){
521
        //Sends packet announcing its entry to the intersection
522
        sendBuffer[0] = WINTERSECTIONENTRY;
523
        sendBuffer[2] = intersectionNum;//Intersection #
524
        sendBuffer[3] = 0; //getIntersectPos(sign);
525
        sendBuffer[4] = turnDir;
526
        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
527
}
528

    
529
#endif