Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.96 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
#ifndef MAIN_NEW
11

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

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

    
38
        sendBuffer[1] = id;
39

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

    
116
}
117

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

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

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