Revision 1859
Added code to make it demo that the queue works. Mostly orbs and buttons, but some variables changed. Also started work on wireless reliability.
trunk/code/projects/traffic_navigation/main.c | ||
---|---|---|
49 | 49 |
#define WINTERSECTIONREPLY 11 //[type, fromBot, intersection, toBot] |
50 | 50 |
#define WINTERSECTIONEXIT 12 //[type, bot, intersection] |
51 | 51 |
#define WINTERSECTIONGO 13 //[type, bot, intersection] |
52 |
#define WINTERSECTIONPOLICEENTRY 14 |
|
52 | 53 |
#define WHIGHWAYENTRY 20 //[type, bot, highway] |
53 | 54 |
#define WHIGHWAYREPLY 21 //[type, fromBot, highway, toBot] |
54 | 55 |
#define WHIGHWAYEXIT 22 //[type, bot, highway] |
56 |
#define WPINGGLOBAL 30 //[type, bot] |
|
57 |
#define WPINGBOT 31 //[type, fromBot, toBot] |
|
58 |
#define WPINGQUEUE 32 //[type, bot, queue] |
|
59 |
#define WPINGREPLY 33 //[type, fromBot, toBot] |
|
55 | 60 |
|
56 | 61 |
int main (void) { |
57 | 62 |
|
58 | 63 |
int state, sign, dataLength; |
59 |
char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, *packet, nextDir, nextPath; |
|
64 |
char sendBuffer[PACKET_LENGTH], prevBot, nextBot, id, *packet, nextDir, nextPath, intersectionNum;
|
|
60 | 65 |
|
61 | 66 |
/* Initialize the dragonfly boards, the xbee, encoders, lineFollowing */ |
62 | 67 |
dragonfly_init(ALL_ON); |
... | ... | |
64 | 69 |
encoders_init(); |
65 | 70 |
lineFollow_init(); |
66 | 71 |
rtc_init(SIXTEENTH_SECOND, NULL); |
67 |
|
|
72 |
wl_basic_init_default(); |
|
73 |
wl_set_channel(13); |
|
74 |
|
|
68 | 75 |
id = get_robotid(); |
69 | 76 |
sign = 0; |
70 | 77 |
|
78 |
//Test code |
|
79 |
state = SROAD; |
|
80 |
|
|
71 | 81 |
sendBuffer[1] = id; |
72 | 82 |
|
73 | 83 |
while (1) { |
74 | 84 |
/*DTM Finite State Machine*/ |
75 | 85 |
switch(state){ |
76 | 86 |
case SROAD:/*Following a normal road*/ |
77 |
sign = lineFollow(); |
|
87 |
// sign = lineFollow();
|
|
78 | 88 |
//other road behaviors |
79 | 89 |
//tailgating? |
80 | 90 |
//read barcode |
81 |
if(sign & CINTERSECTION){
|
|
91 |
if((sign & CINTERSECTION) || button1_click()){
|
|
82 | 92 |
state = SINTERSECTION; |
83 | 93 |
} |
84 | 94 |
break; |
... | ... | |
88 | 98 |
//no-stop? |
89 | 99 |
//check wireless |
90 | 100 |
|
101 |
intersectionNum = 0; |
|
102 |
|
|
91 | 103 |
/*Intersection queue: |
92 | 104 |
*Each robot when entering the intersection will check for other robots |
93 | 105 |
*in the intersection, and insert itself in a queue to go through. |
... | ... | |
96 | 108 |
nextBot = 0; |
97 | 109 |
//Sends packet announcing its entry to the intersection |
98 | 110 |
sendBuffer[0] = WINTERSECTIONENTRY; |
99 |
sendBuffer[2] = 0;//Intersection #
|
|
111 |
sendBuffer[2] = intersectionNum;//Intersection #
|
|
100 | 112 |
sendBuffer[3] = 0;//From Direction |
101 |
sendBuffer[4] = 0;//To Direction
|
|
113 |
sendBuffer[4] = 2;//To Direction
|
|
102 | 114 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH); |
115 |
orb1_set_color(BLUE); |
|
103 | 116 |
rtc_reset(); |
104 | 117 |
while(rtc_get() < 8){//waits for a reply, otherwise assumes it is first in queue |
105 | 118 |
packet = wl_basic_do_default(&dataLength); |
106 |
if(dataLength==PACKET_LENGTH && packet[0]==WINTERSECTIONREPLY && packet[2]==0/*Intersection Num*/){
|
|
119 |
if(dataLength==PACKET_LENGTH && packet[0]==WINTERSECTIONREPLY && packet[2]==intersectionNum/*Intersection Num*/){
|
|
107 | 120 |
if(packet[3]==id){//Reply for me |
108 | 121 |
prevBot = packet[1]; |
122 |
orb2_set_color(GREEN); |
|
109 | 123 |
break; |
110 | 124 |
}else{//Someone else got here first, try again |
111 | 125 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH); |
... | ... | |
113 | 127 |
} |
114 | 128 |
} |
115 | 129 |
} |
130 |
orb1_set_color(PURPLE); |
|
116 | 131 |
//waits for its turn |
117 | 132 |
while(prevBot != 0){ |
118 | 133 |
packet = wl_basic_do_default(&dataLength); |
119 |
if(dataLength==PACKET_LENGTH && packet[2]==0/*Intersection Num*/){
|
|
134 |
if(dataLength==PACKET_LENGTH && packet[2]==intersectionNum/*Intersection Num*/){
|
|
120 | 135 |
if(packet[1]==prevBot && (packet[0]==WINTERSECTIONGO || packet[0]==WINTERSECTIONEXIT)){ |
121 | 136 |
prevBot = 0; |
137 |
orb2_set_color(PURPLE); |
|
122 | 138 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){ |
123 | 139 |
sendBuffer[0] = WINTERSECTIONREPLY; |
124 |
sendBuffer[2] = 0;//Intersection #
|
|
140 |
sendBuffer[2] = intersectionNum;//Intersection #
|
|
125 | 141 |
sendBuffer[3] = packet[1]; |
126 | 142 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH); |
127 | 143 |
nextBot = packet[1]; |
128 | 144 |
nextDir = packet[3]; |
129 | 145 |
nextPath = packet[4]; |
146 |
orb2_set_color(BLUE); |
|
130 | 147 |
} |
131 | 148 |
} |
132 | 149 |
} |
150 |
orb1_set_color(RED); |
|
133 | 151 |
//Drives through intersection |
134 | 152 |
//Code to choose path through intersection goes in this while loop |
135 | 153 |
//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*/){
|
|
154 |
while(!button1_click()/*replace with variable to keep track of if in intersection*/){
|
|
137 | 155 |
packet = wl_basic_do_default(&dataLength); |
138 |
if(dataLength==PACKET_LENGTH && packet[2]==0/*Intersection Num*/){
|
|
156 |
if(dataLength==PACKET_LENGTH && packet[2]==intersectionNum/*Intersection Num*/){
|
|
139 | 157 |
if(packet[0]==WINTERSECTIONEXIT){ |
140 | 158 |
prevBot = 0; |
159 |
orb2_set_color(RED); |
|
141 | 160 |
}else if(packet[0]==WINTERSECTIONENTRY && nextBot==0){ |
142 | 161 |
sendBuffer[0] = WINTERSECTIONREPLY; |
143 |
sendBuffer[2] = 0;//Intersection #
|
|
162 |
sendBuffer[2] = intersectionNum;//Intersection #
|
|
144 | 163 |
sendBuffer[3] = packet[1]; |
145 | 164 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH); |
146 | 165 |
nextBot = packet[1]; |
147 | 166 |
nextDir = packet[3]; |
148 | 167 |
nextPath = packet[4]; |
168 |
orb2_set_color(YELLOW); |
|
149 | 169 |
} |
150 | 170 |
} |
151 | 171 |
if(prevBot==0 && nextBot!=0){//let 2 bots go through at same time |
... | ... | |
158 | 178 |
} |
159 | 179 |
//Exits intersection |
160 | 180 |
sendBuffer[0] = WINTERSECTIONEXIT; |
161 |
sendBuffer[2] = 0;//Intersection #
|
|
181 |
sendBuffer[2] = intersectionNum;//Intersection #
|
|
162 | 182 |
wl_basic_send_global_packet(42, sendBuffer, PACKET_LENGTH); |
183 |
orb1_set_color(ORANGE); |
|
163 | 184 |
|
164 | 185 |
if(!(sign & CINTERSECTION)){ |
165 | 186 |
if(sign & CHIGHWAYROAD){ |
... | ... | |
170 | 191 |
} |
171 | 192 |
break; |
172 | 193 |
case SHIGHWAY:/*On highway*/ |
173 |
sign = lineFollow(); |
|
194 |
// sign = lineFollow();
|
|
174 | 195 |
//highway behaviors |
175 | 196 |
//merging |
176 | 197 |
//passing |
Also available in: Unified diff