Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (8.41 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 <dragonfly_lib.h>
9
#include <wl_basic.h>
10
#include "../linefollowing/lineDrive.h"
11

    
12

    
13

    
14
/*States*/
15
#define SROAD 0
16
#define SINTERSECTION 10
17
#define SHIGHWAY 20
18

    
19
/*Sign Codes
20
 * bitwise OR labels to create a barcode or read one
21
 * There should be macros to extract these probably
22
 * The bits will be stored in some variable (char or short)
23
 * Bits if road: ? ? ? NAME NAME NAME TYPE CROAD
24
 * Bits if intersection: ? ? ? ? DIR DIR #WAYS CINTERSECTION
25
 */
26
#define CROAD 0x0 //0b
27
#define CINTERSECTION 0x1 //1b
28
#define CNORMALROAD 0x0 //00b
29
#define CHIGHWAYROAD 0x2 //10b
30
#define C4WAY 0x0 //00b
31
#define C3WAY 0x2 //10b
32
#define CNORTH 0x0 //0000b
33
#define CEAST 0x4 //0100b
34
#define CSOUTH 0x8 //1000b
35
#define CWEST 0x12 //1100b
36

    
37
/*Wireless Packet Types
38
 * The first byte of any wireless packet should be one of these types.
39
 * Each type will have its own structure
40
 * The second byte should be the id of the bot sending the packet
41
 * The third byte should be the number of the intersection or road that
42
 *   the packet pertains to
43
 */
44
#define PACKET_LENGTH 5
45
#define WROADENTRY 0 //[type, bot, road]
46
#define WROADREPLY 1 //[type, fromBot, road, toBot]
47
#define WROADEXIT 2 //[type, bot, road]
48
#define WROADSTOP 3 //[type, bot, road]
49
#define WINTERSECTIONENTRY 10 //[type, bot, intersection, fromDir, toDir]
50
#define WINTERSECTIONREPLY 11 //[type, fromBot, intersection, toBot]
51
#define WINTERSECTIONEXIT 12 //[type, bot, intersection]
52
#define WINTERSECTIONGO 13 //[type, bot, intersection]
53
#define WINTERSECTIONPOLICEENTRY 14
54
#define WHIGHWAYENTRY 20 //[type, bot, highway]
55
#define WHIGHWAYREPLY 21 //[type, fromBot, highway, toBot]
56
#define WHIGHWAYEXIT 22 //[type, bot, highway]
57
#define WPINGGLOBAL 30 //[type, bot]
58
#define WPINGBOT 31 //[type, fromBot, toBot]
59
#define WPINGQUEUE 32 //[type, fromBot, toBot]
60
#define WPINGREPLY 33 //[type, fromBot, toBot]
61

    
62
/*Macros
63
 */
64
#define ISPING(p) ((p)[0]==WPINGGLOBAL || (p)[0]==WPINGBOT || (p)[0]==WPINGQUEUE)
65

    
66
int main (void) {
67

    
68
        int state, sign, dataLength, pingWaitTime;
69
        char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, nextDir, nextPath, intersectionNum;
70
        unsigned char *packet;
71

    
72
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
73
        dragonfly_init(ALL_ON);
74
        xbee_init();
75
        encoders_init();
76
//        lineDrive_init();
77
        rtc_init(SIXTEENTH_SECOND, NULL);        
78
        wl_basic_init_default();
79
        wl_set_channel(13);
80
        
81
        id = get_robotid();
82
        sign = 0;
83
        
84
        //Test code
85
        state = SROAD;
86

    
87
        sendBuffer[1] = id;
88

    
89
        while (1) {
90
                /*DTM Finite State Machine*/
91
                switch(state){
92
                case SROAD:/*Following a normal road*/
93
//                        sign = lineFollow();
94
                        //other road behaviors
95
                                //tailgating?
96
                        //read barcode
97
//                        doDrive(255);
98
                        if((sign & CINTERSECTION) || button1_click()){
99
                                state = SINTERSECTION;
100
                        }
101
                        break;
102
                case SINTERSECTION:/*Entering, and in intersection*/
103
                        //Intersection behaviors
104
                                //queueing
105
                                //no-stop?
106
                        //check wireless
107
                        
108
                        intersectionNum = 0;
109

    
110
                        /*Intersection queue:
111
                         *Each robot when entering the intersection will check for other robots
112
                         *in the intersection, and insert itself in a queue to go through.
113
                         */
114
                        prevBot = 0;
115
                        nextBot = 0;
116
                        //Sends packet announcing its entry to the intersection
117
                        sendBuffer[0] = WINTERSECTIONENTRY;
118
                        sendBuffer[2] = intersectionNum;//Intersection #
119
                        sendBuffer[3] = 0;//From Direction
120
                        sendBuffer[4] = 2;//To Direction
121
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
122
                        orb1_set_color(BLUE);
123
//                        stop();
124
                        rtc_reset();
125
                        while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue
126
                                dataLength = 0;
127
                                packet = wl_basic_do_default(&dataLength);
128
                                if(dataLength==PACKET_LENGTH && packet[2]==intersectionNum){
129
                                        if(packet[0] == WINTERSECTIONREPLY){
130
                                                if(packet[3] == id){//Reply for me
131
                                                        prevBot = packet[1];
132
                                                        orb2_set_color(GREEN);
133
                                                        break;
134
                                                }else if(packet[1] != id){//Someone else got here first, try again
135
                                                        sendBuffer[0] = WINTERSECTIONENTRY;
136
                                                        sendBuffer[2] = intersectionNum;//Intersection #
137
                                                        sendBuffer[3] = 0;//From Direction
138
                                                        sendBuffer[4] = 2;//To Direction
139
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
140
                                                        orb2_set_color(ORANGE);
141
                                                        rtc_reset();
142
                                                }
143
                                        }else /*if(packet[0]==WINTERSECTIONENTRY && nextBot==0 && prevBot!=packet[1]){
144
                                                sendBuffer[0] = WINTERSECTIONREPLY;
145
                                                sendBuffer[2] = intersectionNum;
146
                                                sendBuffer[3] = packet[1];
147
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
148
                                                nextBot = packet[1];
149
                                                nextDir = packet[3];
150
                                                nextPath = packet[4];
151
                                                orb2_set_color(BLUE);
152
                                                delay_ms(200);
153
                                        }*/
154
                                                delay_ms(0);
155
                                }
156
                        }
157
                        orb1_set_color(PURPLE);
158
                        //waits for its turn
159
                        while(prevBot != 0){
160
                                dataLength = 0;
161
                                packet = wl_basic_do_default(&dataLength);
162
                                if(dataLength==PACKET_LENGTH){
163
                                        if(packet[2] == intersectionNum){
164
                                                if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){
165
                                                        prevBot = 0;
166
                                                        orb2_set_color(PURPLE);
167
                                                }/*else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
168
                                                        sendBuffer[0] = WINTERSECTIONREPLY;
169
                                                        sendBuffer[2] = intersectionNum;
170
                                                        sendBuffer[3] = packet[1];
171
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
172
                                                        nextBot = packet[1];
173
                                                        nextDir = packet[3];
174
                                                        nextPath = packet[4];
175
                                                        orb2_set_color(BLUE);
176
                                                }*/
177
                                        }
178
/*                                        if(ISPING(packet)){
179
                                                sendBuffer[0] = WPINGREPLY;
180
                                                sendBuffer[2] = packet[1];
181
                                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
182
                                                        nextBot = packet[1];
183
                                                }
184
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
185
                                        }
186
                                        if(packet[0]==WPINGREPLY && packet[2]==id){
187
                                                prevBot = packet[1];
188
                                                pingWaitTime = rtc_get();
189
                                        }
190
*/                                }
191
                                                        
192
                                if(prevBot && rtc_get() << 12 == 0){
193
                                        sendBuffer[0] = WPINGBOT;
194
                                        sendBuffer[2] = prevBot;
195
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
196
                                        pingWaitTime = rtc_get();
197
                                }
198
                                if(prevBot && pingWaitTime - rtc_get() > 4){
199
                                        sendBuffer[0] = WPINGBOT;
200
                                        if(pingWaitTime - rtc_get() > 8){
201
                                                sendBuffer[0] = WPINGQUEUE;
202
                                        }
203
                                        sendBuffer[2] = prevBot;
204
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
205
                                }
206
/*                                if(prevBot && pingWaitTime - rtc_get() > 12){
207
                                        prevBot = 0;
208
                                }
209
*/                        }
210
                        orb1_set_color(RED);
211
                        //Drives through intersection
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, ILEFT);//Change paramers to variables
216
                        while(!button1_click()/*replace with variable to keep track of if in intersection*/){
217
//                                doDrive(255);
218
                                packet = wl_basic_do_default(&dataLength);                
219
                                if(dataLength==PACKET_LENGTH){
220
                                        if(packet[2]==intersectionNum/*Intersection Num*/){
221
                                                if(packet[0]==WINTERSECTIONEXIT){
222
                                                        prevBot = 0;
223
                                                        orb2_set_color(RED);
224
                                                }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
225
                                                        sendBuffer[0] = WINTERSECTIONREPLY;
226
                                                        sendBuffer[2] = intersectionNum;//Intersection #
227
                                                        sendBuffer[3] = packet[1];
228
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
229
                                                        nextBot = packet[1];
230
                                                        nextDir = packet[3];
231
                                                        nextPath = packet[4];
232
                                                        orb2_set_color(YELLOW);
233
                                                }
234
                                        }
235
                                        if(ISPING(packet)){
236
                                                sendBuffer[0] = WPINGREPLY;
237
                                                sendBuffer[2] = packet[1];
238
                                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
239
                                                        nextBot = packet[1];
240
                                                }
241
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
242
                                        }
243
                                }
244
                                if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time
245
                                        /*if(bots paths won't collide){
246
                                                sendBuffer[0] = WINTERSECTIONGO;
247
                                                sendBuffer[2] = 0;//Intersection #
248
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
249
                                        */
250
                                }
251
                        }
252
                        //Exits intersection
253
                        sendBuffer[0] = WINTERSECTIONEXIT;
254
                        sendBuffer[2] = intersectionNum;//Intersection #
255
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
256
                        orb1_set_color(ORANGE);
257
                        
258
                        if(!(sign & CINTERSECTION)){
259
                                if(sign & CHIGHWAYROAD){
260
                                        state = SHIGHWAY;
261
                                }else{
262
                                        state = SROAD;
263
                                }
264
                        }
265
                        break;
266
                case SHIGHWAY:/*On highway*/
267
//                        sign = lineFollow();
268
                        //highway behaviors
269
                                //merging
270
                                //passing
271
                        //read barcode
272
                        break;
273
                default:
274
                        usb_puts("I got stuck in an unknown state! My state is ");
275
                        usb_puti(state);
276
                }
277
        }
278

    
279
}