Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.57 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
                                sign = doDrive(255);
79
                                if((sign >= 0) || button1_click()){
80
                                        if(0){
81
                                                state = SHIGHWAY;
82
                                                break;
83
                                        }else{
84
                                                state = SROAD;
85
                                                break;
86
                                        }
87
                                }
88
                        }
89
                        break;
90
                case SHIGHWAY:/*On highway*/
91
//                        sign = lineFollow();
92
                        //highway behaviors
93
                                //merging
94
                                //passing
95
                        //read barcode
96
                        break;
97
                default:
98
                        usb_puts("I got stuck in an unknown state! My state is ");
99
                        usb_puti(state);
100
                }
101
        }
102

    
103
}
104

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

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

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