root / trunk / code / projects / traffic_navigation / main.c @ 1966
History | View | Annotate | Download (6.94 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 |
|
11 |
static int state, sign, dataLength, pingWaitTime, turnDir; |
12 |
static char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, nextDir, nextPath, intersectionNum; |
13 |
static unsigned char *packet; |
14 |
|
15 |
void enterIntersectionQueue(int sign); |
16 |
void waitInIntersectionQueue(void); |
17 |
void driveThroughIntersection(int sign); |
18 |
|
19 |
int main (void) { |
20 |
|
21 |
/* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
|
22 |
dragonfly_init(ALL_ON); |
23 |
xbee_init(); |
24 |
encoders_init(); |
25 |
lineDrive_init(); |
26 |
rtc_init(SIXTEENTH_SECOND, NULL);
|
27 |
wl_basic_init_default(); |
28 |
wl_set_channel(13);
|
29 |
initializeData(); |
30 |
|
31 |
id = get_robotid(); |
32 |
sign = 0;
|
33 |
|
34 |
//Test code
|
35 |
state = SROAD; |
36 |
|
37 |
sendBuffer[1] = id;
|
38 |
|
39 |
/*
|
40 |
doDrive(200);
|
41 |
turn(DOUBLE, ILEFT);
|
42 |
*/
|
43 |
/* while(1)
|
44 |
doDrive(255);
|
45 |
*/
|
46 |
|
47 |
while (1) { |
48 |
/*DTM Finite State Machine*/
|
49 |
switch(state){
|
50 |
case SROAD:/*Following a normal road*/ |
51 |
// sign = lineFollow();
|
52 |
//other road behaviors
|
53 |
//tailgating?
|
54 |
//read barcode
|
55 |
sign = doDrive(200);
|
56 |
if(button1_click()){
|
57 |
state = SINTERSECTION; |
58 |
} |
59 |
break;
|
60 |
case SINTERSECTION:/*Entering, and in intersection*/ |
61 |
intersectionNum = 0;
|
62 |
/*Intersection queue:
|
63 |
*Each robot when entering the intersection will check for other robots
|
64 |
*in the intersection, and insert itself in a queue to go through.
|
65 |
*/
|
66 |
prevBot = 0;
|
67 |
nextBot = 0;
|
68 |
|
69 |
sign = 0; //Test code until barcodes integrated |
70 |
|
71 |
enterIntersectionQueue(sign); |
72 |
|
73 |
orb1_set_color(PURPLE); |
74 |
//waits for its turn
|
75 |
|
76 |
waitInIntersectionQueue(); |
77 |
|
78 |
orb1_set_color(RED); |
79 |
//Drives through intersection
|
80 |
driveThroughIntersection(sign); |
81 |
|
82 |
//Exits intersection
|
83 |
sendBuffer[0] = WINTERSECTIONEXIT;
|
84 |
sendBuffer[2] = intersectionNum;//Intersection # |
85 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
86 |
orb1_set_color(ORANGE); |
87 |
while(1){ |
88 |
stop(); |
89 |
sign = doDrive(200);
|
90 |
if(button1_click()){
|
91 |
start(); |
92 |
state = SHIGHWAY; |
93 |
break;
|
94 |
} |
95 |
if(button2_click()){
|
96 |
start(); |
97 |
state = SROAD; |
98 |
break;
|
99 |
} |
100 |
} |
101 |
break;
|
102 |
case SHIGHWAY:/*On highway*/ |
103 |
orb1_set_color(CYAN); |
104 |
while(!button1_click()){
|
105 |
highwayFSM(); |
106 |
} |
107 |
state = SINTERSECTION; |
108 |
break;
|
109 |
default:
|
110 |
usb_puts("I got stuck in an unknown state! My state is ");
|
111 |
usb_puti(state); |
112 |
} |
113 |
} |
114 |
|
115 |
} |
116 |
|
117 |
void enterIntersectionQueue(int sign){ |
118 |
|
119 |
//Choose turn direction
|
120 |
intersectionNum = getIntersectType(sign); |
121 |
turnDir = validateTurn(sign, getTurnType(4));
|
122 |
//Sends packet announcing its entry to the intersection
|
123 |
sendBuffer[0] = WINTERSECTIONENTRY;
|
124 |
sendBuffer[2] = intersectionNum;//Intersection # |
125 |
sendBuffer[3] = getIntersectPos(sign);
|
126 |
sendBuffer[4] = turnDir;
|
127 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
128 |
orb1_set_color(BLUE); |
129 |
stop(); |
130 |
doDrive(0);
|
131 |
rtc_reset(); |
132 |
while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue |
133 |
dataLength = 0;
|
134 |
packet = NULL;
|
135 |
packet = wl_basic_do_default(&dataLength); |
136 |
if(packet && dataLength==PACKET_LENGTH && packet[2]==intersectionNum){ |
137 |
if(packet[0] == WINTERSECTIONREPLY){ |
138 |
if(packet[3] == id){//Reply for me |
139 |
prevBot = packet[1];
|
140 |
orb2_set_color(GREEN); |
141 |
break;
|
142 |
}else if(packet[1] != id){//Someone else got here first, try again |
143 |
sendBuffer[0] = WINTERSECTIONENTRY;
|
144 |
sendBuffer[2] = intersectionNum;
|
145 |
sendBuffer[3] = getIntersectPos(sign);
|
146 |
sendBuffer[4] = turnDir;
|
147 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
148 |
orb2_set_color(ORANGE); |
149 |
rtc_reset(); |
150 |
} |
151 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0 && prevBot!=packet[1]){ |
152 |
sendBuffer[0] = WINTERSECTIONREPLY;
|
153 |
sendBuffer[2] = intersectionNum;
|
154 |
sendBuffer[3] = packet[1]; |
155 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
156 |
nextBot = packet[1];
|
157 |
nextDir = packet[3];
|
158 |
nextPath = packet[4];
|
159 |
orb2_set_color(BLUE); |
160 |
//delay_ms(200);
|
161 |
} |
162 |
delay_ms(0);
|
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
void waitInIntersectionQueue(){
|
168 |
while(prevBot != 0){ |
169 |
dataLength = 0;
|
170 |
packet = NULL;
|
171 |
packet = wl_basic_do_default(&dataLength); |
172 |
if(packet && dataLength==PACKET_LENGTH){
|
173 |
if(packet[2] == intersectionNum){ |
174 |
if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){ |
175 |
prevBot = 0;
|
176 |
orb2_set_color(PURPLE); |
177 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){ |
178 |
sendBuffer[0] = WINTERSECTIONREPLY;
|
179 |
sendBuffer[2] = intersectionNum;
|
180 |
sendBuffer[3] = packet[1]; |
181 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
182 |
nextBot = packet[1];
|
183 |
nextDir = packet[3];
|
184 |
nextPath = packet[4];
|
185 |
orb2_set_color(BLUE); |
186 |
} |
187 |
} |
188 |
/*
|
189 |
if(ISPING(packet)){
|
190 |
sendBuffer[0] = WPINGREPLY;
|
191 |
sendBuffer[2] = packet[1];
|
192 |
if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
|
193 |
nextBot = packet[1];
|
194 |
}
|
195 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
196 |
}
|
197 |
if(packet[0]==WPINGREPLY && packet[2]==id){
|
198 |
prevBot = packet[1];
|
199 |
pingWaitTime = rtc_get();
|
200 |
}
|
201 |
*/
|
202 |
} |
203 |
|
204 |
if(prevBot && rtc_get() << 12 == 0){ |
205 |
sendBuffer[0] = WPINGBOT;
|
206 |
sendBuffer[2] = prevBot;
|
207 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
208 |
pingWaitTime = rtc_get(); |
209 |
} |
210 |
if(prevBot && pingWaitTime - rtc_get() > 4){ |
211 |
sendBuffer[0] = WPINGBOT;
|
212 |
if(pingWaitTime - rtc_get() > 8){ |
213 |
sendBuffer[0] = WPINGQUEUE;
|
214 |
} |
215 |
sendBuffer[2] = prevBot;
|
216 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
217 |
} |
218 |
/* if(prevBot && pingWaitTime - rtc_get() > 12){
|
219 |
prevBot = 0;
|
220 |
}
|
221 |
*/
|
222 |
} |
223 |
} |
224 |
|
225 |
void driveThroughIntersection(int sign){ |
226 |
//Code to choose path through intersection goes in this while loop
|
227 |
//But here's the code to handle wireless while in the intersection...
|
228 |
start(); |
229 |
turn(getIntersectType(0), turnDir);
|
230 |
while(doDrive(200) != FINISHED){ |
231 |
dataLength = 0;
|
232 |
packet = NULL;
|
233 |
packet = wl_basic_do_default(&dataLength); |
234 |
if(dataLength==PACKET_LENGTH){
|
235 |
if(packet[2]==intersectionNum/*Intersection Num*/){ |
236 |
if(packet[0]==WINTERSECTIONEXIT){ |
237 |
prevBot = 0;
|
238 |
orb2_set_color(RED); |
239 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){ |
240 |
sendBuffer[0] = WINTERSECTIONREPLY;
|
241 |
sendBuffer[2] = intersectionNum;//Intersection # |
242 |
sendBuffer[3] = packet[1]; |
243 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
244 |
nextBot = packet[1];
|
245 |
nextDir = packet[3];
|
246 |
nextPath = packet[4];
|
247 |
orb2_set_color(YELLOW); |
248 |
} |
249 |
} |
250 |
if(ISPING(packet)){
|
251 |
sendBuffer[0] = WPINGREPLY;
|
252 |
sendBuffer[2] = packet[1]; |
253 |
if(packet[0]==WPINGQUEUE && packet[3]==nextBot){ |
254 |
nextBot = packet[1];
|
255 |
} |
256 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
257 |
} |
258 |
} |
259 |
if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time |
260 |
/*if(bots paths won't collide){
|
261 |
sendBuffer[0] = WINTERSECTIONGO;
|
262 |
sendBuffer[2] = 0;//Intersection #
|
263 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
264 |
*/
|
265 |
} |
266 |
} |
267 |
} |