Project

General

Profile

Revision 1850

Added my RefBot code, and the hunter_prey.* files that it needs. Brad's old code is still existing in main.c.old, and the Makefile for it is in Makefile.old. The new RefBot runs on a robot, not a USB XBee.

View differences:

trunk/code/behaviors/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

  
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

  
trunk/code/behaviors/hunter_prey/ref/main.c.old
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

  
trunk/code/behaviors/hunter_prey/ref/refBot.c
1
/* Referee Bot for Hunter-Prey
2
 * Ben Wasserman
3
 * Robotics Club Colony Project 2010
4
 */
5

  
6
#include <dragonfly_lib.h>
7
#include <wl_basic.h>
8
#include "hunter_prey.h"
9

  
10
int main(void){
11
	
12
	dragonfly_init(ALL_ON);
13
	xbee_init();
14
	wl_basic_init_default();
15
	wl_set_channel(15);
16
	rtc_init(SIXTEENTH_SECOND, NULL);
17
	
18
	int currentPrey, length = 0, index, lastTime = 0, lastAck = 0, preyTime[20], timeOfLastTag[20];
19
	unsigned char *packet, lastTags[5], tags[20], acks[20], timesAsPrey[20];
20
	float iAcks, iTags, ratio;
21
	
22
	for(currentPrey=19; currentPrey>0; currentPrey--){
23
		preyTime[currentPrey] = 0;
24
		tags[currentPrey] = 0;
25
		acks[currentPrey] = 0;
26
		timesAsPrey[currentPrey] = 0;
27
		timeOfLastTag[currentPrey] = 0;
28
	}
29
	lastTags[0] = 0;
30
	lastTags[1] = 0;
31
	lastTags[2] = 0;
32
	lastTags[3] = 0;
33
	lastTags[4] = 0;
34
	
35
	delay_ms(1000);
36
	
37
	while(1){
38
		length = 0;
39
		if(rtc_get()%2){
40
			orb1_set_color(GREEN);
41
		}else{
42
			orb1_set_color(BLUE);
43
		}
44
		delay_ms(10);
45
		packet = wl_basic_do_default(&length);
46
		switch(length){
47
			case 0:
48
				delay_ms(10);
49
				orb2_set_color(BLUE);
50
				break;
51
			case 1:
52
				usb_puts("\r\nGot packet of size 1: ");
53
				usb_puti(packet[0]);
54
				orb2_set_color(ORANGE);
55
				break;
56
			case 2:
57
				usb_puts("\r\nGot packet of size 2: ");
58
				usb_puti(packet[0]);
59
				usb_putc('\t');
60
				usb_puti(packet[0]);
61
				orb2_set_color(ORANGE);
62
				break;
63
			case 3:
64
				usb_puts("\r\nGot packet of size 3: Action: ");
65
				switch(packet[0]){
66
					case HUNTER_PREY_ACTION_TAG:
67
						usb_puts("TAG");
68
						lastTags[0] = lastTags[1];
69
						lastTags[1] = lastTags[2];
70
						lastTags[2] = lastTags[3];
71
						lastTags[3] = lastTags[4];
72
						lastTags[4] = packet[1];
73
						if(packet[1] != currentPrey && (rtc_get() - timeOfLastTag[packet[1]]) > 16 && (rtc_get() - lastAck) > 48){
74
							tags[packet[1]]++;
75
						}else{
76
							usb_puts(" CHEAT");
77
						}
78
						timeOfLastTag[packet[1]] = rtc_get();
79
						orb2_set_color(PURPLE);
80
						break;
81
					case HUNTER_PREY_ACTION_ACK:
82
						usb_puts("ACK");
83
						if(lastTags[0]==packet[1] || lastTags[1]==packet[1] || lastTags[2]==packet[1] || lastTags[3]==packet[1] || lastTags[4]==packet[1]){
84
							acks[packet[1]]++;
85
							timesAsPrey[packet[2]]++;
86
							currentPrey = packet[1];
87
							lastAck = rtc_get();
88
							usb_puts(" New Prey: ");
89
							usb_puti(currentPrey);
90
							orb2_set_color(GREEN);
91
						}else{
92
							usb_puts(" Spam!");
93
							orb2_set_color(ORANGE);
94
						}
95
						break;
96
					default:
97
						usb_puts("Unknown: ");
98
						usb_puti(packet[0]);
99
						orb2_set_color(ORANGE);
100
						break;
101
				}
102
				usb_puts(" Tagging Bot: ");
103
				usb_puti(packet[1]);
104
				usb_puts(" Sending Bot: ");
105
				usb_puti(packet[2]);
106
				if(packet[0] == HUNTER_PREY_ACTION_TAG && packet[1] != packet[2]){
107
					usb_puts(" Tag error: mismatched IDs");
108
				}
109
				break;
110
			default:
111
				usb_puts("\nOversized packet. Contents: ");
112
				for(index = 0; index < length; index++){
113
					usb_puti(packet[index]);
114
					usb_putc(' ');
115
					orb2_set_color(ORANGE);
116
				}
117
		}
118
		if(rtc_get() - 4 > lastTime){
119
			preyTime[currentPrey]++;
120
			lastTime = rtc_get();
121
			if(timesAsPrey[currentPrey] == 0){
122
				timesAsPrey[currentPrey] = 1;
123
			}
124
		}
125
		if(button1_read()){
126
			usb_puts("\r\n!!Game Stats!! Game time: ");
127
			usb_puti(rtc_get() / 16);
128
			usb_puts("\r\nBot\tTags\tAcks\tATR\tP Time\t#aP\tTp#\r\n");
129
			for(index = 1; index < 20; index++){
130
				if(tags[index]){
131
					usb_puti(index);
132
					if(index == currentPrey){
133
						usb_putc('*');
134
					}
135
					usb_putc('\t');
136
					usb_puti(tags[index]);
137
					usb_putc('\t');
138
					usb_puti(acks[index]);
139
					usb_putc('\t');
140
					iAcks = acks[index];
141
					iTags = tags[index];
142
					iAcks *= 1000;
143
					iTags *= 1000;
144
					ratio = iAcks / iTags;
145
					usb_puti(ratio*1000);
146
					usb_putc('\t');
147
					usb_puti(preyTime[index]/4);
148
					usb_putc('\t');
149
					usb_puti(timesAsPrey[index]);
150
					usb_putc('\t');
151
					if(timesAsPrey[index]){
152
						usb_puti((preyTime[index] / 4) / timesAsPrey[index]);
153
					}else{
154
						usb_puts("NaN");
155
					}
156
					usb_puts("\r\n");
157
				}
158
			}
159
		}
160
	}
161
	
162
	while(1);
163
	
164
	return 0;
165
}
0 166

  
trunk/code/behaviors/hunter_prey/ref/hunter_prey.c
1
#include "hunter_prey.h"
2
#include <dragonfly_lib.h>
3

  
4
#define TAG_TIME 3
5
#define TAG_RANGE 150
6

  
7
/**** This file should not be edited! ****/
8

  
9

  
10
/*
11
 * The criteria for tagging are the following:
12
 *   * The max bom is betweed 1 and 7
13
 *   * The front rangefinder reads less than TAG_RANGE
14
 *   * -1 values from the rangefinder are ignored
15
 *   * These conditions are met across TAG_TIME calls to this function
16
 */
17

  
18
unsigned char hunter_prey_tagged(int max_bom, int frontRange) {
19

  
20
  static int onTarget = 0;
21

  
22
  if(max_bom < 7 && max_bom > 1 && frontRange > 0 && frontRange < TAG_RANGE) {
23
    if(onTarget == 0) {
24
      onTarget = TAG_TIME;
25
      usb_puts("On target!\n");
26
    }
27
    else {
28
      if(--onTarget <= 0) {
29
	onTarget = 0;
30
	usb_puts("TAG!\n");
31
	return 1;
32
      }
33
    }
34
  }
35
  else{
36
    //don't reset onTarget because the robot got too close
37
    if(frontRange > 0)
38
      onTarget = 0;
39
  }
40

  
41
  return 0;
42

  
43
}
0 44

  
trunk/code/behaviors/hunter_prey/ref/Makefile.old
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

  
0 12

  
trunk/code/behaviors/hunter_prey/ref/hunter_prey.h
1
#ifndef _HUNTER_PREY_H
2
#define _HUNTER_PREY_H
3

  
4
#include <inttypes.h>
5

  
6
/**** This file should not be edited! ****/
7

  
8
/*
9
 * The packet structure is 2 bytes
10
 * byte 0 is the action, which is one of the values below
11
 * byte 1 is the robot id
12
 */
13

  
14
#define HUNTER_PREY_ACTION_TAG 'T'
15
#define HUNTER_PREY_ACTION_ACK 'A'
16

  
17
unsigned char hunter_prey_tagged(int max_bom, int front_rangefinder);
18

  
19
#endif
0 20

  
trunk/code/behaviors/hunter_prey/ref/Makefile
1
CC = gcc
2
CFLAGS = -Wall -g
1
# this is a local makefile
3 2

  
4
all: ref
3
# Relative path to the root directory (containing lib directory)
4
ifndef COLONYROOT
5
COLONYROOT := ..
5 6

  
6
ref: *.c ../../libwireless/lib/*.c 
7
	$(CC) $(CFLAGS) *.c -L../../libwireless/lib -o ref -DWL_DEBUG -lwireless -lpthread -ggdb
7
# Target file name (without extension).
8
TARGET = main
8 9

  
9
clean:
10
	rm -f *.o ref ../lib/*.o
10
# Uncomment this to use the wireless library
11
USE_WIRELESS = 1
11 12

  
13
# com1 = serial port. Use lpt1 to connect to parallel port.
14
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
15

  
16
else
17
COLONYROOT := ../$(COLONYROOT)
18
endif
19

  
20
include $(COLONYROOT)/Makefile

Also available in: Unified diff