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