Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (8.21 KB)

1
/*
2
 * main.c for Traffic Navigation
3
 * Runs the highest level behavior for the Dynamic Traffic Navigation (DTM) SURG
4
 *
5
 * Author: Benjamin Wasserman, Colony Project, CMU Robotics Club */
6

    
7
#include <dragonfly_lib.h>
8
#include <wl_basic.h>
9
#include "../linefollowing/lineDrive.h"
10

    
11

    
12
/*States*/
13
#define SROAD 0
14
#define SINTERSECTION 10
15
#define SHIGHWAY 20
16

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

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

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

    
64
void pingReply(char ping[PACKET_LENGTH], char sendBuffer[PACKET_LENGTH], char nextBot){
65
        sendBuffer[0] = WPINGREPLY;
66
        switch(ping[0]){
67
                case WPINGBOT:
68
                        sendBuffer[2] = ping[1];
69
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
70
                        break;
71
                case WPINGQUEUE:
72
                        sendBuffer[2] = ping[1];
73
                        if(ping[3] == get_robotid()){
74
                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
75
                        }
76
                        if(ping[3] == nextBot){
77
                                //If next bot is missing from queue, take its place
78
                        }
79
                        break;
80
                default:
81
                        break;
82
        }
83
}
84

    
85
int main (void) {
86

    
87
        int state, sign, dataLength, pingWaitTime;
88
        char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, *packet, nextDir, nextPath, intersectionNum;
89

    
90
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
91
        dragonfly_init(ALL_ON);
92
        xbee_init();
93
        encoders_init();
94
        lineDrive_init();
95
        rtc_init(SIXTEENTH_SECOND, NULL);        
96
        wl_basic_init_default();
97
        wl_set_channel(13);
98
        
99
        id = get_robotid();
100
        sign = 0;
101
        
102
        //Test code
103
        state = SROAD;
104

    
105
        sendBuffer[1] = id;
106

    
107
        while (1) {
108
                /*DTM Finite State Machine*/
109
                switch(state){
110
                case SROAD:/*Following a normal road*/
111
//                        sign = lineFollow();
112
                        //other road behaviors
113
                                //tailgating?
114
                        //read barcode
115
                        doDrive(255);
116
                        if((sign & CINTERSECTION) || button1_click()){
117
                                state = SINTERSECTION;
118
                        }
119
                        break;
120
                case SINTERSECTION:/*Entering, and in intersection*/
121
                        //Intersection behaviors
122
                                //queueing
123
                                //no-stop?
124
                        //check wireless
125
                        
126
                        intersectionNum = 0;
127

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

    
277
}