Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / traffic_navigation / main-intersectionTest.c @ 1984

History | View | Annotate | Download (6.99 KB)

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