Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (13.5 KB)

1 1999 azl
#define MAIN_NEW
2 1969 azl
/*
3
 * main.c for Traffic Navigation
4 1988 bwasserm
 * Runs the highest level behavior for the Dynamic Traffic Navigation (DTN) SURG
5 1969 azl
 *
6
 * Author: Colony Project, CMU Robotics Club
7
 */
8
9
#include "traffic_navigation.h"
10
#include "../linefollowing/lineDrive.h"
11 1991 azl
#define ORB_INTERSECTION
12
#define DEBUG_INTERSECTION
13 1988 bwasserm
#ifdef MAIN_NEW
14 1969 azl
15 1988 bwasserm
#ifdef ORB_INTERSECTION
16
  #define ORB1_DBG_CLR(color) orb1_set_color(color)
17
  #define ORB2_DBG_CLR(color) orb2_set_color(color)
18
#else
19
  #define ORB1_DBG_CLR(color)
20
  #define ORB2_DBG_CLR(color)
21
#endif
22
23
#ifdef DEBUG_INTERSECTION
24
  #define DBG_USBS(str) usb_puts(str)
25
#else
26
  #define DBG_USBS(str)
27
#endif
28
29 1969 azl
        static int state, sign, turnDir;
30
        static char sendBuffer[PACKET_LENGTH], queuePrevBot, queueNextBot, id, nextDir, nextPath, intersectionNum, resolvPrevBotID = -3;
31
        unsigned char resolvSeed = 0xC9, resolvDraw = 0, resolvPrevBotDraw = 0;
32 1972 azl
        bool done;
33 1969 azl
34
        int wirelessPacketHandle(int state);
35
        void enterIntersection(void);
36
        void sendResolv(bool override);
37
        unsigned char resolvRandomNumberGen();
38
39
int main (void) {
40
41
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
42
        dragonfly_init(ALL_ON);
43
        xbee_init();
44
        encoders_init();
45
        lineDrive_init();
46
        rtc_init(SIXTEENTH_SECOND, NULL);
47
        wl_basic_init_default();
48
        wl_set_channel(13);
49
        initializeData();
50
51
        id = get_robotid();
52
        sign = 0;
53 1988 bwasserm
        ORB1_DBG_CLR(GREEN);
54 1991 azl
        delay_ms(500);
55 1988 bwasserm
        ORB1_DBG_CLR(ORB_OFF);
56 1969 azl
57
        //Test code
58
        state = SROAD;
59
60
        sendBuffer[1] = id;
61
62
        /*
63 1988 bwasserm
        doDrive(180);
64 1969 azl
        turn(DOUBLE, ILEFT);
65
        */
66
        /*
67
        while(1)
68
                doDrive(255);
69
        */
70
71
        while (1) {
72 1988 bwasserm
                /*DTN Finite State Machine*/
73 1969 azl
                switch(state){
74
                case SROAD:/*Following a normal road*/
75
                        /* implement other road behaviors?
76
                         *    -tailgating
77
                         */
78 1988 bwasserm
      ORB1_DBG_CLR(WHITE);
79
      ORB2_DBG_CLR(ORB_OFF);
80 1977 azl
                        start();
81 1972 azl
                        done = false;
82
                        while(!done){
83 1988 bwasserm
                                sign = doDrive(180);
84 1972 azl
                                switch(sign){
85
                                        case NORMAL:
86
                                        case FINISHED:
87
                                        case LOST:
88
                                        case ERROR:
89
                                                break;
90
                                        default:
91
                                                //we have a barcode!
92
                                                state = SINTERSECTION_ENTER;
93 1991 azl
                                                usb_puts("Read Barcode #:");
94
                                                usb_puti(sign);
95
                                                usb_putc('\n');
96 1972 azl
                                                done = true;
97
                                                break;
98
99
                                }
100 1969 azl
                        }
101
                        break;
102
                case SINTERSECTION_ENTER:/*Entering, and in intersection*/
103
                        stop();
104
                        doDrive(0);
105 1988 bwasserm
      ORB1_DBG_CLR(RED);
106
      ORB2_DBG_CLR(ORB_OFF);
107 1969 azl
108 1988 bwasserm
                        DBG_USBS("STATE: SINTERSECTION_ENTER\n");
109 1969 azl
110
                        /*Intersection queue:
111
                         *Each robot when entering the intersection will check for other robots
112
                         *in the intersection, and insert itself in a queue to go through.
113
                         */
114
                        queuePrevBot = -1; //the bot that will drive before this bot
115
                        queueNextBot = -1; //the bot that will drive after this bot
116
                        resolvPrevBotID = -3; //in the case of a race, the bot that is
117
                                              //to enter the queue before this bot
118
                        resolvPrevBotDraw = 0; //the random priority number that bot has
119
                        resolvDraw = 0; //my random priority number
120
121 1972 azl
                        intersectionNum = getIntersectNum(sign);
122
                        if(intersectionNum == (char) -1){ //invalid
123
                                state = SROAD;
124 1991 azl
                                usb_puts("Barcode has invalid intersectionNum\n");
125 1972 azl
                                break;
126
                        }
127
                        turnDir = validateTurn(sign, getTurnType(sign));
128
                        if(turnDir == (char) -1){ //invalid
129
                                state = SROAD;
130 1991 azl
                                usb_puts("Barcode has invalid turn\n");
131 1972 azl
                                break;
132
                        }
133 1969 azl
134
                        enterIntersection(); //sends wireless packet for entry
135
                        state = SINTERSECTION_WAIT;
136
137
                        rtc_reset(); //reset rtc for timeout wait for reply
138 1972 azl
                        done = false;
139 1969 azl
                        char retried = 0;
140 1999 azl
                        while(rtc_get() < 8 && !done){//waits for a reply, otherwise assumes it is first in queue
141 1969 azl
                                int ret = wirelessPacketHandle(SINTERSECTION_ENTER);
142 1999 azl
                                if(rtc_get() > 6 && !retried){//by now all resolvs should be done from bots that arrived earlier...
143 1988 bwasserm
                                        ORB2_DBG_CLR(PURPLE);
144 1969 azl
                                        enterIntersection();
145
                                        retried = 1;
146
                                }
147
                                switch (ret) {
148
                                        case KPLACEDINQUEUE:
149 1988 bwasserm
                                                ORB2_DBG_CLR(GREEN);
150 1969 azl
                                                done = true;
151
                                                break;
152
                                        case KFAILEDTOQUEUE:
153 1988 bwasserm
                                                DBG_USBS("Failed to queue\n");
154
                                                ORB2_DBG_CLR(RED);
155 1969 azl
                                                enterIntersection();
156
                                                rtc_reset();
157
                                                break;
158
                                        case KRESOLVINGENTER:
159
160 1988 bwasserm
                                                ORB2_DBG_CLR(ORANGE);
161 1969 azl
                                                state = SINTERSECTION_ENTER_RESOLV;
162
                                                done = true;
163
                                                break;
164
                                }
165
                        }
166
                        break;
167
168
                case SINTERSECTION_ENTER_RESOLV:
169 1988 bwasserm
                        ORB1_DBG_CLR(PURPLE);
170
                        ORB2_DBG_CLR(ORB_OFF);
171
                        DBG_USBS("STATE: SINTERSECTION_ENTER_RESOLV\n");
172 1969 azl
173
                        rtc_reset();
174
                        done = false;
175
                        retried = 0;
176 1999 azl
                        while(rtc_get() < 4 && !done){
177 1969 azl
                                int ret = wirelessPacketHandle(SINTERSECTION_ENTER_RESOLV);
178
                                switch (ret) {
179
                                        case KRESOLVINGENTER:
180 1988 bwasserm
181
                                                ORB2_DBG_CLR(YELLOW);
182
183 1969 azl
                                                break;
184
                                        case KPLACEDINQUEUE:
185 1988 bwasserm
186
                                                ORB2_DBG_CLR(GREEN);
187
188 1969 azl
                                                done = true;
189
                                                break;
190
                                        case KFAILEDTOQUEUE:
191
                                                usb_puts("Failed to queue\n");
192
                                                orb2_set_color(RED);
193
                                                enterIntersection();
194
                                                rtc_reset();
195
                                                break;
196
                                }
197
                                //if resolvPrevBotID == -1, this indicates that
198
                                //there was a prevbot before, but it has entered
199
                                //the queue so it's our turn.
200
                                if(!done && resolvPrevBotID == (char) -1 && !retried){
201
                                        enterIntersection();
202
                                        rtc_reset();
203
                                        retried = 1;
204
                                //if resolvPrevBotID == -2, we have been
205
                                //resolving but never have seen a bot with lower
206
                                //priority than us. after the 6/16ths sec
207
                                //timeout, assume we are first.
208 1999 azl
                                } else if(!done && resolvPrevBotID == (char) -2 && rtc_get() > 2){
209 1969 azl
                                        //send a intersection reply to myself to
210
                                        //trigger other bots to enter queue.
211
                                        sendBuffer[0] = WINTERSECTIONREPLY;
212
                                        sendBuffer[2] = intersectionNum;
213
                                        sendBuffer[3] = id;
214
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
215
216
                                        done = true;
217
                                        break;
218
                                }
219
                        }
220
                        state = SINTERSECTION_WAIT;
221
                        break;
222
                case SINTERSECTION_WAIT:/*Waiting in intersection */
223
224 1988 bwasserm
                        ORB1_DBG_CLR(YELLOW);
225
                        ORB2_DBG_CLR(ORB_OFF);
226
227
                        DBG_USBS("STATE: SINTERSECTION_WAIT\n");
228
229 1998 azl
                        done = false;
230 1969 azl
                        while(queuePrevBot != (char) -1){
231 1998 azl
                                done = true;
232 1969 azl
                                int ret = wirelessPacketHandle(state);
233
                                switch (ret){
234
                                        case KFIRSTINQUEUE:
235 1988 bwasserm
236
                                                ORB2_DBG_CLR(GREEN);
237
238 1969 azl
                                                state = SINTERSECTION_DRIVE;
239
                                                break;
240
                                        case KREPLIEDTOENTER:
241 1988 bwasserm
242
                                                ORB2_DBG_CLR(BLUE);
243
244 1969 azl
                                                break;
245
                                        case KRESOLVINGENTER:
246 1988 bwasserm
247
                                                ORB2_DBG_CLR(ORANGE);
248
249 1969 azl
                                                break;
250
                                }
251
                        }
252 1992 azl
                        //hack to make sure bot that just left intersection is
253
                        //really out of the intersection.
254
                        rtc_reset();
255 1999 azl
                        while(rtc_get() < 2 && done){//wait one second
256 1992 azl
                                wirelessPacketHandle(state);
257
                        }
258
259 1969 azl
                        state = SINTERSECTION_DRIVE;
260
                        break;
261
                case SINTERSECTION_DRIVE:
262 1988 bwasserm
                        DBG_USBS("STATE: SINTERSECTION_DRIVE\n");
263
264
265
                        ORB1_DBG_CLR(GREEN);
266
                        ORB2_DBG_CLR(ORB_OFF);
267
268 1969 azl
                        start();
269 1972 azl
                        turn(getIntersectType(sign), turnDir);
270 1988 bwasserm
                        while(doDrive(180) != FINISHED){
271 1969 azl
                        //while(!button2_click()){
272
                                 int ret = wirelessPacketHandle(state);
273
                                 switch (ret){
274
                                         case KREPLIEDTOENTER:
275 1988 bwasserm
276
                                                ORB2_DBG_CLR(BLUE);
277
278 1969 azl
                                                break;
279
                                         case KRESOLVINGENTER:
280 1988 bwasserm
281
                                                 ORB2_DBG_CLR(ORANGE);
282
283 1969 azl
                                                break;
284
                                 }
285
                         }
286
287
                        //Exits intersection
288
289 1988 bwasserm
                        ORB1_DBG_CLR(WHITE);
290
291
292 1969 azl
                        sendBuffer[0] = WINTERSECTIONEXIT;
293
                        sendBuffer[2] = intersectionNum;//Intersection #
294
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
295
296
                        //Exits intersection
297 1972 azl
                        /*
298 1969 azl
                        while(1){
299
                                if(button1_click()){
300
                                        start();
301
                                        state = SHIGHWAY;
302
                                        break;
303
                                }
304
                                if(button2_click()){
305
                                        start();
306
                                        state = SROAD;
307
                                        break;
308
                                }
309 1972 azl
                        }*/
310
                        state = SROAD;
311 1969 azl
                        break;
312
                case SHIGHWAY:/*On highway*/
313 1988 bwasserm
314
                        ORB1_DBG_CLR(CYAN);
315
316 1969 azl
                        while(!button1_click()){
317
                                highwayFSM();
318
                        }
319
                        state = SINTERSECTION_ENTER;
320
                        break;
321
                default:
322
                        usb_puts("I got stuck in an unknown state! My state is ");
323
                        usb_puti(state);
324
                }
325
        }
326
327
}
328
329
int wirelessPacketHandle(int state){
330
        int dataLength = 0;
331
        unsigned char *packet = NULL;
332
        packet = wl_basic_do_default(&dataLength);
333
334
        /* sanity check */
335
        if(dataLength == 0 || packet == NULL) //no packet
336
                return ENOPACKET;
337
338
        if(dataLength != PACKET_LENGTH)
339
                return EPACKETLEN;
340
341
        usb_puts("Recieved Wireless Packet: ");
342
        for(int i = 0; i < dataLength; i++){
343
                usb_puti(packet[i]);
344
                usb_putc(' ');
345
        }
346
        usb_putc('\n');
347
348
        switch (packet[0]){
349
                case WROADENTRY: //[type, bot, road]
350
                        return ENOACTION;
351
                        break;
352
                case WROADEXIT: //[type, bot, road]
353
                        return ENOACTION;
354
                        break;
355
                case WROADSTOP: //[type, bot, road]
356
                        return ENOACTION;
357
                        break;
358
                case WINTERSECTIONENTRY: //[type, bot, intersection, fromDir, toDir]
359
                        if (packet[2] == intersectionNum){
360
                                switch (state){
361
                                        case SINTERSECTION_ENTER:
362
                                                sendResolv(false);
363
                                                resolvPrevBotID = -2;
364
                                                return KRESOLVINGENTER;
365
                                                break;
366
                                        case SINTERSECTION_ENTER_RESOLV:
367
                                                return ENOACTION;
368
                                        case SINTERSECTION_WAIT:
369
                                        case SINTERSECTION_DRIVE:
370
                                                if(queueNextBot == (char) -1){
371
                                                        sendBuffer[0] = WINTERSECTIONREPLY;
372
                                                        sendBuffer[2] = intersectionNum;
373
                                                        sendBuffer[3] = packet[1];
374
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
375
                                                        queueNextBot = packet[1];
376
                                                        return KREPLIEDTOENTER;
377
                                                }
378
                                                break;
379
380
                                }
381
                        }
382
                        break;
383
                case WINTERSECTIONREPLY: //[type, fromBot, intersection, toBot]
384
                        if (packet[2] == intersectionNum){
385
                                switch (state){
386
                                        case SINTERSECTION_ENTER:
387
                                        case SINTERSECTION_ENTER_RESOLV:
388
                                                if(packet[3] == id){ //Reply for me
389
                                                        queuePrevBot = packet[1];
390
                                                        return KPLACEDINQUEUE;
391
                                                } else {
392
                                                        if(packet[3] == resolvPrevBotID)
393
                                                                resolvPrevBotID = -1;
394
                                                        return KFAILEDTOQUEUE;
395
                                                }
396
                                                break;
397
                                        default:
398
                                                return ENOACTION;
399
                                }
400
                        }
401
                        break;
402
                case WINTERSECTIONEXIT: //[type, bot, intersection]
403
                        if (packet[2] == intersectionNum){
404
                                switch (state){
405
                                        case SINTERSECTION_WAIT:
406
                                                if(packet[1]==queuePrevBot){
407
                                                        queuePrevBot=-1;
408
                                                        return KFIRSTINQUEUE;
409
                                                }
410
                                }
411
                        }
412
                        break;
413
                case WINTERSECTIONGO: //[type, bot, intersection]
414
                        break;
415
                case WINTERSECTIONPOLICEENTRY: //[?]
416
                        return ENOACTION;
417
                        break;
418
                case WINTERSECTIONRESOLVERACE: //[type, bot, intersection, num]
419
                        //in the case robots draw the same number these will be
420
                        //arbitrated using the sending robot's id, prefering
421
                        //lower ids
422
                        usb_puts("Now in wireless WINTERSECTIONRESOLVERACE handler: resolvPrevBotID: ");
423
                        usb_puti((int) resolvPrevBotID);
424
                        usb_putc('\n');
425
                        if (packet[2] == intersectionNum){
426
                                switch (state){
427
                                        case SINTERSECTION_ENTER:
428
                                        case SINTERSECTION_ENTER_RESOLV:
429
                                                if(resolvPrevBotID == (char) -3){
430
                                                        usb_puts("resolvPrevBotID == -3; sending a resolv packet and setting to -2\n");
431
                                                        sendResolv(false);
432
                                                        resolvPrevBotID = -2;
433
                                                }
434
                                                if((unsigned char) packet[3] == resolvDraw && id > packet[1]){
435
                                                        //other bot drew same number as me,
436
                                                        //and i have a higher id, so it goes first.
437
                                                        usb_puts("bot ");
438
                                                        usb_puti(packet[1]);
439
                                                        usb_puts(" Drew the same number as me and I have a higher id, so it goes first\n");
440
                                                        resolvPrevBotID = packet[1];
441
                                                } else if((unsigned char) packet[3] == resolvPrevBotDraw && resolvPrevBotID > packet[1]){
442
                                                        //other bot drew same number as the bot before me,
443
                                                        //so if it has a higher id than the bot before me,
444
                                                        //it is going to go in between that one and me.
445
                                                        usb_puts("bot ");
446
                                                        usb_puti(packet[1]);
447
                                                        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");
448
                                                        resolvPrevBotID = packet[1];
449
                                                } else if((unsigned char)packet[3] < resolvDraw && (unsigned char)packet[3] > resolvPrevBotDraw){
450
                                                        //found a bot that goes in between the bot before me
451
                                                        //and me, so now it is before me.
452
                                                        usb_puts("bot ");
453
                                                        usb_puti(packet[1]);
454
                                                        usb_puts(" Drew a lower number bot before me, and the bot before me has have a higher id, so this goes first\n");
455
                                                        resolvPrevBotDraw = packet[3];
456
                                                        resolvPrevBotID = packet[1];
457
                                                }
458
                                                return KRESOLVINGENTER;
459
                                                break;
460
                                        case SINTERSECTION_WAIT:
461
                                        case SINTERSECTION_DRIVE:
462
                                                usb_puts("Trying to resolv in non resolv case...queueNextbot = "); usb_puti(queueNextBot); usb_puts("\n");
463
                                                if(queueNextBot == (char) -1){
464
                                                        sendResolv(true);
465
                                                }
466
                                                return KRESOLVINGENTER;
467
                                                break;
468
                                }
469
470
                        }
471
                        break;
472
                case WHIGHWAYENTRY: //[type, bot, highway]
473
                        return ENOACTION;
474
                        break;
475
                case WHIGHWAYREPLY: //[type, fromBot, highway, toBot]
476
                        return ENOACTION;
477
                        break;
478
                case WHIGHWAYEXIT: //[type, bot, highway]
479
                        return ENOACTION;
480
                        break;
481
                case WPINGGLOBAL: //[type, bot]
482
                        return ENOACTION;
483
                        break;
484
                case WPINGBOT: //[type, fromBot, toBot]
485
                        return ENOACTION;
486
                        break;
487
                case WPINGQUEUE: //[type, fromBot, toBot]
488
                        return ENOACTION;
489
                        break;
490
                case WPINGREPLY: //[type, fromBot, toBot]
491
                        return ENOACTION;
492
                        break;
493
                case WCOLLISIONAVOID: //[type, bot, intersection, collision-int] //Note: collision is an int and thus takes two spaces
494
                        return ENOACTION;
495
                        break;
496
                default:
497
                        return ENOACTION;
498
                        break;
499
        }
500
}
501
502
unsigned char resolvRandomNumberGen(){
503 1991 azl
        if ((resolvSeed *= (rtc_get() + encoder_read(LEFT))%9) == 0){
504 1969 azl
                return resolvSeed + 1; //0 is a reseved priority value for the last
505
                                 //bot that is already in the queue.
506
        }
507
        return resolvSeed;
508
}
509
void sendResolv(bool override){
510
        if(!override)
511
                resolvDraw = resolvRandomNumberGen();
512
        else
513
                resolvDraw = 0;
514
        sendBuffer[0] = WINTERSECTIONRESOLVERACE;
515
        sendBuffer[2] = intersectionNum;
516
        sendBuffer[3] = resolvDraw;
517
        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
518
}
519
void enterIntersection(void){
520
        //Sends packet announcing its entry to the intersection
521
        sendBuffer[0] = WINTERSECTIONENTRY;
522
        sendBuffer[2] = intersectionNum;//Intersection #
523
        sendBuffer[3] = 0; //getIntersectPos(sign);
524
        sendBuffer[4] = turnDir;
525
        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
526
}
527 1988 bwasserm
528 1969 azl
#endif