Revision 1408
behavior without BOM seems to be working
main.c | ||
---|---|---|
1 | 1 |
#include <dragonfly_lib.h> |
2 | 2 |
#include <wireless.h> |
3 |
#include <wl_basic.h> |
|
4 |
#include "smart_run_around_fsm.h" |
|
3 | 5 |
|
4 | 6 |
#define CHAN 0xF |
5 |
#define TAG_TIME 3000
|
|
7 |
#define TAG_TIME 10
|
|
6 | 8 |
#define TAG_RANGE 150 |
9 |
#define TAG_PAUSE 2000 |
|
7 | 10 |
|
11 |
#define ROBOT_HUNTER 0 |
|
12 |
#define ROBOT_PREY 1 |
|
13 |
#define ROBOT_TAGGED 2 |
|
14 |
|
|
15 |
void packet_receive(char type, int source, unsigned char* packet, int length); |
|
16 |
|
|
17 |
volatile uint8_t robotState = ROBOT_HUNTER; |
|
18 |
volatile uint8_t color; |
|
19 |
volatile uint8_t tagAck; |
|
20 |
volatile uint8_t tagger; |
|
21 |
uint8_t id; |
|
22 |
|
|
8 | 23 |
int main(void) { |
9 | 24 |
|
10 | 25 |
int frontRange; |
11 | 26 |
uint8_t angle; |
12 | 27 |
uint8_t i; |
13 |
uint8_t isPrey = 0; |
|
14 | 28 |
int onTarget = 0; |
15 |
uint8_t color;
|
|
29 |
char packet[2];
|
|
16 | 30 |
|
17 | 31 |
//intialize the robot and wireless |
18 | 32 |
dragonfly_init(ALL_ON); |
19 |
wl_init(); |
|
20 | 33 |
|
21 |
analog_init(1); |
|
34 |
id = get_robotid(); |
|
35 |
packet[1] = id; |
|
36 |
usb_puts("hello, I am robot \n"); |
|
37 |
usb_puti(id); |
|
38 |
usb_putc('\n'); |
|
22 | 39 |
|
23 |
bom_init(BOM15);
|
|
40 |
usb_puts("wireless.\n");
|
|
24 | 41 |
|
42 |
wl_basic_init(&packet_receive); |
|
43 |
|
|
44 |
usb_puts("run around.\n"); |
|
45 |
|
|
46 |
run_around_init(); |
|
47 |
|
|
48 |
// bom_init(BOM15); |
|
49 |
|
|
50 |
usb_puts("channel\n"); |
|
51 |
|
|
25 | 52 |
//use a specific channel to avoid interfering with other tasks |
26 | 53 |
wl_set_channel(CHAN); |
27 | 54 |
|
55 |
usb_puts("orb set\n"); |
|
56 |
|
|
28 | 57 |
orb_set_color(BLUE); |
29 | 58 |
color = BLUE; |
30 | 59 |
|
60 |
//if button 2 is held, you are prey |
|
61 |
if(button2_read()) { |
|
62 |
robotState = ROBOT_PREY; |
|
63 |
} |
|
64 |
|
|
65 |
|
|
31 | 66 |
while(1) { |
32 | 67 |
|
33 |
bom_refresh(BOM_ALL); |
|
68 |
if(robotState==ROBOT_TAGGED) { |
|
69 |
usb_puts("tagged, waiting to send ACK\n"); |
|
70 |
move(0,0); |
|
71 |
delay_ms(TAG_PAUSE); |
|
72 |
usb_puts("sending ACK to "); |
|
73 |
usb_puti(tagger); |
|
74 |
usb_putc('\n'); |
|
75 |
packet[0] = 'A'; |
|
76 |
packet[1] = tagger; |
|
77 |
wl_basic_send_global_packet (0, packet, 2); |
|
78 |
usb_puts("sent\n"); |
|
79 |
move(0,0); |
|
80 |
robotState = ROBOT_HUNTER; //no longer prey |
|
81 |
} |
|
34 | 82 |
|
35 |
frontRange = range_read_distance (IR2); |
|
83 |
else if(robotState == ROBOT_PREY) { |
|
84 |
run_around_FSM(); |
|
85 |
} |
|
36 | 86 |
|
37 |
usb_puti(frontRange); |
|
38 |
usb_putc('\n'); |
|
87 |
else { //ROBOT_HUNTER |
|
39 | 88 |
|
40 |
//BUG: when get_max_bom is called, the rangefinder seems to stop |
|
41 |
//working |
|
42 |
angle = get_max_bom(); |
|
43 |
angle = 4; |
|
89 |
// bom_refresh(BOM_ALL); |
|
44 | 90 |
|
45 |
if(angle == 4 && frontRange > 0 && frontRange < TAG_RANGE) { |
|
46 |
if(onTarget == 0) { |
|
47 |
onTarget = TAG_TIME; |
|
48 |
if(color != GREEN) { |
|
49 |
color = GREEN; |
|
50 |
orb_set_color(GREEN); |
|
91 |
frontRange = range_read_distance (IR2); |
|
92 |
|
|
93 |
/* usb_puti(frontRange); */ |
|
94 |
/* usb_putc('\n'); */ |
|
95 |
|
|
96 |
//BUG: when get_max_bom is called, the rangefinder seems to stop |
|
97 |
//working |
|
98 |
//angle = get_max_bom(); |
|
99 |
angle = 4; |
|
100 |
|
|
101 |
if(angle == 4 && frontRange > 0 && frontRange < TAG_RANGE) { |
|
102 |
if(onTarget == 0) { |
|
103 |
onTarget = TAG_TIME; |
|
104 |
if(color != GREEN) { |
|
105 |
color = GREEN; |
|
106 |
orb_set_color(GREEN); |
|
107 |
} |
|
108 |
usb_puts("On target!\n"); |
|
51 | 109 |
} |
52 |
usb_puts("On target!\n"); |
|
110 |
else { |
|
111 |
if(--onTarget <= 0) { |
|
112 |
orb_set_color(YELLOW); |
|
113 |
color = YELLOW; |
|
114 |
usb_puts("TAG!\n"); |
|
115 |
tagAck = 0; |
|
116 |
packet[0] = 'T'; |
|
117 |
packet[1] = id; |
|
118 |
wl_basic_send_global_packet (0, packet, 2); |
|
119 |
move(0,0); |
|
120 |
while(!tagAck) |
|
121 |
wl_do(); |
|
122 |
|
|
123 |
if(tagAck == id) { //if the ack was for us |
|
124 |
usb_puts("ACK!\n"); |
|
125 |
// delay_ms(TAG_PAUSE); |
|
126 |
robotState = ROBOT_PREY; |
|
127 |
} |
|
128 |
else { |
|
129 |
usb_puts("ACK failed!\n"); |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
53 | 133 |
} |
54 |
else { |
|
55 |
if(--onTarget <= 0) { |
|
56 |
orb_set_color(RED); |
|
57 |
color = RED; |
|
58 |
usb_puts("TAG!\n"); |
|
59 |
while(1); |
|
134 |
else{ |
|
135 |
if(color != BLUE) { |
|
136 |
color = BLUE; |
|
137 |
orb_set_color(BLUE); |
|
60 | 138 |
} |
139 |
|
|
140 |
//don't reset onTarget because the robot got too close |
|
141 |
if(frontRange > 0) |
|
142 |
onTarget = 0; |
|
61 | 143 |
} |
62 |
} |
|
63 |
else{ |
|
64 |
if(color != BLUE) { |
|
65 |
color = BLUE; |
|
66 |
orb_set_color(BLUE); |
|
67 |
} |
|
68 | 144 |
|
69 |
//don't reset onTarget because the robot got too close |
|
70 |
if(frontRange > 0) |
|
71 |
onTarget = 0; |
|
72 | 145 |
} |
73 | 146 |
|
74 |
|
|
75 | 147 |
wl_do(); |
76 | 148 |
} |
77 | 149 |
|
78 | 150 |
return 0; |
79 | 151 |
} |
152 |
|
|
153 |
void packet_receive(char type, int source, unsigned char* packet, int length) |
|
154 |
{ |
|
155 |
if(type==0 && length>=2){ |
|
156 |
if(packet[0] == 'T') { |
|
157 |
if(robotState == ROBOT_PREY) { |
|
158 |
robotState = ROBOT_TAGGED; |
|
159 |
color = BLUE; |
|
160 |
orb_set_color(BLUE); |
|
161 |
tagger = packet[1]; |
|
162 |
} |
|
163 |
else { |
|
164 |
usb_puts("tagged, but I'm not it!\n"); |
|
165 |
} |
|
166 |
} |
|
167 |
else if(packet[0] == 'A' && robotState == ROBOT_HUNTER) { |
|
168 |
tagAck = packet[1]; |
|
169 |
} |
|
170 |
else { |
|
171 |
usb_puts("got '"); |
|
172 |
usb_putc(packet[0]); |
|
173 |
usb_puti(packet[1]); |
|
174 |
usb_puts("'\n"); |
|
175 |
} |
|
176 |
} |
|
177 |
else { |
|
178 |
usb_puts("got unkown packet! type="); |
|
179 |
usb_puti(type); |
|
180 |
usb_puts(" length="); |
|
181 |
usb_puti(length); |
|
182 |
usb_putc('\n'); |
|
183 |
} |
|
184 |
} |
|
185 |
|
Also available in: Unified diff