Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / hunter_prey / testbench / main.c @ 1451

History | View | Annotate | Download (6.69 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 1441 alevkoy
#include <unistd.h>
24
#include "../../libwireless/lib/wl_basic.h"
25 1438 alevkoy
#include "../../libwireless/lib/wireless.h"
26 1433 alevkoy
#include "hunter_prey.h"
27 1447 bneuman
#include <time.h>
28 1433 alevkoy
29 1451 bneuman
#define CHANNEL 12 //0xF // channel for wireless communication
30 1434 alevkoy
#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 1451 bneuman
34
void waitKey() {
35
  unsigned char buf[10];
36
37
  //read from stdin
38
   while(read(0, buf, 10)==10) {
39
     printf("!");
40
     fflush(stdout);
41
   }
42
43
  while(getchar()==-1);
44
}
45
46 1438 alevkoy
int main(int argc, char *argv[])
47 1433 alevkoy
{
48 1434 alevkoy
    char send_buffer[2];    // holds data to send
49
    int data_length;        // length of data received
50
    unsigned char *packet_data;        // data received
51 1447 bneuman
    int ret;
52 1433 alevkoy
53 1447 bneuman
    struct timespec delay8, rem;
54
55
    delay8.tv_sec = 2;
56
    delay8.tv_nsec = 800000000;
57
58
    wl_set_com_port("/dev/ttyUSB0");
59 1434 alevkoy
    // set up wireless
60
    wl_basic_init_default();
61
    wl_set_channel(CHANNEL);
62
63
    printf("Testing communications\n\n");
64
65
    // Receive TAG, send ACK
66
    printf("Receive TAG, send ACK... ");
67 1442 alevkoy
    fflush(stdout);
68 1434 alevkoy
    // Wait until we receive a packet
69
    while (!(packet_data = wl_basic_do_default(&data_length)));
70
71 1447 bneuman
    printf("got packet, validating and sending ack...");
72
    fflush(stdout);
73
74 1434 alevkoy
    if (data_length > 2)
75 1442 alevkoy
    {
76 1434 alevkoy
        printf("Excessive TAG packet length... ");
77 1442 alevkoy
        fflush(stdout);
78
    }
79 1434 alevkoy
80
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
81
    {
82
        // send back an ACK
83 1438 alevkoy
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
84 1434 alevkoy
        send_buffer[1] = packet_data[1];
85 1447 bneuman
        printf("sending ack intended for robot %d\n", packet_data[1]);
86
        fflush(stdout);
87 1434 alevkoy
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
88
89 1438 alevkoy
        printf("PASSED\n");
90 1434 alevkoy
    }
91
    else
92 1438 alevkoy
        printf("FAILED\n");
93
94 1447 bneuman
95 1451 bneuman
    waitKey();
96 1447 bneuman
97 1438 alevkoy
    // Send a TAG, receive an ACK
98
    printf("Send TAG, wait for ACK... ");
99 1442 alevkoy
    fflush(stdout);
100 1438 alevkoy
101
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
102
    send_buffer[1] = ROBOTID;
103
    // robot number stays the same
104
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
105
106
    // Wait for an ACK
107 1447 bneuman
    ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
108
109
    while(ret) {
110
      //      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
111
      ret = nanosleep(&rem, &rem);
112
    }
113
114
    data_length = -1;
115 1438 alevkoy
    packet_data = wl_basic_do_default(&data_length);
116
117
    // Check ACK for presence and correctness
118
    if (!packet_data || data_length < 2 ||
119
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
120 1451 bneuman
        packet_data[1] != ROBOTID) {
121 1447 bneuman
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
122 1451 bneuman
      if(data_length >= 2) {
123
        printf("\tpacket = [%c, %d]\n",packet_data[0], packet_data[1]);
124
      }
125
    }
126 1438 alevkoy
    else
127
        printf("PASSED\n");
128
129 1451 bneuman
    waitKey();
130
131 1438 alevkoy
    // Receive a TAG, send a delayed ACK
132
    printf("Receive a TAG, send a delayed ACK... ");
133 1442 alevkoy
    fflush(stdout);
134 1438 alevkoy
135
    while (!(packet_data = wl_basic_do_default(&data_length)));
136
137
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
138
    {
139 1447 bneuman
        printf("got TAG, waiting...");
140
        fflush(stdout);
141 1438 alevkoy
        // wait before sending ACK back
142 1447 bneuman
        //usleep(900000);
143
        delay8.tv_sec = 0;
144
        ret = nanosleep(&delay8, &rem);  // wait for 800 ms before sending ACK
145 1438 alevkoy
146 1447 bneuman
        while(ret) {
147
          printf(".");
148
          fflush(stdout);//      printf("nanosleep not done, needs to wait %ld more\n", rem.tv_nsec);
149
          ret = nanosleep(&rem, &rem);
150
        }
151
152
153 1438 alevkoy
        if (wl_basic_do_default(&data_length))
154 1447 bneuman
            printf("FAILED, got another TAG too soon\n");
155 1438 alevkoy
        else
156
        {
157
            // send packet
158
            send_buffer[0] = HUNTER_PREY_ACTION_ACK;
159
            send_buffer[1] = packet_data[1];
160
            wl_basic_send_global_packet(TYPE, send_buffer, 2);
161
162
            printf("PASSED\n");
163
        }
164
    }
165
166
    else
167
        printf("FAILED\n");
168
169 1447 bneuman
170 1451 bneuman
    waitKey();
171
172 1447 bneuman
    printf("sending courtesy tag...");
173
    fflush(stdout);
174
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
175
    send_buffer[1] = ROBOTID;
176
    // robot number stays the same
177
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
178
    printf("done.\nwaiting for ack...");
179
    fflush(stdout);
180
181
    while (!(packet_data = wl_basic_do_default(&data_length)));
182
183
    if (!packet_data || data_length < 2 ||
184
            packet_data[0] != HUNTER_PREY_ACTION_ACK ||
185
            packet_data[1] != ROBOTID)
186
      printf("FAILED, packet=%p, len=%d\n", packet_data, data_length);
187
    else
188
        printf("done\n");
189
190 1451 bneuman
    waitKey();
191 1447 bneuman
192 1438 alevkoy
    // Receive a TAG, never send an ACK
193
    printf("Receive TAG, never send ACK... ");
194 1442 alevkoy
    fflush(stdout);
195 1438 alevkoy
196
    while (!(packet_data = wl_basic_do_default(&data_length)));
197
198
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
199
    {
200 1447 bneuman
201
        printf("got TAG, monitoring for 5 seconds\n");
202
        fflush(stdout);
203 1441 alevkoy
        usleep(5000000);    // wait 5 seconds to see if they TAG again
204 1438 alevkoy
205
        if (wl_basic_do_default(&data_length))
206 1447 bneuman
            printf("FAILED, robot is spamming\n");
207 1438 alevkoy
        else
208
            printf("PASSED\n");
209
    }
210
211
    else
212
        printf("FAILED\n");
213
214 1451 bneuman
    waitKey();
215
216 1438 alevkoy
    // Receive TAG, send ACK to incorrect robot
217
    printf("Receive TAG, send ACK to incorrect robot... ");
218 1442 alevkoy
    fflush(stdout);
219 1438 alevkoy
220
    // Wait until we receive a packet
221
    while (!(packet_data = wl_basic_do_default(&data_length)));
222
223
    if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
224
    {
225
        // send back an ACK
226
        send_buffer[0] = HUNTER_PREY_ACTION_ACK;
227
        send_buffer[1] = packet_data[1] + 1;
228
        wl_basic_send_global_packet(TYPE, send_buffer, 2);
229
230
        printf("PASSED\n");
231
    }
232
    else
233
        printf("FAILED\n");
234
235 1451 bneuman
    waitKey();
236
237 1438 alevkoy
    // Simulate TAG from robot A to B, ACK from robot B to A
238
    printf("Send TAG from robot A to B, ACK from B to A... ");
239 1442 alevkoy
    fflush(stdout);
240 1438 alevkoy
241
    // TAG
242
    send_buffer[0] = HUNTER_PREY_ACTION_TAG;
243
    send_buffer[1] = packet_data[1] - 1;    // robot other than testee
244
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
245
246
    // ACK
247
    send_buffer[0] = HUNTER_PREY_ACTION_ACK;
248
    // send_buffer[1] stays the same
249
    wl_basic_send_global_packet(TYPE, send_buffer, 2);
250
251
    if (wl_basic_do_default(&data_length))
252
        printf("FAILED\n");
253
    else
254
        printf("PASSED\n");
255
256
    return 0;
257 1433 alevkoy
}