Project

General

Profile

Revision 1466

Added by Brad Neuman over 11 years ago

started on the ref bot, added some error checking to the testbench
setting the channel doesnt seem to work, so everyone will have to use C

View differences:

trunk/code/projects/hunter_prey/testbench/main.c
62 62
    delay8.tv_sec = 2;
63 63
    delay8.tv_nsec = 800000000;
64 64

  
65
    wl_set_com_port("/dev/ttyUSB1");
65
    wl_set_com_port("/dev/ttyUSB0");
66 66
    // set up wireless
67
    wl_basic_init_default();
68
    wl_set_channel(channel);
67
    ret = wl_basic_init_default();
69 68

  
69
    if(ret) {
70
      printf("ERROR: wl_basic_init_default returned %d\n", ret);
71
      return ret;
72
    }
73

  
74
    ret = wl_set_channel(channel);
75

  
76
    if(ret) {
77
      printf("ERROR: set channel failed! %d\n", ret);
78
      return ret;
79
    }
80

  
70 81
    printf("Testing communications\n\n");
71 82

  
72 83
    // Receive TAG, send ACK
trunk/code/projects/hunter_prey/main.c
4 4
#include "smart_run_around_fsm.h"
5 5
#include "hunter_prey.h"
6 6

  
7
#define CHAN 0xF
7
#define CHAN 0xC
8 8
#define TAG_PAUSE 2000
9 9

  
10 10
#define ROBOT_HUNTER 0
trunk/code/projects/hunter_prey/ref/main.c
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
#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
}
129

  
trunk/code/projects/hunter_prey/ref/Makefile
1
CC = gcc
2
CFLAGS = -Wall -g
3

  
4
all: ref
5

  
6
ref: *.c ../../libwireless/lib/*.c 
7
	$(CC) $(CFLAGS) *.c -L../../libwireless/lib -o ref -DWL_DEBUG -lwireless -lpthread -ggdb
8

  
9
clean:
10
	rm -f *.o ref ../lib/*.o
11

  

Also available in: Unified diff