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