root / trunk / code / projects / traffic_navigation / main.c @ 1844
History | View | Annotate | Download (5.61 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 <dragonfly_lib.h> |
9 |
#include <wl_basic.h> |
10 |
#include "../linefollowing/lineFollow.h" |
11 |
|
12 |
/*States*/
|
13 |
#define SROAD 0 |
14 |
#define SINTERSECTION 10 |
15 |
#define SHIGHWAY 20 |
16 |
|
17 |
/*Sign Codes
|
18 |
* bitwise OR labels to create a barcode or read one
|
19 |
* There should be macros to extract these probably
|
20 |
* The bits will be stored in some variable (char or short)
|
21 |
* Bits if road: ? ? ? NAME NAME NAME TYPE CROAD
|
22 |
* Bits if intersection: ? ? ? ? DIR DIR #WAYS CINTERSECTION
|
23 |
*/
|
24 |
#define CROAD 0x0 //0b |
25 |
#define CINTERSECTION 0x1 //1b |
26 |
#define CNORMALROAD 0x0 //00b |
27 |
#define CHIGHWAYROAD 0x2 //10b |
28 |
#define C4WAY 0x0 //00b |
29 |
#define C3WAY 0x2 //10b |
30 |
#define CNORTH 0x0 //0000b |
31 |
#define CEAST 0x4 //0100b |
32 |
#define CSOUTH 0x8 //1000b |
33 |
#define CWEST 0x12 //1100b |
34 |
|
35 |
/*Wireless Packet Types
|
36 |
* The first byte of any wireless packet should be one of these types.
|
37 |
* Each type will have its own structure
|
38 |
* The second byte should be the id of the bot sending the packet
|
39 |
* The third byte should be the number of the intersection or road that
|
40 |
* the packet pertains to
|
41 |
*/
|
42 |
#define PACKET_LENGTH 5 |
43 |
#define WROADENTRY 0 //[type, bot, road] |
44 |
#define WROADREPLY 1 //[type, fromBot, road, toBot] |
45 |
#define WROADEXIT 2 //[type, bot, road] |
46 |
#define WROADSTOP 3 //[type, bot, road] |
47 |
#define WINTERSECTIONENTRY 10 //[type, bot, intersection, fromDir, toDir] |
48 |
#define WINTERSECTIONREPLY 11 //[type, fromBot, intersection, toBot] |
49 |
#define WINTERSECTIONEXIT 12 //[type, bot, intersection] |
50 |
#define WINTERSECTIONGO 13 //[type, bot, intersection] |
51 |
#define WHIGHWAYENTRY 20 //[type, bot, highway] |
52 |
#define WHIGHWAYREPLY 21 //[type, fromBot, highway, toBot] |
53 |
#define WHIGHWAYEXIT 22 //[type, bot, highway] |
54 |
|
55 |
int main (void) { |
56 |
|
57 |
int state, sign, dataLength;
|
58 |
char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, *packet, nextDir, nextPath;
|
59 |
|
60 |
/* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
|
61 |
dragonfly_init(ALL_ON); |
62 |
xbee_init(); |
63 |
encoders_init(); |
64 |
lineFollow_init(); |
65 |
rtc_init(SIXTEENTH_SECOND, NULL);
|
66 |
|
67 |
id = get_robotid(); |
68 |
sign = 0;
|
69 |
|
70 |
sendBuffer[1] = id;
|
71 |
|
72 |
while (1) { |
73 |
/*DTM Finite State Machine*/
|
74 |
switch(state){
|
75 |
case SROAD:/*Following a normal road*/ |
76 |
sign = lineFollow(); |
77 |
//other road behaviors
|
78 |
//tailgating?
|
79 |
//read barcode
|
80 |
if(sign & CINTERSECTION){
|
81 |
state = SINTERSECTION; |
82 |
} |
83 |
break;
|
84 |
case SINTERSECTION:/*Entering, and in intersection*/ |
85 |
//Intersection behaviors
|
86 |
//queueing
|
87 |
//no-stop?
|
88 |
//check wireless
|
89 |
|
90 |
/*Intersection queue:
|
91 |
*Each robot when entering the intersection will check for other robots
|
92 |
*in the intersection, and insert itself in a queue to go through.
|
93 |
*/
|
94 |
prevBot = 0;
|
95 |
nextBot = 0;
|
96 |
//Sends packet announcing its entry to the intersection
|
97 |
sendBuffer[0] = WINTERSECTIONENTRY;
|
98 |
sendBuffer[2] = 0;//Intersection # |
99 |
sendBuffer[3] = 0;//From Direction |
100 |
sendBuffer[4] = 0;//To Direction |
101 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
102 |
rtc_reset(); |
103 |
while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue |
104 |
packet = wl_basic_do_default(&dataLength); |
105 |
if(dataLength==PACKET_LENGTH && packet[0]==WINTERSECTIONREPLY && packet[2]==0/*Intersection Num*/){ |
106 |
if(packet[3]==id){//Reply for me |
107 |
prevBot = packet[1];
|
108 |
break;
|
109 |
}else{//Someone else got here first, try again |
110 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
111 |
rtc_reset(); |
112 |
} |
113 |
} |
114 |
} |
115 |
//waits for its turn
|
116 |
while(prevBot != 0){ |
117 |
packet = wl_basic_do_default(&dataLength); |
118 |
if(dataLength==PACKET_LENGTH && packet[2]==0/*Intersection Num*/){ |
119 |
if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){ |
120 |
prevBot = 0;
|
121 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){ |
122 |
sendBuffer[0] = WINTERSECTIONREPLY;
|
123 |
sendBuffer[2] = 0;//Intersection # |
124 |
sendBuffer[3] = packet[1]; |
125 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
126 |
nextBot = packet[1];
|
127 |
nextDir = packet[3];
|
128 |
nextPath = packet[4];
|
129 |
} |
130 |
} |
131 |
} |
132 |
//Drives through intersection
|
133 |
//Code to choose path through intersection goes in this while loop
|
134 |
//But here's the code to handle wireless while in the intersection...
|
135 |
while(1/*replace with variable to keep track of if in intersection*/){ |
136 |
packet = wl_basic_do_default(&dataLength); |
137 |
if(dataLength==PACKET_LENGTH && packet[2]==0/*Intersection Num*/){ |
138 |
if(packet[0]==WINTERSECTIONEXIT){ |
139 |
prevBot = 0;
|
140 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){ |
141 |
sendBuffer[0] = WINTERSECTIONREPLY;
|
142 |
sendBuffer[2] = 0;//Intersection # |
143 |
sendBuffer[3] = packet[1]; |
144 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
145 |
nextBot = packet[1];
|
146 |
nextDir = packet[3];
|
147 |
nextPath = packet[4];
|
148 |
} |
149 |
} |
150 |
if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time |
151 |
/*if(bots paths won't collide){
|
152 |
sendBuffer[0] = WINTERSECTIONGO;
|
153 |
sendBuffer[2] = 0;//Intersection #
|
154 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
155 |
*/
|
156 |
} |
157 |
} |
158 |
//Exits intersection
|
159 |
sendBuffer[0] = WINTERSECTIONEXIT;
|
160 |
sendBuffer[2] = 0;//Intersection # |
161 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
162 |
|
163 |
if(!(sign & CINTERSECTION)){
|
164 |
if(sign & CHIGHWAYROAD){
|
165 |
state = SHIGHWAY; |
166 |
}else{
|
167 |
state = SROAD; |
168 |
} |
169 |
} |
170 |
break;
|
171 |
case SHIGHWAY:/*On highway*/ |
172 |
sign = lineFollow(); |
173 |
//highway behaviors
|
174 |
//merging
|
175 |
//passing
|
176 |
//read barcode
|
177 |
break;
|
178 |
default:
|
179 |
usb_puts("I got stuck in an unknown state! My state is ");
|
180 |
usb_puti(state); |
181 |
} |
182 |
} |
183 |
|
184 |
} |