root / trunk / code / projects / hunter_prey / ref / main.c @ 1466
History | View | Annotate | Download (2.36 KB)
1 | 1466 | bneuman | #include <stdlib.h> |
---|---|---|---|
2 | #include <stdio.h> |
||
3 | #include <time.h> |
||
4 | #include <unistd.h> |
||
5 | #include "../../libwireless/lib/wl_basic.h" |
||
6 | #include "../../libwireless/lib/wireless.h" |
||
7 | #include "../hunter_prey.h" |
||
8 | |||
9 | #define TYPE 42 // packet type for wireless communication |
||
10 | #define ROBOTID 255 // make up a robot id because the PC doesn't have one |
||
11 | |||
12 | void packet_receive(char type, int source, unsigned char* packet, int length); |
||
13 | |||
14 | volatile unsigned int preyTime[20]; |
||
15 | volatile int preyBot=-1; |
||
16 | |||
17 | void waitKey(void) { |
||
18 | unsigned char buf[10]; |
||
19 | |||
20 | //read from stdin
|
||
21 | while(read(0, buf, 10)==10) { |
||
22 | printf("!");
|
||
23 | fflush(stdout); |
||
24 | } |
||
25 | |||
26 | while(getchar()==-1); |
||
27 | } |
||
28 | |||
29 | void print_stats(void) { |
||
30 | |||
31 | int i=0; |
||
32 | |||
33 | for(i=0;i<20;i++) { |
||
34 | printf("%5d ", preyTime[i]);
|
||
35 | } |
||
36 | |||
37 | printf("\n");
|
||
38 | |||
39 | } |
||
40 | |||
41 | int main(int argc, char *argv[]) |
||
42 | { |
||
43 | char send_buffer[2]; // holds data to send |
||
44 | int data_length; // length of data received |
||
45 | unsigned char *packet_data; // data received |
||
46 | int ret;
|
||
47 | int i;
|
||
48 | |||
49 | unsigned int channel = 0xF; |
||
50 | |||
51 | struct timespec delay, rem;
|
||
52 | |||
53 | delay.tv_sec = 0;
|
||
54 | delay.tv_nsec = 100000000;
|
||
55 | |||
56 | |||
57 | if(argc > 1) { |
||
58 | channel = atoi(argv[1]);
|
||
59 | } |
||
60 | |||
61 | printf("using wireless channel %d\n", channel);
|
||
62 | |||
63 | wl_set_com_port("/dev/ttyUSB0");
|
||
64 | // set up wireless
|
||
65 | ret = wl_basic_init(&packet_receive); |
||
66 | |||
67 | if(ret) {
|
||
68 | printf("ERROR: wl_basic_init failed. %d\n", ret);
|
||
69 | return ret;
|
||
70 | } |
||
71 | //wl_set_channel(channel);
|
||
72 | |||
73 | for(i=0;i<16;i++) { |
||
74 | preyTime[i]=0;
|
||
75 | } |
||
76 | |||
77 | ret = 0;
|
||
78 | i=0;
|
||
79 | |||
80 | printf("ready\n");
|
||
81 | |||
82 | //TODO: use rtc to make this more accurate
|
||
83 | while(1) { |
||
84 | wl_do(); |
||
85 | |||
86 | if(ret==0) { |
||
87 | ret = nanosleep(&delay, &rem); |
||
88 | } |
||
89 | else {
|
||
90 | ret = nanosleep(&rem, &rem); |
||
91 | } |
||
92 | |||
93 | //update counts when we have waited at least enough time
|
||
94 | if(ret == 0 && preyBot >= 0) { |
||
95 | preyTime[preyBot]++; |
||
96 | i++; |
||
97 | } |
||
98 | |||
99 | if(i%10==0) { |
||
100 | print_stats(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return 0; |
||
105 | } |
||
106 | |||
107 | void packet_receive(char type, int source, unsigned char* packet, int length) |
||
108 | { |
||
109 | |||
110 | if(type==42 && length>=2){ |
||
111 | if(packet[0] == HUNTER_PREY_ACTION_TAG) { |
||
112 | //TODO: check tag
|
||
113 | |||
114 | } |
||
115 | else if(packet[0] == HUNTER_PREY_ACTION_ACK) { |
||
116 | |||
117 | preyBot = packet[1];
|
||
118 | printf("bot %d is prey!\n", preyBot);
|
||
119 | |||
120 | } |
||
121 | else {
|
||
122 | printf("got invalid packet! %c%d\n", packet[0], packet[1]); |
||
123 | } |
||
124 | } |
||
125 | else {
|
||
126 | printf("got unknown packet! type=%d, length=%d\n", type, length);
|
||
127 | } |
||
128 | } |