root / trunk / code / projects / hunter_prey / testbench / main.c @ 1438
History | View | Annotate | Download (4.67 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 <time.h> |
24 |
#include <wl_basic.h> |
25 |
#include <wireless.h> |
26 |
#include "../../libwireless/lib/wireless.h" |
27 |
#include "hunter_prey.h" |
28 |
|
29 |
#define CHANNEL 0xF // channel for wireless communication |
30 |
#define TYPE 42 // packet type for wireless communication |
31 |
#define ROBOTID 255 // make up a robot id because the PC doesn't have one |
32 |
|
33 |
int main(int argc, char *argv[]) |
34 |
{ |
35 |
char send_buffer[2]; // holds data to send |
36 |
int data_length; // length of data received |
37 |
unsigned char *packet_data; // data received |
38 |
|
39 |
// set up wireless
|
40 |
wl_basic_init_default(); |
41 |
wl_set_channel(CHANNEL); |
42 |
wl_set_com_port("/dev/ttyUSB0");
|
43 |
|
44 |
printf("Testing communications\n\n");
|
45 |
|
46 |
// Receive TAG, send ACK
|
47 |
printf("Receive TAG, send ACK... ");
|
48 |
// Wait until we receive a packet
|
49 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
50 |
|
51 |
if (data_length > 2) |
52 |
printf("Excessive TAG packet length... ");
|
53 |
|
54 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
55 |
{ |
56 |
// send back an ACK
|
57 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
58 |
send_buffer[1] = packet_data[1]; |
59 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
60 |
|
61 |
printf("PASSED\n");
|
62 |
} |
63 |
else
|
64 |
printf("FAILED\n");
|
65 |
|
66 |
// Send a TAG, receive an ACK
|
67 |
printf("Send TAG, wait for ACK... ");
|
68 |
|
69 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
70 |
send_buffer[1] = ROBOTID;
|
71 |
// robot number stays the same
|
72 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
73 |
|
74 |
// Wait for an ACK
|
75 |
delay_ms(800); // wait for 800 ms before sending ACK |
76 |
packet_data = wl_basic_do_default(&data_length); |
77 |
|
78 |
// Check ACK for presence and correctness
|
79 |
if (!packet_data || data_length < 2 || |
80 |
packet_data[0] != HUNTER_PREY_ACTION_ACK ||
|
81 |
packet_data[1] != ROBOTID)
|
82 |
printf("FAILED\n");
|
83 |
else
|
84 |
printf("PASSED\n");
|
85 |
|
86 |
// Receive a TAG, send a delayed ACK
|
87 |
printf("Receive a TAG, send a delayed ACK... ");
|
88 |
|
89 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
90 |
|
91 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
92 |
{ |
93 |
// wait before sending ACK back
|
94 |
delay_ms(900);
|
95 |
|
96 |
if (wl_basic_do_default(&data_length))
|
97 |
printf("FAILED\n");
|
98 |
else
|
99 |
{ |
100 |
// send packet
|
101 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
102 |
send_buffer[1] = packet_data[1]; |
103 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
104 |
|
105 |
printf("PASSED\n");
|
106 |
} |
107 |
} |
108 |
|
109 |
else
|
110 |
printf("FAILED\n");
|
111 |
|
112 |
// Receive a TAG, never send an ACK
|
113 |
printf("Receive TAG, never send ACK... ");
|
114 |
|
115 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
116 |
|
117 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
118 |
{ |
119 |
delay_ms(5000); // wait 5 seconds to see if they TAG again |
120 |
|
121 |
if (wl_basic_do_default(&data_length))
|
122 |
printf("FAILED\n");
|
123 |
else
|
124 |
printf("PASSED\n");
|
125 |
} |
126 |
|
127 |
else
|
128 |
printf("FAILED\n");
|
129 |
|
130 |
// Receive TAG, send ACK to incorrect robot
|
131 |
printf("Receive TAG, send ACK to incorrect robot... ");
|
132 |
|
133 |
// Wait until we receive a packet
|
134 |
while (!(packet_data = wl_basic_do_default(&data_length)));
|
135 |
|
136 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
137 |
{ |
138 |
// send back an ACK
|
139 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
140 |
send_buffer[1] = packet_data[1] + 1; |
141 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
142 |
|
143 |
printf("PASSED\n");
|
144 |
} |
145 |
else
|
146 |
printf("FAILED\n");
|
147 |
|
148 |
// Simulate TAG from robot A to B, ACK from robot B to A
|
149 |
printf("Send TAG from robot A to B, ACK from B to A... ");
|
150 |
|
151 |
// TAG
|
152 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG;
|
153 |
send_buffer[1] = packet_data[1] - 1; // robot other than testee |
154 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
155 |
|
156 |
// ACK
|
157 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
158 |
// send_buffer[1] stays the same
|
159 |
wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
160 |
|
161 |
if (wl_basic_do_default(&data_length))
|
162 |
printf("FAILED\n");
|
163 |
else
|
164 |
printf("PASSED\n");
|
165 |
|
166 |
return 0; |
167 |
} |
168 |
|