root / trunk / code / projects / hunter_prey / testbench / main.c @ 1438
History | View | Annotate | Download (4.67 KB)
1 | 1434 | alevkoy | /* 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 | 1433 | alevkoy | |
6 | 1434 | alevkoy | /* The tests shall be as follows
|
7 | 1438 | alevkoy | * 1. Receive a TAG, send an ACK
|
8 | 1434 | alevkoy | * - Robot passes if it sends a tag and changes to prey state
|
9 | 1438 | alevkoy | * 2. Send a TAG, receive an ACK
|
10 | 1434 | alevkoy | * - Pass if sends ACK, changes to wait state, then hunter state
|
11 | 1438 | alevkoy | * 3. Receive TAG, send slightly delayed (< 1s) ACK
|
12 | 1434 | alevkoy | * - Pass if sends one and only one TAG packet (and changes state appropriately)
|
13 | 1438 | alevkoy | * 4. Receive TAG, do not send ACK
|
14 | 1434 | alevkoy | * - Pass if sends one and only one TAG packet (and does not change state)
|
15 | 1438 | alevkoy | * 5. Receive TAG, send ACK to incorrect robot
|
16 | 1434 | alevkoy | * - Pass if goes to wait state, then back to hunter state
|
17 | 1438 | alevkoy | * 6. Simulate TAG from robot A to B, ACK from B to A
|
18 | 1434 | alevkoy | * - Pass if ignores TAG, goes to wait then hunter in response to ACK
|
19 | */
|
||
20 | |||
21 | 1433 | alevkoy | #include <stdlib.h> |
22 | #include <stdio.h> |
||
23 | 1438 | alevkoy | #include <time.h> |
24 | 1433 | alevkoy | #include <wl_basic.h> |
25 | 1438 | alevkoy | #include <wireless.h> |
26 | #include "../../libwireless/lib/wireless.h" |
||
27 | 1433 | alevkoy | #include "hunter_prey.h" |
28 | |||
29 | 1434 | alevkoy | #define CHANNEL 0xF // channel for wireless communication |
30 | #define TYPE 42 // packet type for wireless communication |
||
31 | 1438 | alevkoy | #define ROBOTID 255 // make up a robot id because the PC doesn't have one |
32 | 1434 | alevkoy | |
33 | 1438 | alevkoy | int main(int argc, char *argv[]) |
34 | 1433 | alevkoy | { |
35 | 1434 | alevkoy | char send_buffer[2]; // holds data to send |
36 | int data_length; // length of data received |
||
37 | unsigned char *packet_data; // data received |
||
38 | 1433 | alevkoy | |
39 | 1434 | alevkoy | // set up wireless
|
40 | wl_basic_init_default(); |
||
41 | wl_set_channel(CHANNEL); |
||
42 | 1438 | alevkoy | wl_set_com_port("/dev/ttyUSB0");
|
43 | 1434 | alevkoy | |
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 | 1438 | alevkoy | send_buffer[0] = HUNTER_PREY_ACTION_ACK;
|
58 | 1434 | alevkoy | send_buffer[1] = packet_data[1]; |
59 | wl_basic_send_global_packet(TYPE, send_buffer, 2);
|
||
60 | |||
61 | 1438 | alevkoy | printf("PASSED\n");
|
62 | 1434 | alevkoy | } |
63 | else
|
||
64 | 1438 | alevkoy | 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 | 1433 | alevkoy | } |