Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.46 KB)

1 1843 bwasserm
/*
2
 * main.c for Traffic Navigation
3
 * Runs the highest level behavior for the Dynamic Traffic Navigation (DTM) SURG
4
 *
5 1869 bwasserm
 * Author: Colony Project, CMU Robotics Club
6
 */
7 1843 bwasserm
8 1872 josephle
#include "traffic_navigation.h"
9 16 bcoltin
10 1843 bwasserm
int main (void) {
11 1419 jsexton
12 1863 bwasserm
        int state, sign, dataLength, pingWaitTime;
13 1869 bwasserm
        char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, nextDir, nextPath, intersectionNum;
14
        unsigned char *packet;
15 1876 markwill
16 1843 bwasserm
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
17
        dragonfly_init(ALL_ON);
18
        xbee_init();
19
        encoders_init();
20 1876 markwill
        lineDrive_init();
21 1844 bwasserm
        rtc_init(SIXTEENTH_SECOND, NULL);
22 1859 bwasserm
        wl_basic_init_default();
23
        wl_set_channel(13);
24
25 1844 bwasserm
        id = get_robotid();
26 1843 bwasserm
        sign = 0;
27
28 1859 bwasserm
        //Test code
29 1876 markwill
        state = SHIGHWAY;
30 1859 bwasserm
31 1844 bwasserm
        sendBuffer[1] = id;
32
33 1843 bwasserm
        while (1) {
34
                /*DTM Finite State Machine*/
35
                switch(state){
36
                case SROAD:/*Following a normal road*/
37 1859 bwasserm
//                        sign = lineFollow();
38 1843 bwasserm
                        //other road behaviors
39
                                //tailgating?
40
                        //read barcode
41 1869 bwasserm
//                        doDrive(255);
42 1859 bwasserm
                        if((sign & CINTERSECTION) || button1_click()){
43 1843 bwasserm
                                state = SINTERSECTION;
44
                        }
45
                        break;
46
                case SINTERSECTION:/*Entering, and in intersection*/
47
                        //Intersection behaviors
48
                                //queueing
49
                                //no-stop?
50
                        //check wireless
51 1844 bwasserm
52 1859 bwasserm
                        intersectionNum = 0;
53
54 1844 bwasserm
                        /*Intersection queue:
55
                         *Each robot when entering the intersection will check for other robots
56
                         *in the intersection, and insert itself in a queue to go through.
57
                         */
58
                        prevBot = 0;
59
                        nextBot = 0;
60
                        //Sends packet announcing its entry to the intersection
61
                        sendBuffer[0] = WINTERSECTIONENTRY;
62 1859 bwasserm
                        sendBuffer[2] = intersectionNum;//Intersection #
63 1844 bwasserm
                        sendBuffer[3] = 0;//From Direction
64 1859 bwasserm
                        sendBuffer[4] = 2;//To Direction
65 1844 bwasserm
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
66 1859 bwasserm
                        orb1_set_color(BLUE);
67 1869 bwasserm
//                        stop();
68 1844 bwasserm
                        rtc_reset();
69
                        while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue
70 1869 bwasserm
                                dataLength = 0;
71 1844 bwasserm
                                packet = wl_basic_do_default(&dataLength);
72 1869 bwasserm
                                if(dataLength==PACKET_LENGTH && packet[2]==intersectionNum){
73
                                        if(packet[0] == WINTERSECTIONREPLY){
74
                                                if(packet[3] == id){//Reply for me
75
                                                        prevBot = packet[1];
76
                                                        orb2_set_color(GREEN);
77
                                                        break;
78
                                                }else if(packet[1] != id){//Someone else got here first, try again
79
                                                        sendBuffer[0] = WINTERSECTIONENTRY;
80
                                                        sendBuffer[2] = intersectionNum;//Intersection #
81
                                                        sendBuffer[3] = 0;//From Direction
82
                                                        sendBuffer[4] = 2;//To Direction
83
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
84
                                                        orb2_set_color(ORANGE);
85
                                                        rtc_reset();
86
                                                }
87
                                        }else /*if(packet[0]==WINTERSECTIONENTRY && nextBot==0 && prevBot!=packet[1]){
88
                                                sendBuffer[0] = WINTERSECTIONREPLY;
89
                                                sendBuffer[2] = intersectionNum;
90
                                                sendBuffer[3] = packet[1];
91 1844 bwasserm
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
92 1869 bwasserm
                                                nextBot = packet[1];
93
                                                nextDir = packet[3];
94
                                                nextPath = packet[4];
95
                                                orb2_set_color(BLUE);
96
                                                delay_ms(200);
97
                                        }*/
98
                                                delay_ms(0);
99 1844 bwasserm
                                }
100
                        }
101 1859 bwasserm
                        orb1_set_color(PURPLE);
102 1844 bwasserm
                        //waits for its turn
103
                        while(prevBot != 0){
104 1869 bwasserm
                                dataLength = 0;
105
                                packet = wl_basic_do_default(&dataLength);
106 1863 bwasserm
                                if(dataLength==PACKET_LENGTH){
107
                                        if(packet[2] == intersectionNum){
108
                                                if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){
109
                                                        prevBot = 0;
110
                                                        orb2_set_color(PURPLE);
111 1869 bwasserm
                                                }/*else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
112 1863 bwasserm
                                                        sendBuffer[0] = WINTERSECTIONREPLY;
113 1869 bwasserm
                                                        sendBuffer[2] = intersectionNum;
114 1863 bwasserm
                                                        sendBuffer[3] = packet[1];
115
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
116
                                                        nextBot = packet[1];
117
                                                        nextDir = packet[3];
118
                                                        nextPath = packet[4];
119
                                                        orb2_set_color(BLUE);
120 1869 bwasserm
                                                }*/
121 1863 bwasserm
                                        }
122 1869 bwasserm
/*                                        if(ISPING(packet)){
123 1863 bwasserm
                                                sendBuffer[0] = WPINGREPLY;
124
                                                sendBuffer[2] = packet[1];
125
                                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
126
                                                        nextBot = packet[1];
127
                                                }
128 1844 bwasserm
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
129
                                        }
130 1863 bwasserm
                                        if(packet[0]==WPINGREPLY && packet[2]==id){
131
                                                prevBot = packet[1];
132
                                                pingWaitTime = rtc_get();
133
                                        }
134 1869 bwasserm
*/                                }
135 1863 bwasserm
136 1869 bwasserm
                                if(prevBot && rtc_get() << 12 == 0){
137 1863 bwasserm
                                        sendBuffer[0] = WPINGBOT;
138
                                        sendBuffer[2] = prevBot;
139
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
140
                                        pingWaitTime = rtc_get();
141 1861 bwasserm
                                }
142 1869 bwasserm
                                if(prevBot && pingWaitTime - rtc_get() > 4){
143 1863 bwasserm
                                        sendBuffer[0] = WPINGBOT;
144
                                        if(pingWaitTime - rtc_get() > 8){
145
                                                sendBuffer[0] = WPINGQUEUE;
146
                                        }
147
                                        sendBuffer[2] = prevBot;
148 1861 bwasserm
                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
149
                                }
150 1869 bwasserm
/*                                if(prevBot && pingWaitTime - rtc_get() > 12){
151 1863 bwasserm
                                        prevBot = 0;
152
                                }
153 1869 bwasserm
*/                        }
154 1859 bwasserm
                        orb1_set_color(RED);
155 1844 bwasserm
                        //Drives through intersection
156
                        //Code to choose path through intersection goes in this while loop
157
                        //But here's the code to handle wireless while in the intersection...
158 1869 bwasserm
//                        start();
159
//                        turn(DOUBLE, ILEFT);//Change paramers to variables
160 1859 bwasserm
                        while(!button1_click()/*replace with variable to keep track of if in intersection*/){
161 1869 bwasserm
//                                doDrive(255);
162 1844 bwasserm
                                packet = wl_basic_do_default(&dataLength);
163 1863 bwasserm
                                if(dataLength==PACKET_LENGTH){
164
                                        if(packet[2]==intersectionNum/*Intersection Num*/){
165
                                                if(packet[0]==WINTERSECTIONEXIT){
166
                                                        prevBot = 0;
167
                                                        orb2_set_color(RED);
168
                                                }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
169
                                                        sendBuffer[0] = WINTERSECTIONREPLY;
170
                                                        sendBuffer[2] = intersectionNum;//Intersection #
171
                                                        sendBuffer[3] = packet[1];
172
                                                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
173
                                                        nextBot = packet[1];
174
                                                        nextDir = packet[3];
175
                                                        nextPath = packet[4];
176
                                                        orb2_set_color(YELLOW);
177
                                                }
178
                                        }
179
                                        if(ISPING(packet)){
180
                                                sendBuffer[0] = WPINGREPLY;
181
                                                sendBuffer[2] = packet[1];
182
                                                if(packet[0]==WPINGQUEUE && packet[3]==nextBot){
183
                                                        nextBot = packet[1];
184
                                                }
185 1844 bwasserm
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
186
                                        }
187
                                }
188
                                if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time
189
                                        /*if(bots paths won't collide){
190
                                                sendBuffer[0] = WINTERSECTIONGO;
191
                                                sendBuffer[2] = 0;//Intersection #
192
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
193
                                        */
194
                                }
195
                        }
196
                        //Exits intersection
197
                        sendBuffer[0] = WINTERSECTIONEXIT;
198 1859 bwasserm
                        sendBuffer[2] = intersectionNum;//Intersection #
199 1844 bwasserm
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
200 1859 bwasserm
                        orb1_set_color(ORANGE);
201 1844 bwasserm
202 1843 bwasserm
                        if(!(sign & CINTERSECTION)){
203
                                if(sign & CHIGHWAYROAD){
204
                                        state = SHIGHWAY;
205
                                }else{
206
                                        state = SROAD;
207
                                }
208
                        }
209
                        break;
210
                case SHIGHWAY:/*On highway*/
211 1877 markwill
                        highwayFSM();
212 1843 bwasserm
                default:
213
                        usb_puts("I got stuck in an unknown state! My state is ");
214
                        usb_puti(state);
215
                }
216 967 alevkoy
        }
217
218 16 bcoltin
}