root / trunk / code / projects / traffic_navigation / main.c @ 1987
History | View | Annotate | Download (6.99 KB)
1 | 1843 | bwasserm | /*
|
---|---|---|---|
2 | * main.c for Traffic Navigation
|
||
3 | * Runs the highest level behavior for the Dynamic Traffic Navigation (DTM) SURG
|
||
4 | *
|
||
5 | 1869 | bwasserm | * Author: Colony Project, CMU Robotics Club
|
6 | */
|
||
7 | 1843 | bwasserm | |
8 | 1872 | josephle | #include "traffic_navigation.h" |
9 | 1966 | alevkoy | #include "../linefollowing/lineDrive.h" |
10 | 1972 | azl | #ifndef MAIN_NEW
|
11 | 1977 | azl | #ifndef WIRELESSWATCH
|
12 | 16 | bcoltin | |
13 | 1957 | azl | 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 | 1888 | bwasserm | |
17 | 1896 | bwasserm | void enterIntersectionQueue(int sign); |
18 | 1966 | alevkoy | void waitInIntersectionQueue(void); |
19 | 1896 | bwasserm | void driveThroughIntersection(int sign); |
20 | |||
21 | 1843 | bwasserm | int main (void) { |
22 | 1896 | bwasserm | |
23 | 1843 | bwasserm | /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
|
24 | dragonfly_init(ALL_ON); |
||
25 | xbee_init(); |
||
26 | encoders_init(); |
||
27 | 1876 | markwill | lineDrive_init(); |
28 | 1844 | bwasserm | rtc_init(SIXTEENTH_SECOND, NULL);
|
29 | 1859 | bwasserm | wl_basic_init_default(); |
30 | wl_set_channel(13);
|
||
31 | 1896 | bwasserm | initializeData(); |
32 | 1859 | bwasserm | |
33 | 1844 | bwasserm | id = get_robotid(); |
34 | 1843 | bwasserm | sign = 0;
|
35 | |||
36 | 1859 | bwasserm | //Test code
|
37 | 1901 | bwasserm | state = SROAD; |
38 | 1859 | bwasserm | |
39 | 1844 | bwasserm | sendBuffer[1] = id;
|
40 | 1901 | bwasserm | |
41 | 1957 | azl | /*
|
42 | doDrive(200);
|
||
43 | turn(DOUBLE, ILEFT);
|
||
44 | */
|
||
45 | /* while(1)
|
||
46 | doDrive(255);
|
||
47 | */
|
||
48 | |||
49 | 1843 | bwasserm | while (1) { |
50 | /*DTM Finite State Machine*/
|
||
51 | switch(state){
|
||
52 | case SROAD:/*Following a normal road*/ |
||
53 | 1859 | bwasserm | // sign = lineFollow();
|
54 | 1843 | bwasserm | //other road behaviors
|
55 | //tailgating?
|
||
56 | //read barcode
|
||
57 | 1901 | bwasserm | sign = doDrive(200);
|
58 | if(button1_click()){
|
||
59 | 1843 | bwasserm | state = SINTERSECTION; |
60 | } |
||
61 | break;
|
||
62 | case SINTERSECTION:/*Entering, and in intersection*/ |
||
63 | 1859 | bwasserm | intersectionNum = 0;
|
64 | 1844 | bwasserm | /*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 | 1888 | bwasserm | |
71 | 1913 | bwasserm | sign = 0; //Test code until barcodes integrated |
72 | 1896 | bwasserm | |
73 | enterIntersectionQueue(sign); |
||
74 | |||
75 | 1859 | bwasserm | orb1_set_color(PURPLE); |
76 | 1844 | bwasserm | //waits for its turn
|
77 | 1888 | bwasserm | |
78 | waitInIntersectionQueue(); |
||
79 | |||
80 | 1859 | bwasserm | orb1_set_color(RED); |
81 | 1844 | bwasserm | //Drives through intersection
|
82 | 1896 | bwasserm | driveThroughIntersection(sign); |
83 | 1888 | bwasserm | |
84 | 1844 | bwasserm | //Exits intersection
|
85 | sendBuffer[0] = WINTERSECTIONEXIT;
|
||
86 | 1859 | bwasserm | sendBuffer[2] = intersectionNum;//Intersection # |
87 | 1844 | bwasserm | wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
|
88 | 1859 | bwasserm | orb1_set_color(ORANGE); |
89 | 1888 | bwasserm | while(1){ |
90 | 1891 | bwasserm | stop(); |
91 | 1901 | bwasserm | sign = doDrive(200);
|
92 | 1913 | bwasserm | if(button1_click()){
|
93 | 1891 | bwasserm | start(); |
94 | 1913 | bwasserm | state = SHIGHWAY; |
95 | break;
|
||
96 | 1843 | bwasserm | } |
97 | 1913 | bwasserm | if(button2_click()){
|
98 | start(); |
||
99 | state = SROAD; |
||
100 | break;
|
||
101 | } |
||
102 | 1843 | bwasserm | } |
103 | break;
|
||
104 | case SHIGHWAY:/*On highway*/ |
||
105 | 1901 | bwasserm | orb1_set_color(CYAN); |
106 | while(!button1_click()){
|
||
107 | 1891 | bwasserm | highwayFSM(); |
108 | 1901 | bwasserm | } |
109 | state = SINTERSECTION; |
||
110 | 1888 | bwasserm | break;
|
111 | 1843 | bwasserm | default:
|
112 | usb_puts("I got stuck in an unknown state! My state is ");
|
||
113 | usb_puti(state); |
||
114 | } |
||
115 | 967 | alevkoy | } |
116 | |||
117 | 16 | bcoltin | } |
118 | 1888 | bwasserm | |
119 | 1896 | bwasserm | void enterIntersectionQueue(int sign){ |
120 | |||
121 | //Choose turn direction
|
||
122 | intersectionNum = getIntersectType(sign); |
||
123 | 1913 | bwasserm | turnDir = validateTurn(sign, getTurnType(4));
|
124 | 1888 | bwasserm | //Sends packet announcing its entry to the intersection
|
125 | sendBuffer[0] = WINTERSECTIONENTRY;
|
||
126 | sendBuffer[2] = intersectionNum;//Intersection # |
||
127 | 1896 | bwasserm | sendBuffer[3] = getIntersectPos(sign);
|
128 | sendBuffer[4] = turnDir;
|
||
129 | 1888 | bwasserm | 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 | 1896 | bwasserm | sendBuffer[2] = intersectionNum;
|
147 | sendBuffer[3] = getIntersectPos(sign);
|
||
148 | sendBuffer[4] = turnDir;
|
||
149 | 1888 | bwasserm | 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 | 1896 | bwasserm | void driveThroughIntersection(int sign){ |
228 | 1888 | bwasserm | //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 | 1912 | pdeo | turn(getIntersectType(0), turnDir);
|
232 | 1901 | bwasserm | while(doDrive(200) != FINISHED){ |
233 | 1888 | bwasserm | 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 | 1905 | pdeo | } |
270 | 1972 | azl | #endif
|
271 | 1977 | azl | #endif |