Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.62 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

    
8
#include <dragonfly_lib.h>
9
#include <wl_basic.h>
10
#include "lineFollow.h"
11

    
12

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

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

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

    
56
int main (void) {
57

    
58
        int state, sign, dataLength;
59
        char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, *packet, nextDir, nextPath;
60

    
61
        /* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */
62
        dragonfly_init(ALL_ON);
63
        xbee_init();
64
        encoders_init();
65
        lineFollow_init();
66
        rtc_init(SIXTEENTH_SECOND, NULL);        
67

    
68
        id = get_robotid();
69
        sign = 0;
70
        
71
        sendBuffer[1] = id;
72

    
73
        while (1) {
74
                /*DTM Finite State Machine*/
75
                switch(state){
76
                case SROAD:/*Following a normal road*/
77
                        sign = lineFollow();
78
                        //other road behaviors
79
                                //tailgating?
80
                        //read barcode
81
                        if(sign & CINTERSECTION){
82
                                state = SINTERSECTION;
83
                        }
84
                        break;
85
                case SINTERSECTION:/*Entering, and in intersection*/
86
                        //Intersection behaviors
87
                                //queueing
88
                                //no-stop?
89
                        //check wireless
90
                        
91
                        /*Intersection queue:
92
                         *Each robot when entering the intersection will check for other robots
93
                         *in the intersection, and insert itself in a queue to go through.
94
                         */
95
                        prevBot = 0;
96
                        nextBot = 0;
97
                        //Sends packet announcing its entry to the intersection
98
                        sendBuffer[0] = WINTERSECTIONENTRY;
99
                        sendBuffer[2] = 0;//Intersection #
100
                        sendBuffer[3] = 0;//From Direction
101
                        sendBuffer[4] = 0;//To Direction
102
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
103
                        rtc_reset();
104
                        while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue
105
                                packet = wl_basic_do_default(&dataLength);
106
                                if(dataLength==PACKET_LENGTH && packet[0]==WINTERSECTIONREPLY && packet[2]==0/*Intersection Num*/){
107
                                        if(packet[3]==id){//Reply for me
108
                                                prevBot = packet[1];
109
                                                break;
110
                                        }else{//Someone else got here first, try again
111
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
112
                                                rtc_reset();
113
                                        }
114
                                }
115
                        }
116
                        //waits for its turn
117
                        while(prevBot != 0){
118
                                packet = wl_basic_do_default(&dataLength);                
119
                                if(dataLength==PACKET_LENGTH && packet[2]==0/*Intersection Num*/){
120
                                        if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){
121
                                                prevBot = 0;
122
                                        }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
123
                                                sendBuffer[0] = WINTERSECTIONREPLY;
124
                                                sendBuffer[2] = 0;//Intersection #
125
                                                sendBuffer[3] = packet[1];
126
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
127
                                                nextBot = packet[1];
128
                                                nextDir = packet[3];
129
                                                nextPath = packet[4];
130
                                        }
131
                                }
132
                        }
133
                        //Drives through intersection
134
                        //Code to choose path through intersection goes in this while loop
135
                        //But here's the code to handle wireless while in the intersection...
136
                        while(1/*replace with variable to keep track of if in intersection*/){
137
                                packet = wl_basic_do_default(&dataLength);                
138
                                if(dataLength==PACKET_LENGTH && packet[2]==0/*Intersection Num*/){
139
                                        if(packet[0]==WINTERSECTIONEXIT){
140
                                                prevBot = 0;
141
                                        }else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){
142
                                                sendBuffer[0] = WINTERSECTIONREPLY;
143
                                                sendBuffer[2] = 0;//Intersection #
144
                                                sendBuffer[3] = packet[1];
145
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
146
                                                nextBot = packet[1];
147
                                                nextDir = packet[3];
148
                                                nextPath = packet[4];
149
                                        }
150
                                }
151
                                if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time
152
                                        /*if(bots paths won't collide){
153
                                                sendBuffer[0] = WINTERSECTIONGO;
154
                                                sendBuffer[2] = 0;//Intersection #
155
                                                wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
156
                                        */
157
                                }
158
                        }
159
                        //Exits intersection
160
                        sendBuffer[0] = WINTERSECTIONEXIT;
161
                        sendBuffer[2] = 0;//Intersection #
162
                        wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH);
163
                        
164
                        if(!(sign & CINTERSECTION)){
165
                                if(sign & CHIGHWAYROAD){
166
                                        state = SHIGHWAY;
167
                                }else{
168
                                        state = SROAD;
169
                                }
170
                        }
171
                        break;
172
                case SHIGHWAY:/*On highway*/
173
                        sign = lineFollow();
174
                        //highway behaviors
175
                                //merging
176
                                //passing
177
                        //read barcode
178
                        break;
179
                default:
180
                        usb_puts("I got stuck in an unknown state! My state is ");
181
                        usb_puti(state);
182
                }
183
        }
184

    
185
}