Project

General

Profile

Statistics
| Revision:

root / trunk / code / behaviors / hunter_prey / oldRef / main.c @ 1852

History | View | Annotate | Download (2.88 KB)

1
#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

    
11
#define NUM_BOTS 17
12
#define TOTAL_GAME_TIME 600*5 //in 100 ms intervals
13

    
14
void packet_receive(char type, int source, unsigned char* packet, int length);
15

    
16
volatile unsigned int preyTime[20];
17
volatile int preyBot=-1;
18

    
19
void waitKey(void) {
20
  unsigned char buf[10];
21

    
22
  //read from stdin
23
   while(read(0, buf, 10)==10) {
24
     printf("!");
25
     fflush(stdout);
26
   }
27

    
28
  while(getchar()==-1);
29
}
30

    
31
void print_stats(void) {
32

    
33
  int i=0;
34

    
35
  for(i=0;i<NUM_BOTS;i++) {
36
    printf("%5d ", preyTime[i]);
37
  }
38

    
39
  printf("\n");
40

    
41
}
42

    
43
int main(int argc, char *argv[])
44
{
45
    char send_buffer[2];    // holds data to send
46
    int data_length;        // length of data received
47
    unsigned char *packet_data;        // data received
48
    int ret;
49
    int i;
50
    int winner, maxTime=0;
51

    
52
    unsigned int channel = 0xF;
53

    
54
    struct timespec delay, rem;
55

    
56
    delay.tv_sec = 0;
57
    delay.tv_nsec = 100000000;
58

    
59

    
60
    if(argc > 1) {
61
      channel = atoi(argv[1]);
62
    }
63

    
64
    printf("using wireless channel %d\n", channel);
65

    
66
    wl_set_com_port("/dev/ttyUSB0");
67
    // set up wireless
68
    ret = wl_basic_init(&packet_receive);
69

    
70
    if(ret) {
71
      printf("ERROR: wl_basic_init failed. %d\n", ret);
72
      return ret;
73
    }
74
    //wl_set_channel(channel);
75

    
76
    for(i=0;i<NUM_BOTS;i++) {
77
      preyTime[i]=0;
78
    }
79

    
80
    ret = 0;    
81
    i=0;
82

    
83
    printf("ready\n");
84

    
85
    //TODO: use rtc to make this more accurate
86
    while(i < TOTAL_GAME_TIME) {
87
      wl_do();
88

    
89
      if(ret==0) {
90
        ret = nanosleep(&delay, &rem);
91
      }
92
      else {
93
        ret = nanosleep(&rem, &rem);
94
      }
95

    
96
      //update counts when we have waited at least enough time
97
      if(ret == 0 && preyBot >= 0) {
98
        preyTime[preyBot]++;
99

    
100
        if(i==0) { //time starts on the first real tag
101
          printf("\nThe race is on!\n");
102
        }
103
        i++;
104
      }
105

    
106
      if((TOTAL_GAME_TIME - i)%600 == 0) {
107
        printf("%d minutes remaining!\n", (TOTAL_GAME_TIME - i)/600);
108
      }
109

    
110
      if(i%10==0) {
111
        print_stats();
112
        if( TOTAL_GAME_TIME - i <= 100)
113
          printf("%d...\n", (TOTAL_GAME_TIME - i)/10);
114
      }
115
    }
116

    
117

    
118
    for(i=1;i<NUM_BOTS;i++) {
119
      if(preyTime[i] > maxTime) {
120
        maxTime = preyTime[i];
121
        winner = i;
122
      }
123
    }
124

    
125
    printf("\n\GAME OVER!n\nAAAAAAAAAAND the winner is\nBOT %d!!!!\n", winner);
126

    
127
    return 0;
128
}
129

    
130
void packet_receive(char type, int source, unsigned char* packet, int length)
131
{
132

    
133
  if(type==42 && length>=2){
134
    if(packet[0] == HUNTER_PREY_ACTION_TAG) {
135
      //TODO: check tag
136

    
137
    }
138
    else if(packet[0] == HUNTER_PREY_ACTION_ACK) {
139

    
140
      preyBot = packet[1];
141
      printf("bot %d is prey!\n", preyBot);
142

    
143
    }
144
    else {
145
      printf("got invalid packet! %c%d\n", packet[0], packet[1]);
146
    }
147
  }
148
  else {
149
    printf("got unknown packet! type=%d, length=%d\n", type, length);
150
  }
151
}
152