Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / traffic_navigation / main.c @ 1963

History | View | Annotate | Download (6.89 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

    
10
        static int state, sign, dataLength, pingWaitTime, turnDir;
11
        static char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, nextDir, nextPath, intersectionNum;
12
        static unsigned char *packet;
13

    
14
        void enterIntersectionQueue(int sign);
15
        void waitInIntersectionQueue();
16
        void driveThroughIntersection(int sign);
17
        
18
int main (void) {
19
        
20
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
21
        dragonfly_init(ALL_ON);
22
        xbee_init();
23
        encoders_init();
24
        lineDrive_init();
25
        rtc_init(SIXTEENTH_SECOND, NULL);        
26
        wl_basic_init_default();
27
        wl_set_channel(13);
28
        initializeData();
29
        
30
        id = get_robotid();
31
        sign = 0;
32
        
33
        //Test code
34
        state = SROAD;
35

    
36
        sendBuffer[1] = id;
37

    
38
        /*
39
        doDrive(200);
40
        turn(DOUBLE, ILEFT);
41
        */
42
/*        while(1)
43
                doDrive(255);
44
        */
45
        
46
        while (1) {
47
                /*DTM Finite State Machine*/
48
                switch(state){
49
                case SROAD:/*Following a normal road*/
50
//                        sign = lineFollow();
51
                        //other road behaviors
52
                                //tailgating?
53
                        //read barcode
54
                        sign = doDrive(200);
55
                        if(button1_click()){
56
                                state = SINTERSECTION;
57
                        }
58
                        break;
59
                case SINTERSECTION:/*Entering, and in intersection*/
60
                        intersectionNum = 0;
61
                        /*Intersection queue:
62
                         *Each robot when entering the intersection will check for other robots
63
                         *in the intersection, and insert itself in a queue to go through.
64
                         */
65
                        prevBot = 0;
66
                        nextBot = 0;
67
                        
68
                        sign = 0; //Test code until barcodes integrated
69
                        
70
                        enterIntersectionQueue(sign);
71
                        
72
                        orb1_set_color(PURPLE);
73
                        //waits for its turn
74
                        
75
                        waitInIntersectionQueue();
76
                        
77
                        orb1_set_color(RED);
78
                        //Drives through intersection
79
                        driveThroughIntersection(sign);
80
                        
81
                        //Exits intersection
82
                        sendBuffer[0] = WINTERSECTIONEXIT;
83
                        sendBuffer[2] = intersectionNum;//Intersection #
84
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
85
                        orb1_set_color(ORANGE);
86
                        while(1){
87
                                stop();
88
                                sign = doDrive(200);
89
                                if(button1_click()){
90
                                        start();
91
                                        state = SHIGHWAY;
92
                                        break;
93
                                }
94
                                if(button2_click()){
95
                                        start();
96
                                        state = SROAD;
97
                                        break;
98
                                }
99
                        }
100
                        break;
101
                case SHIGHWAY:/*On highway*/
102
                        orb1_set_color(CYAN);
103
                        while(!button1_click()){
104
                                highwayFSM();
105
                        }
106
                        state = SINTERSECTION;
107
                        break;
108
                default:
109
                        usb_puts("I got stuck in an unknown state! My state is ");
110
                        usb_puti(state);
111
                }
112
        }
113

    
114
}
115

    
116
void enterIntersectionQueue(int sign){
117
                
118
        //Choose turn direction
119
        intersectionNum = getIntersectType(sign);
120
        turnDir = validateTurn(sign, getTurnType(4));
121
        //Sends packet announcing its entry to the intersection
122
        sendBuffer[0] = WINTERSECTIONENTRY;
123
        sendBuffer[2] = intersectionNum;//Intersection #
124
        sendBuffer[3] = getIntersectPos(sign);
125
        sendBuffer[4] = turnDir;
126
        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
127
        orb1_set_color(BLUE);
128
        stop();
129
        doDrive(0);
130
        rtc_reset();
131
        while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue
132
                dataLength = 0;
133
                packet = NULL;
134
                packet = wl_basic_do_default(&dataLength);
135
                if(packet && dataLength==PACKET_LENGTH && packet[2]==intersectionNum){
136
                        if(packet[0] == WINTERSECTIONREPLY){
137
                                if(packet[3] == id){//Reply for me
138
                                        prevBot = packet[1];
139
                                        orb2_set_color(GREEN);
140
                                        break;
141
                                }else if(packet[1] != id){//Someone else got here first, try again
142
                                        sendBuffer[0] = WINTERSECTIONENTRY;
143
                                        sendBuffer[2] = intersectionNum;
144
                                        sendBuffer[3] = getIntersectPos(sign);
145
                                        sendBuffer[4] = turnDir;
146
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
147
                                        orb2_set_color(ORANGE);
148
                                        rtc_reset();
149
                                }
150
                        }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0 && prevBot!=packet[1]){
151
                                sendBuffer[0] = WINTERSECTIONREPLY;
152
                                sendBuffer[2] = intersectionNum;
153
                                sendBuffer[3] = packet[1];
154
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
155
                                nextBot = packet[1];
156
                                nextDir = packet[3];
157
                                nextPath = packet[4];
158
                                orb2_set_color(BLUE);
159
                                //delay_ms(200);
160
                        }
161
                                delay_ms(0);
162
                }
163
        }
164
}
165

    
166
void waitInIntersectionQueue(){
167
        while(prevBot != 0){
168
                dataLength = 0;
169
                packet = NULL;
170
                packet = wl_basic_do_default(&dataLength);
171
                if(packet && dataLength==PACKET_LENGTH){
172
                        if(packet[2] == intersectionNum){
173
                                if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){
174
                                        prevBot = 0;
175
                                        orb2_set_color(PURPLE);
176
                                }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
177
                                        sendBuffer[0] = WINTERSECTIONREPLY;
178
                                        sendBuffer[2] = intersectionNum;
179
                                        sendBuffer[3] = packet[1];
180
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
181
                                        nextBot = packet[1];
182
                                        nextDir = packet[3];
183
                                        nextPath = packet[4];
184
                                        orb2_set_color(BLUE);
185
                                }
186
                        }
187
/*
188
                        if(ISPING(packet)){
189
                                sendBuffer[0] = WPINGREPLY;
190
                                sendBuffer[2] = packet[1];
191
                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
192
                                        nextBot = packet[1];
193
                                }
194
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
195
                        }
196
                        if(packet[0]==WPINGREPLY && packet[2]==id){
197
                                prevBot = packet[1];
198
                                pingWaitTime = rtc_get();
199
                        }
200
*/
201
                }
202
                                        
203
                if(prevBot && rtc_get() << 12 == 0){
204
                        sendBuffer[0] = WPINGBOT;
205
                        sendBuffer[2] = prevBot;
206
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
207
                        pingWaitTime = rtc_get();
208
                }
209
                if(prevBot && pingWaitTime - rtc_get() > 4){
210
                        sendBuffer[0] = WPINGBOT;
211
                        if(pingWaitTime - rtc_get() > 8){
212
                                sendBuffer[0] = WPINGQUEUE;
213
                        }
214
                        sendBuffer[2] = prevBot;
215
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
216
                }
217
/*                                if(prevBot && pingWaitTime - rtc_get() > 12){
218
                        prevBot = 0;
219
                }
220
*/
221
        }
222
}
223

    
224
void driveThroughIntersection(int sign){
225
        //Code to choose path through intersection goes in this while loop
226
        //But here's the code to handle wireless while in the intersection...
227
        start();
228
        turn(getIntersectType(0), turnDir);
229
        while(doDrive(200) != FINISHED){
230
                dataLength = 0;
231
                packet = NULL;
232
                packet = wl_basic_do_default(&dataLength);                
233
                if(dataLength==PACKET_LENGTH){
234
                        if(packet[2]==intersectionNum/*Intersection Num*/){
235
                                if(packet[0]==WINTERSECTIONEXIT){
236
                                        prevBot = 0;
237
                                        orb2_set_color(RED);
238
                                }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
239
                                        sendBuffer[0] = WINTERSECTIONREPLY;
240
                                        sendBuffer[2] = intersectionNum;//Intersection #
241
                                        sendBuffer[3] = packet[1];
242
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
243
                                        nextBot = packet[1];
244
                                        nextDir = packet[3];
245
                                        nextPath = packet[4];
246
                                        orb2_set_color(YELLOW);
247
                                }
248
                        }
249
                        if(ISPING(packet)){
250
                                sendBuffer[0] = WPINGREPLY;
251
                                sendBuffer[2] = packet[1];
252
                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
253
                                        nextBot = packet[1];
254
                                }
255
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
256
                        }
257
                }
258
                if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time
259
                        /*if(bots paths won't collide){
260
                                sendBuffer[0] = WINTERSECTIONGO;
261
                                sendBuffer[2] = 0;//Intersection #
262
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
263
                        */
264
                }
265
        }
266
}