Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.61 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;
11
        static char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, nextDir, nextPath, intersectionNum;
12
        static unsigned char *packet;
13

    
14
int main (void) {
15

    
16
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
17
        dragonfly_init(ALL_ON);
18
        xbee_init();
19
        encoders_init();
20
        lineDrive_init();
21
        rtc_init(SIXTEENTH_SECOND, NULL);        
22
        wl_basic_init_default();
23
        wl_set_channel(13);
24
        
25
        id = get_robotid();
26
        sign = 0;
27
        
28
        //Test code
29
        state = SROAD;
30

    
31
        sendBuffer[1] = id;
32

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

    
105
}
106

    
107
void enterIntersectionQueue(){
108
        //Sends packet announcing its entry to the intersection
109
        sendBuffer[0] = WINTERSECTIONENTRY;
110
        sendBuffer[2] = intersectionNum;//Intersection #
111
        sendBuffer[3] = 0;//From Direction
112
        sendBuffer[4] = 2;//To Direction
113
        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
114
        orb1_set_color(BLUE);
115
        stop();
116
        doDrive(0);
117
        rtc_reset();
118
        while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue
119
                dataLength = 0;
120
                packet = NULL;
121
                packet = wl_basic_do_default(&dataLength);
122
                if(packet && dataLength==PACKET_LENGTH && packet[2]==intersectionNum){
123
                        if(packet[0] == WINTERSECTIONREPLY){
124
                                if(packet[3] == id){//Reply for me
125
                                        prevBot = packet[1];
126
                                        orb2_set_color(GREEN);
127
                                        break;
128
                                }else if(packet[1] != id){//Someone else got here first, try again
129
                                        sendBuffer[0] = WINTERSECTIONENTRY;
130
                                        sendBuffer[2] = intersectionNum;//Intersection #
131
                                        sendBuffer[3] = 0;//From Direction
132
                                        sendBuffer[4] = 2;//To Direction
133
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
134
                                        orb2_set_color(ORANGE);
135
                                        rtc_reset();
136
                                }
137
                        }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0 && prevBot!=packet[1]){
138
                                sendBuffer[0] = WINTERSECTIONREPLY;
139
                                sendBuffer[2] = intersectionNum;
140
                                sendBuffer[3] = packet[1];
141
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
142
                                nextBot = packet[1];
143
                                nextDir = packet[3];
144
                                nextPath = packet[4];
145
                                orb2_set_color(BLUE);
146
                                //delay_ms(200);
147
                        }
148
                                delay_ms(0);
149
                }
150
        }
151
}
152

    
153
void waitInIntersectionQueue(){
154
        while(prevBot != 0){
155
                dataLength = 0;
156
                packet = NULL;
157
                packet = wl_basic_do_default(&dataLength);
158
                if(packet && dataLength==PACKET_LENGTH){
159
                        if(packet[2] == intersectionNum){
160
                                if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){
161
                                        prevBot = 0;
162
                                        orb2_set_color(PURPLE);
163
                                }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
164
                                        sendBuffer[0] = WINTERSECTIONREPLY;
165
                                        sendBuffer[2] = intersectionNum;
166
                                        sendBuffer[3] = packet[1];
167
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
168
                                        nextBot = packet[1];
169
                                        nextDir = packet[3];
170
                                        nextPath = packet[4];
171
                                        orb2_set_color(BLUE);
172
                                }
173
                        }
174
/*
175
                        if(ISPING(packet)){
176
                                sendBuffer[0] = WPINGREPLY;
177
                                sendBuffer[2] = packet[1];
178
                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
179
                                        nextBot = packet[1];
180
                                }
181
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
182
                        }
183
                        if(packet[0]==WPINGREPLY && packet[2]==id){
184
                                prevBot = packet[1];
185
                                pingWaitTime = rtc_get();
186
                        }
187
*/
188
                }
189
                                        
190
                if(prevBot && rtc_get() << 12 == 0){
191
                        sendBuffer[0] = WPINGBOT;
192
                        sendBuffer[2] = prevBot;
193
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
194
                        pingWaitTime = rtc_get();
195
                }
196
                if(prevBot && pingWaitTime - rtc_get() > 4){
197
                        sendBuffer[0] = WPINGBOT;
198
                        if(pingWaitTime - rtc_get() > 8){
199
                                sendBuffer[0] = WPINGQUEUE;
200
                        }
201
                        sendBuffer[2] = prevBot;
202
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
203
                }
204
/*                                if(prevBot && pingWaitTime - rtc_get() > 12){
205
                        prevBot = 0;
206
                }
207
*/
208
        }
209
}
210

    
211
void driveThroughIntersection(){
212
        //Code to choose path through intersection goes in this while loop
213
        //But here's the code to handle wireless while in the intersection...
214
        start();
215
        turn(DOUBLE, IRIGHT);//Change paramers to variables
216
        while(doDrive(255) != FINISHED){
217
                dataLength = 0;
218
                packet = NULL;
219
                packet = wl_basic_do_default(&dataLength);                
220
                if(dataLength==PACKET_LENGTH){
221
                        if(packet[2]==intersectionNum/*Intersection Num*/){
222
                                if(packet[0]==WINTERSECTIONEXIT){
223
                                        prevBot = 0;
224
                                        orb2_set_color(RED);
225
                                }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
226
                                        sendBuffer[0] = WINTERSECTIONREPLY;
227
                                        sendBuffer[2] = intersectionNum;//Intersection #
228
                                        sendBuffer[3] = packet[1];
229
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
230
                                        nextBot = packet[1];
231
                                        nextDir = packet[3];
232
                                        nextPath = packet[4];
233
                                        orb2_set_color(YELLOW);
234
                                }
235
                        }
236
                        if(ISPING(packet)){
237
                                sendBuffer[0] = WPINGREPLY;
238
                                sendBuffer[2] = packet[1];
239
                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
240
                                        nextBot = packet[1];
241
                                }
242
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
243
                        }
244
                }
245
                if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time
246
                        /*if(bots paths won't collide){
247
                                sendBuffer[0] = WINTERSECTIONGO;
248
                                sendBuffer[2] = 0;//Intersection #
249
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
250
                        */
251
                }
252
        }
253
}