root / trunk / code / projects / traffic_navigation / main-new.c @ 1991
History | View | Annotate | Download (13.3 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 |
state = SINTERSECTION_DRIVE; |
253 |
break;
|
254 |
|
255 |
case SINTERSECTION_DRIVE:
|
256 |
DBG_USBS("STATE: SINTERSECTION_DRIVE\n");
|
257 |
|
258 |
|
259 |
ORB1_DBG_CLR(GREEN); |
260 |
ORB2_DBG_CLR(ORB_OFF); |
261 |
|
262 |
start(); |
263 |
turn(getIntersectType(sign), turnDir); |
264 |
while(doDrive(180) != FINISHED){ |
265 |
//while(!button2_click()){
|
266 |
int ret = wirelessPacketHandle(state);
|
267 |
switch (ret){
|
268 |
case KREPLIEDTOENTER:
|
269 |
|
270 |
ORB2_DBG_CLR(BLUE); |
271 |
|
272 |
break;
|
273 |
case KRESOLVINGENTER:
|
274 |
|
275 |
ORB2_DBG_CLR(ORANGE); |
276 |
|
277 |
break;
|
278 |
} |
279 |
} |
280 |
|
281 |
//Exits intersection
|
282 |
|
283 |
ORB1_DBG_CLR(WHITE); |
284 |
|
285 |
|
286 |
sendBuffer[0] = WINTERSECTIONEXIT;
|
287 |
sendBuffer[2] = intersectionNum;//Intersection # |
288 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
289 |
|
290 |
//Exits intersection
|
291 |
/*
|
292 |
while(1){
|
293 |
if(button1_click()){
|
294 |
start();
|
295 |
state = SHIGHWAY;
|
296 |
break;
|
297 |
}
|
298 |
if(button2_click()){
|
299 |
start();
|
300 |
state = SROAD;
|
301 |
break;
|
302 |
}
|
303 |
}*/
|
304 |
state = SROAD; |
305 |
break;
|
306 |
case SHIGHWAY:/*On highway*/ |
307 |
|
308 |
ORB1_DBG_CLR(CYAN); |
309 |
|
310 |
while(!button1_click()){
|
311 |
highwayFSM(); |
312 |
} |
313 |
state = SINTERSECTION_ENTER; |
314 |
break;
|
315 |
default:
|
316 |
usb_puts("I got stuck in an unknown state! My state is ");
|
317 |
usb_puti(state); |
318 |
} |
319 |
} |
320 |
|
321 |
} |
322 |
|
323 |
int wirelessPacketHandle(int state){ |
324 |
int dataLength = 0; |
325 |
unsigned char *packet = NULL; |
326 |
packet = wl_basic_do_default(&dataLength); |
327 |
|
328 |
/* sanity check */
|
329 |
if(dataLength == 0 || packet == NULL) //no packet |
330 |
return ENOPACKET;
|
331 |
|
332 |
if(dataLength != PACKET_LENGTH)
|
333 |
return EPACKETLEN;
|
334 |
|
335 |
usb_puts("Recieved Wireless Packet: ");
|
336 |
for(int i = 0; i < dataLength; i++){ |
337 |
usb_puti(packet[i]); |
338 |
usb_putc(' ');
|
339 |
} |
340 |
usb_putc('\n');
|
341 |
|
342 |
switch (packet[0]){ |
343 |
case WROADENTRY: //[type, bot, road] |
344 |
return ENOACTION;
|
345 |
break;
|
346 |
case WROADEXIT: //[type, bot, road] |
347 |
return ENOACTION;
|
348 |
break;
|
349 |
case WROADSTOP: //[type, bot, road] |
350 |
return ENOACTION;
|
351 |
break;
|
352 |
case WINTERSECTIONENTRY: //[type, bot, intersection, fromDir, toDir] |
353 |
if (packet[2] == intersectionNum){ |
354 |
switch (state){
|
355 |
case SINTERSECTION_ENTER:
|
356 |
sendResolv(false);
|
357 |
resolvPrevBotID = -2;
|
358 |
return KRESOLVINGENTER;
|
359 |
break;
|
360 |
case SINTERSECTION_ENTER_RESOLV:
|
361 |
return ENOACTION;
|
362 |
case SINTERSECTION_WAIT:
|
363 |
case SINTERSECTION_DRIVE:
|
364 |
if(queueNextBot == (char) -1){ |
365 |
sendBuffer[0] = WINTERSECTIONREPLY;
|
366 |
sendBuffer[2] = intersectionNum;
|
367 |
sendBuffer[3] = packet[1]; |
368 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
369 |
queueNextBot = packet[1];
|
370 |
return KREPLIEDTOENTER;
|
371 |
} |
372 |
break;
|
373 |
|
374 |
} |
375 |
} |
376 |
break;
|
377 |
case WINTERSECTIONREPLY: //[type, fromBot, intersection, toBot] |
378 |
if (packet[2] == intersectionNum){ |
379 |
switch (state){
|
380 |
case SINTERSECTION_ENTER:
|
381 |
case SINTERSECTION_ENTER_RESOLV:
|
382 |
if(packet[3] == id){ //Reply for me |
383 |
queuePrevBot = packet[1];
|
384 |
return KPLACEDINQUEUE;
|
385 |
} else {
|
386 |
if(packet[3] == resolvPrevBotID) |
387 |
resolvPrevBotID = -1;
|
388 |
return KFAILEDTOQUEUE;
|
389 |
} |
390 |
break;
|
391 |
default:
|
392 |
return ENOACTION;
|
393 |
} |
394 |
} |
395 |
break;
|
396 |
case WINTERSECTIONEXIT: //[type, bot, intersection] |
397 |
if (packet[2] == intersectionNum){ |
398 |
switch (state){
|
399 |
case SINTERSECTION_WAIT:
|
400 |
if(packet[1]==queuePrevBot){ |
401 |
queuePrevBot=-1;
|
402 |
return KFIRSTINQUEUE;
|
403 |
} |
404 |
} |
405 |
} |
406 |
break;
|
407 |
case WINTERSECTIONGO: //[type, bot, intersection] |
408 |
break;
|
409 |
case WINTERSECTIONPOLICEENTRY: //[?] |
410 |
return ENOACTION;
|
411 |
break;
|
412 |
case WINTERSECTIONRESOLVERACE: //[type, bot, intersection, num] |
413 |
//in the case robots draw the same number these will be
|
414 |
//arbitrated using the sending robot's id, prefering
|
415 |
//lower ids
|
416 |
usb_puts("Now in wireless WINTERSECTIONRESOLVERACE handler: resolvPrevBotID: ");
|
417 |
usb_puti((int) resolvPrevBotID);
|
418 |
usb_putc('\n');
|
419 |
if (packet[2] == intersectionNum){ |
420 |
switch (state){
|
421 |
case SINTERSECTION_ENTER:
|
422 |
case SINTERSECTION_ENTER_RESOLV:
|
423 |
if(resolvPrevBotID == (char) -3){ |
424 |
usb_puts("resolvPrevBotID == -3; sending a resolv packet and setting to -2\n");
|
425 |
sendResolv(false);
|
426 |
resolvPrevBotID = -2;
|
427 |
} |
428 |
if((unsigned char) packet[3] == resolvDraw && id > packet[1]){ |
429 |
//other bot drew same number as me,
|
430 |
//and i have a higher id, so it goes first.
|
431 |
usb_puts("bot ");
|
432 |
usb_puti(packet[1]);
|
433 |
usb_puts(" Drew the same number as me and I have a higher id, so it goes first\n");
|
434 |
resolvPrevBotID = packet[1];
|
435 |
} else if((unsigned char) packet[3] == resolvPrevBotDraw && resolvPrevBotID > packet[1]){ |
436 |
//other bot drew same number as the bot before me,
|
437 |
//so if it has a higher id than the bot before me,
|
438 |
//it is going to go in between that one and me.
|
439 |
usb_puts("bot ");
|
440 |
usb_puti(packet[1]);
|
441 |
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");
|
442 |
resolvPrevBotID = packet[1];
|
443 |
} else if((unsigned char)packet[3] < resolvDraw && (unsigned char)packet[3] > resolvPrevBotDraw){ |
444 |
//found a bot that goes in between the bot before me
|
445 |
//and me, so now it is before me.
|
446 |
usb_puts("bot ");
|
447 |
usb_puti(packet[1]);
|
448 |
usb_puts(" Drew a lower number bot before me, and the bot before me has have a higher id, so this goes first\n");
|
449 |
resolvPrevBotDraw = packet[3];
|
450 |
resolvPrevBotID = packet[1];
|
451 |
} |
452 |
return KRESOLVINGENTER;
|
453 |
break;
|
454 |
case SINTERSECTION_WAIT:
|
455 |
case SINTERSECTION_DRIVE:
|
456 |
usb_puts("Trying to resolv in non resolv case...queueNextbot = "); usb_puti(queueNextBot); usb_puts("\n"); |
457 |
if(queueNextBot == (char) -1){ |
458 |
sendResolv(true);
|
459 |
} |
460 |
return KRESOLVINGENTER;
|
461 |
break;
|
462 |
} |
463 |
|
464 |
} |
465 |
break;
|
466 |
case WHIGHWAYENTRY: //[type, bot, highway] |
467 |
return ENOACTION;
|
468 |
break;
|
469 |
case WHIGHWAYREPLY: //[type, fromBot, highway, toBot] |
470 |
return ENOACTION;
|
471 |
break;
|
472 |
case WHIGHWAYEXIT: //[type, bot, highway] |
473 |
return ENOACTION;
|
474 |
break;
|
475 |
case WPINGGLOBAL: //[type, bot] |
476 |
return ENOACTION;
|
477 |
break;
|
478 |
case WPINGBOT: //[type, fromBot, toBot] |
479 |
return ENOACTION;
|
480 |
break;
|
481 |
case WPINGQUEUE: //[type, fromBot, toBot] |
482 |
return ENOACTION;
|
483 |
break;
|
484 |
case WPINGREPLY: //[type, fromBot, toBot] |
485 |
return ENOACTION;
|
486 |
break;
|
487 |
case WCOLLISIONAVOID: //[type, bot, intersection, collision-int] //Note: collision is an int and thus takes two spaces |
488 |
return ENOACTION;
|
489 |
break;
|
490 |
default:
|
491 |
return ENOACTION;
|
492 |
break;
|
493 |
} |
494 |
} |
495 |
|
496 |
unsigned char resolvRandomNumberGen(){ |
497 |
if ((resolvSeed *= (rtc_get() + encoder_read(LEFT))%9) == 0){ |
498 |
return resolvSeed + 1; //0 is a reseved priority value for the last |
499 |
//bot that is already in the queue.
|
500 |
} |
501 |
return resolvSeed;
|
502 |
} |
503 |
void sendResolv(bool override){ |
504 |
if(!override)
|
505 |
resolvDraw = resolvRandomNumberGen(); |
506 |
else
|
507 |
resolvDraw = 0;
|
508 |
sendBuffer[0] = WINTERSECTIONRESOLVERACE;
|
509 |
sendBuffer[2] = intersectionNum;
|
510 |
sendBuffer[3] = resolvDraw;
|
511 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
512 |
} |
513 |
void enterIntersection(void){ |
514 |
//Sends packet announcing its entry to the intersection
|
515 |
sendBuffer[0] = WINTERSECTIONENTRY;
|
516 |
sendBuffer[2] = intersectionNum;//Intersection # |
517 |
sendBuffer[3] = 0; //getIntersectPos(sign); |
518 |
sendBuffer[4] = turnDir;
|
519 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
520 |
} |
521 |
|
522 |
#endif
|