root / trunk / code / projects / hunter_prey / testbench / main.c @ 1461
History | View | Annotate | Download (7.27 KB)
1 |
/* testbench for Lab 2 first checkpoint
|
---|---|
2 |
* determine whether robot under test complies with communication standard
|
3 |
* to be conducted using XBee USB dongle
|
4 |
*/
|
5 |
|
6 |
/* The tests shall be as follows
|
7 |
* 1. Receive a TAG, send an ACK
|
8 |
* - Robot passes if it sends a tag and changes to prey state
|
9 |
* 2. Send a TAG, receive an ACK
|
10 |
* - Pass if sends ACK, changes to wait state, then hunter state
|
11 |
* 3. Receive TAG, send slightly delayed (< 1s) ACK
|
12 |
* - Pass if sends one and only one TAG packet (and changes state appropriately)
|
13 |
* 4. Receive TAG, do not send ACK
|
14 |
* - Pass if sends one and only one TAG packet (and does not change state)
|
15 |
* 5. Receive TAG, send ACK to incorrect robot
|
16 |
* - Pass if goes to wait state, then back to hunter state
|
17 |
* 6. Simulate TAG from robot A to B, ACK from B to A
|
18 |
* - Pass if ignores TAG, goes to wait then hunter in response to ACK
|
19 |
*/
|
20 |
|
21 |
#include <stdlib.h> |
22 |
#include <stdio.h> |
23 |
#include <unistd.h> |
24 |
#include "../../libwireless/lib/wl_basic.h" |
25 |
#include "../../libwireless/lib/wireless.h" |
26 |
#include "hunter_prey.h" |
27 |
#include <time.h> |
28 |
|
29 |
#define TYPE 42 // packet type for wireless communication |
30 |
#define ROBOTID 255 // make up a robot id because the PC doesn't have one |
31 |
|
32 |
|
33 |
void waitKey() {
|
34 |
unsigned char buf[10]; |
35 |
|
36 |
//read from stdin
|
37 |
while(read(0, buf, 10)==10) { |
38 |
printf("!");
|
39 |
fflush(stdout); |
40 |
} |
41 |
|
42 |
while(getchar()==-1); |
43 |
} |
44 |
|
45 |
int main(int argc, char *argv[]) |
46 |
{ |
47 |
char send_buffer[2]; // holds data to send |
48 |
int data_length; // length of data received |
49 |
unsigned char *packet_data; // data received |
50 |
int ret;
|
51 |
|
52 |
unsigned int channel = 0xF; |
53 |
|
54 |
struct timespec delay8, rem;
|
55 |
|
56 |
if(argc > 1) { |
57 |
channel = atoi(argv[1]);
|
58 |
} |
59 |
|
60 |
printf("using wireless channel %d\n", channel);
|
61 |
|
62 |
delay8.tv_sec = 2;
|
63 |
delay8.tv_nsec = 800000000;
|
64 |
|
65 |
wl_set_com_port("/dev/ttyUSB1");
|
66 |
// set up wireless
|
67 |
wl_basic_init_default(); |
68 |
wl_set_channel(channel); |
69 |
|
70 |
printf("Testing communications\n\n");
|
71 |
|
72 |
// Receive TAG, send ACK
|
73 |
printf("Receive TAG, send ACK... ");
|
74 |
fflush(stdout); |
75 |
// Wait until we receive a packet
|
76 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
77 |
|
78 |
printf("got packet, validating and sending ack...");
|
79 |
fflush(stdout); |
80 |
|
81 |
if (data_length > 2) |
82 |
{ |
83 |
printf("Excessive TAG packet length... ");
|
84 |
fflush(stdout); |
85 |
} |
86 |
|
87 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
88 |
{ |
89 |
// send back an ACK
|
90 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
91 |
send_buffer[1] = packet_data[1]; |
92 |
printf("sending ack intended for robot %d\n", packet_data[1]); |
93 |
fflush(stdout); |
94 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
95 |
|
96 |
printf("PASSED\n");
|
97 |
} |
98 |
else
|
99 |
printf("FAILED\n");
|
100 |
|
101 |
printf("robot should be prey\nPress enter to continue...\n");
|
102 |
waitKey(); |
103 |
|
104 |
// Send a TAG, receive an ACK
|
105 |
printf("Send TAG, wait for ACK... ");
|
106 |
fflush(stdout); |
107 |
|
108 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
109 |
send_buffer[1] = ROBOTID;
|
110 |
// robot number stays the same
|
111 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
112 |
|
113 |
// Wait for an ACK
|
114 |
ret = nanosleep(&delay8, &rem); // wait for 800 ms before sending ACK
|
115 |
|
116 |
while(ret) {
|
117 |
// printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
|
118 |
ret = nanosleep(&rem, &rem); |
119 |
} |
120 |
|
121 |
data_length = -1;
|
122 |
packet_data = wl_basic_do_default(&data_length); |
123 |
|
124 |
// Check ACK for presence and correctness
|
125 |
if (!packet_data || data_length < 2 || |
126 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
127 |
packet_data[1] != ROBOTID) {
|
128 |
printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
|
129 |
if(data_length >= 2) { |
130 |
printf("\tpacket = [%c, %d]\n",packet_data[0], packet_data[1]); |
131 |
} |
132 |
} |
133 |
else
|
134 |
printf("PASSED\n");
|
135 |
|
136 |
printf("Robot should be Hunter\nPress enter to continue...\n");
|
137 |
|
138 |
waitKey(); |
139 |
|
140 |
// Receive a TAG, send a delayed ACK
|
141 |
printf("Receive a TAG, send a delayed ACK... ");
|
142 |
fflush(stdout); |
143 |
|
144 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
145 |
|
146 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
147 |
{ |
148 |
printf("got TAG, waiting...");
|
149 |
fflush(stdout); |
150 |
// wait before sending ACK back
|
151 |
//usleep(900000);
|
152 |
delay8.tv_sec = 0;
|
153 |
ret = nanosleep(&delay8, &rem); // wait for 800 ms before sending ACK
|
154 |
|
155 |
while(ret) {
|
156 |
printf(".");
|
157 |
fflush(stdout);// printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
|
158 |
ret = nanosleep(&rem, &rem); |
159 |
} |
160 |
|
161 |
|
162 |
if (wl_basic_do_default(&data_length))
|
163 |
printf("FAILED, got another TAG too soon\n");
|
164 |
else
|
165 |
{ |
166 |
// send packet
|
167 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
168 |
send_buffer[1] = packet_data[1]; |
169 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
170 |
|
171 |
printf("PASSED\n");
|
172 |
} |
173 |
} |
174 |
|
175 |
else
|
176 |
printf("FAILED\n");
|
177 |
|
178 |
|
179 |
printf("robot should be prey.\n Press Enter...\n");
|
180 |
waitKey(); |
181 |
|
182 |
printf("sending courtesy tag...");
|
183 |
fflush(stdout); |
184 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
185 |
send_buffer[1] = ROBOTID;
|
186 |
// robot number stays the same
|
187 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
188 |
printf("done.\nwaiting for ack...");
|
189 |
fflush(stdout); |
190 |
|
191 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
192 |
|
193 |
if (!packet_data || data_length < 2 || |
194 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
195 |
packet_data[1] != ROBOTID)
|
196 |
printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
|
197 |
else
|
198 |
printf("done\n");
|
199 |
|
200 |
|
201 |
// Receive a TAG, never send an ACK
|
202 |
printf("Receive TAG, never send ACK... ");
|
203 |
fflush(stdout); |
204 |
|
205 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
206 |
|
207 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
208 |
{ |
209 |
|
210 |
printf("got TAG, monitoring for 5 seconds\n");
|
211 |
fflush(stdout); |
212 |
delay8.tv_sec = 5;
|
213 |
delay8.tv_nsec = 0;
|
214 |
|
215 |
ret = nanosleep(&delay8, &rem); // wait for 5 secs
|
216 |
while(ret) {
|
217 |
// printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
|
218 |
ret = nanosleep(&rem, &rem); |
219 |
} |
220 |
|
221 |
if (wl_basic_do_default(&data_length))
|
222 |
printf("FAILED, robot is spamming\n");
|
223 |
else
|
224 |
printf("PASSED\n");
|
225 |
} |
226 |
|
227 |
else
|
228 |
printf("FAILED\n");
|
229 |
|
230 |
printf("robot should be hunter.\nPress enter...\n");
|
231 |
|
232 |
waitKey(); |
233 |
|
234 |
// Receive TAG, send ACK to incorrect robot
|
235 |
printf("Receive TAG, send ACK to incorrect robot... ");
|
236 |
fflush(stdout); |
237 |
|
238 |
// Wait until we receive a packet
|
239 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
240 |
|
241 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
242 |
{ |
243 |
// send back an ACK
|
244 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
245 |
send_buffer[1] = packet_data[1] + 1; |
246 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
247 |
|
248 |
printf("PASSED\n");
|
249 |
} |
250 |
else
|
251 |
printf("FAILED\n");
|
252 |
|
253 |
|
254 |
printf("robot should wait and then still be hunter.\nPress Enter...\n");
|
255 |
waitKey(); |
256 |
|
257 |
// Simulate TAG from robot A to B, ACK from robot B to A
|
258 |
printf("Send TAG from robot A to B, ACK from B to A... ");
|
259 |
fflush(stdout); |
260 |
|
261 |
// TAG
|
262 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
263 |
send_buffer[1] = packet_data[1] - 1; // robot other than testee |
264 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
265 |
|
266 |
// ACK
|
267 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
268 |
// send_buffer[1] stays the same
|
269 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
270 |
|
271 |
if (wl_basic_do_default(&data_length))
|
272 |
printf("FAILED\n");
|
273 |
else
|
274 |
printf("PASSED\n");
|
275 |
|
276 |
printf("robot should be waiting\n");
|
277 |
|
278 |
return 0; |
279 |
} |
280 |
|