Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (13.4 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

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

    
236
                                                ORB2_DBG_CLR(GREEN);
237

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

    
242
                                                ORB2_DBG_CLR(BLUE);
243

    
244
                                                break;
245
                                        case KRESOLVINGENTER:
246

    
247
                                                ORB2_DBG_CLR(ORANGE);
248

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

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

    
264

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

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

    
276
                                                ORB2_DBG_CLR(BLUE);
277

    
278
                                                break;
279
                                         case KRESOLVINGENTER:
280

    
281
                                                 ORB2_DBG_CLR(ORANGE);
282

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

    
289
                        ORB1_DBG_CLR(WHITE);
290

    
291

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

    
296
                        //Exits intersection
297
                        /*
298
                        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
                        }*/
310
                        state = SROAD;
311
                        break;
312
                case SHIGHWAY:/*On highway*/
313

    
314
                        ORB1_DBG_CLR(CYAN);
315

    
316
                        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
        if ((resolvSeed *= (rtc_get() + encoder_read(LEFT))%9) == 0){
504
                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

    
528
#endif