Revision 1411
Hunter-prey works! its a decent proof of concept but could use improvement
using robots Edgar (3), 7, 5
main.c | ||
---|---|---|
4 | 4 |
#include "smart_run_around_fsm.h" |
5 | 5 |
|
6 | 6 |
#define CHAN 0xF |
7 |
#define TAG_TIME 5
|
|
8 |
#define TAG_RANGE 150
|
|
7 |
#define TAG_TIME 2
|
|
8 |
#define TAG_RANGE 200
|
|
9 | 9 |
#define TAG_PAUSE 2000 |
10 | 10 |
|
11 | 11 |
#define ROBOT_HUNTER 0 |
... | ... | |
15 | 15 |
void packet_receive(char type, int source, unsigned char* packet, int length); |
16 | 16 |
|
17 | 17 |
volatile uint8_t robotState = ROBOT_HUNTER; |
18 |
volatile uint8_t color; |
|
18 |
volatile uint8_t color1, color2;
|
|
19 | 19 |
volatile uint8_t tagAck; |
20 | 20 |
volatile uint8_t tagger; |
21 | 21 |
uint8_t id; |
... | ... | |
106 | 106 |
usb_puts("orb set\n"); |
107 | 107 |
|
108 | 108 |
orb_set_color(BLUE); |
109 |
color = BLUE; |
|
109 |
color1 = BLUE; |
|
110 |
color2 = BLUE; |
|
110 | 111 |
|
111 | 112 |
//if button 2 is held, you are prey |
112 | 113 |
if(button2_read()) { |
... | ... | |
119 | 120 |
|
120 | 121 |
if(robotState==ROBOT_TAGGED) { |
121 | 122 |
usb_puts("tagged, waiting to send ACK\n"); |
123 |
delay_ms(TAG_PAUSE); |
|
122 | 124 |
move(0,0); |
123 |
delay_ms(TAG_PAUSE); |
|
124 | 125 |
usb_puts("sending ACK to "); |
125 | 126 |
usb_puti(tagger); |
126 | 127 |
usb_putc('\n'); |
... | ... | |
128 | 129 |
packet[1] = tagger; |
129 | 130 |
wl_basic_send_global_packet (0, packet, 2); |
130 | 131 |
usb_puts("sent\n"); |
131 |
move(0,0); |
|
132 | 132 |
robotState = ROBOT_HUNTER; //no longer prey |
133 | 133 |
} |
134 | 134 |
|
... | ... | |
140 | 140 |
|
141 | 141 |
bom_refresh(BOM_ALL); |
142 | 142 |
|
143 |
delay_ms(100); |
|
144 |
|
|
143 | 145 |
frontRange = range_read_distance (IR2); |
144 | 146 |
|
145 | 147 |
/* usb_puti(frontRange); */ |
... | ... | |
153 | 155 |
|
154 | 156 |
chase(angle); |
155 | 157 |
|
158 |
if(angle < 7 && angle > 1) { |
|
159 |
if(color1 != RED) { |
|
160 |
color1 = RED; |
|
161 |
orb1_set_color(RED); |
|
162 |
} |
|
163 |
} |
|
164 |
else { |
|
165 |
if(color1 != BLUE) { |
|
166 |
color1 = BLUE; |
|
167 |
orb1_set_color(BLUE); |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
usb_puti(frontRange); |
|
173 |
usb_putc('\n'); |
|
174 |
|
|
175 |
if(frontRange > 0 && frontRange < TAG_RANGE) { |
|
176 |
if(color2 != RED) { |
|
177 |
color2 = RED; |
|
178 |
orb2_set_color(RED); |
|
179 |
} |
|
180 |
} |
|
181 |
else { |
|
182 |
if(color2 != BLUE) { |
|
183 |
color2 = BLUE; |
|
184 |
orb2_set_color(BLUE); |
|
185 |
} |
|
186 |
} |
|
187 |
|
|
188 |
|
|
189 |
|
|
156 | 190 |
if(angle < 7 && angle > 1 && frontRange > 0 && frontRange < TAG_RANGE) { |
157 | 191 |
if(onTarget == 0) { |
158 | 192 |
onTarget = TAG_TIME; |
159 |
if(color != GREEN) { |
|
160 |
color = GREEN; |
|
193 |
if(color1 != GREEN) { |
|
194 |
color1 = GREEN; |
|
195 |
color2 = GREEN; |
|
161 | 196 |
orb_set_color(GREEN); |
162 | 197 |
} |
163 | 198 |
usb_puts("On target!\n"); |
... | ... | |
165 | 200 |
else { |
166 | 201 |
if(--onTarget <= 0) { |
167 | 202 |
orb_set_color(YELLOW); |
168 |
color = YELLOW; |
|
203 |
color1 = YELLOW; |
|
204 |
color2 = YELLOW; |
|
169 | 205 |
usb_puts("TAG!\n"); |
170 | 206 |
tagAck = 0; |
171 | 207 |
packet[0] = 'T'; |
... | ... | |
177 | 213 |
|
178 | 214 |
if(tagAck == id) { //if the ack was for us |
179 | 215 |
usb_puts("ACK!\n"); |
180 |
// delay_ms(TAG_PAUSE); |
|
216 |
|
|
217 |
move(-230,0); //back up to give the new prey some room |
|
218 |
delay_ms(TAG_PAUSE/2); |
|
219 |
move(0,0); |
|
220 |
|
|
181 | 221 |
robotState = ROBOT_PREY; |
182 | 222 |
bom_on(); |
183 | 223 |
} |
... | ... | |
188 | 228 |
} |
189 | 229 |
} |
190 | 230 |
else{ |
191 |
if(color != BLUE) { |
|
192 |
color = BLUE; |
|
193 |
orb_set_color(BLUE); |
|
194 |
} |
|
195 |
|
|
196 | 231 |
//don't reset onTarget because the robot got too close |
197 | 232 |
if(frontRange > 0) |
198 | 233 |
onTarget = 0; |
... | ... | |
213 | 248 |
if(robotState == ROBOT_PREY) { |
214 | 249 |
robotState = ROBOT_TAGGED; |
215 | 250 |
bom_off(); |
216 |
color = BLUE; |
|
251 |
color1 = BLUE; |
|
252 |
color2 = BLUE; |
|
217 | 253 |
orb_set_color(BLUE); |
218 | 254 |
tagger = packet[1]; |
219 | 255 |
} |
Also available in: Unified diff