Revision 1438 trunk/code/projects/hunter_prey/testbench/main.c
| main.c (revision 1438) | ||
|---|---|---|
| 4 | 4 |
*/ |
| 5 | 5 |
|
| 6 | 6 |
/* The tests shall be as follows |
| 7 |
* Receive a TAG, send an ACK |
|
| 7 |
* 1. Receive a TAG, send an ACK |
|
| 8 | 8 |
* - Robot passes if it sends a tag and changes to prey state |
| 9 |
* Send a TAG, receive an ACK |
|
| 9 |
* 2. Send a TAG, receive an ACK |
|
| 10 | 10 |
* - Pass if sends ACK, changes to wait state, then hunter state |
| 11 |
* Receive TAG, send slightly delayed (< 1s) ACK |
|
| 11 |
* 3. Receive TAG, send slightly delayed (< 1s) ACK |
|
| 12 | 12 |
* - Pass if sends one and only one TAG packet (and changes state appropriately) |
| 13 |
* Receive TAG, do not send ACK |
|
| 13 |
* 4. Receive TAG, do not send ACK |
|
| 14 | 14 |
* - Pass if sends one and only one TAG packet (and does not change state) |
| 15 |
* Receive TAG, send ACK to incorrect robot |
|
| 15 |
* 5. Receive TAG, send ACK to incorrect robot |
|
| 16 | 16 |
* - Pass if goes to wait state, then back to hunter state |
| 17 |
* Simulate TAG from robot A to B, ACK from B to A |
|
| 17 |
* 6. Simulate TAG from robot A to B, ACK from B to A |
|
| 18 | 18 |
* - Pass if ignores TAG, goes to wait then hunter in response to ACK |
| 19 | 19 |
*/ |
| 20 | 20 |
|
| 21 | 21 |
#include <stdlib.h> |
| 22 | 22 |
#include <stdio.h> |
| 23 |
#include <time.h> |
|
| 23 | 24 |
#include <wl_basic.h> |
| 25 |
#include <wireless.h> |
|
| 26 |
#include "../../libwireless/lib/wireless.h" |
|
| 24 | 27 |
#include "hunter_prey.h" |
| 25 | 28 |
|
| 26 | 29 |
#define CHANNEL 0xF // channel for wireless communication |
| 27 | 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 |
|
| 28 | 32 |
|
| 29 |
void main() |
|
| 33 |
int main(int argc, char *argv[]) |
|
| 30 | 34 |
{
|
| 31 | 35 |
char send_buffer[2]; // holds data to send |
| 32 | 36 |
int data_length; // length of data received |
| ... | ... | |
| 35 | 39 |
// set up wireless |
| 36 | 40 |
wl_basic_init_default(); |
| 37 | 41 |
wl_set_channel(CHANNEL); |
| 38 |
wl_set_com("/dev/ttyUSB0");
|
|
| 42 |
wl_set_com_port("/dev/ttyUSB0");
|
|
| 39 | 43 |
|
| 40 | 44 |
printf("Testing communications\n\n");
|
| 41 | 45 |
|
| ... | ... | |
| 50 | 54 |
if (data_length >= 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG) |
| 51 | 55 |
{
|
| 52 | 56 |
// send back an ACK |
| 53 |
send_buffer[0] = HUNTER_PREY_ACTION_TAG; |
|
| 57 |
send_buffer[0] = HUNTER_PREY_ACTION_ACK; |
|
| 54 | 58 |
send_buffer[1] = packet_data[1]; |
| 55 | 59 |
wl_basic_send_global_packet(TYPE, send_buffer, 2); |
| 56 | 60 |
|
| 57 |
printf("PASSED");
|
|
| 61 |
printf("PASSED\n");
|
|
| 58 | 62 |
} |
| 59 | 63 |
else |
| 60 |
printf("FAILED");
|
|
| 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; |
|
| 61 | 167 |
} |
| 62 | 168 |
|
Also available in: Unified diff