Index: trunk/code/behaviors/hunter_prey/ref/main.c
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/main.c	(revision 1849)
+++ trunk/code/behaviors/hunter_prey/ref/main.c	(nonexistent)
@@ -1,152 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <time.h>
-#include <unistd.h>
-#include "../../libwireless/lib/wl_basic.h"
-#include "../../libwireless/lib/wireless.h"
-#include "../hunter_prey.h"
-
-#define TYPE 42	// packet type for wireless communication
-
-#define NUM_BOTS 17
-#define TOTAL_GAME_TIME 600*5 //in 100 ms intervals
-
-void packet_receive(char type, int source, unsigned char* packet, int length);
-
-volatile unsigned int preyTime[20];
-volatile int preyBot=-1;
-
-void waitKey(void) {
-  unsigned char buf[10];
-
-  //read from stdin
-   while(read(0, buf, 10)==10) {
-     printf("!");
-     fflush(stdout);
-   }
-
-  while(getchar()==-1);
-}
-
-void print_stats(void) {
-
-  int i=0;
-
-  for(i=0;i<NUM_BOTS;i++) {
-    printf("%5d ", preyTime[i]);
-  }
-
-  printf("\n");
-
-}
-
-int main(int argc, char *argv[])
-{
-    char send_buffer[2];    // holds data to send
-    int data_length;	// length of data received
-    unsigned char *packet_data;	// data received
-    int ret;
-    int i;
-    int winner, maxTime=0;
-
-    unsigned int channel = 0xF;
-
-    struct timespec delay, rem;
-
-    delay.tv_sec = 0;
-    delay.tv_nsec = 100000000;
-
-
-    if(argc > 1) {
-      channel = atoi(argv[1]);
-    }
-
-    printf("using wireless channel %d\n", channel);
-
-    wl_set_com_port("/dev/ttyUSB0");
-    // set up wireless
-    ret = wl_basic_init(&packet_receive);
-
-    if(ret) {
-      printf("ERROR: wl_basic_init failed. %d\n", ret);
-      return ret;
-    }
-    //wl_set_channel(channel);
-
-    for(i=0;i<NUM_BOTS;i++) {
-      preyTime[i]=0;
-    }
-
-    ret = 0;    
-    i=0;
-
-    printf("ready\n");
-
-    //TODO: use rtc to make this more accurate
-    while(i < TOTAL_GAME_TIME) {
-      wl_do();
-
-      if(ret==0) {
-	ret = nanosleep(&delay, &rem);
-      }
-      else {
-	ret = nanosleep(&rem, &rem);
-      }
-
-      //update counts when we have waited at least enough time
-      if(ret == 0 && preyBot >= 0) {
-	preyTime[preyBot]++;
-
-	if(i==0) { //time starts on the first real tag
-	  printf("\nThe race is on!\n");
-	}
-	i++;
-      }
-
-      if((TOTAL_GAME_TIME - i)%600 == 0) {
-	printf("%d minutes remaining!\n", (TOTAL_GAME_TIME - i)/600);
-      }
-
-      if(i%10==0) {
-	print_stats();
-	if( TOTAL_GAME_TIME - i <= 100)
-	  printf("%d...\n", (TOTAL_GAME_TIME - i)/10);
-      }
-    }
-
-
-    for(i=1;i<NUM_BOTS;i++) {
-      if(preyTime[i] > maxTime) {
-	maxTime = preyTime[i];
-	winner = i;
-      }
-    }
-
-    printf("\n\GAME OVER!n\nAAAAAAAAAAND the winner is\nBOT %d!!!!\n", winner);
-
-    return 0;
-}
-
-void packet_receive(char type, int source, unsigned char* packet, int length)
-{
-
-  if(type==42 && length>=2){
-    if(packet[0] == HUNTER_PREY_ACTION_TAG) {
-      //TODO: check tag
-
-    }
-    else if(packet[0] == HUNTER_PREY_ACTION_ACK) {
-
-      preyBot = packet[1];
-      printf("bot %d is prey!\n", preyBot);
-
-    }
-    else {
-      printf("got invalid packet! %c%d\n", packet[0], packet[1]);
-    }
-  }
-  else {
-    printf("got unknown packet! type=%d, length=%d\n", type, length);
-  }
-}
-
Index: trunk/code/behaviors/hunter_prey/ref/main.c.old
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/main.c.old	(nonexistent)
+++ trunk/code/behaviors/hunter_prey/ref/main.c.old	(revision 1850)
@@ -0,0 +1,152 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+#include "../../libwireless/lib/wl_basic.h"
+#include "../../libwireless/lib/wireless.h"
+#include "../hunter_prey.h"
+
+#define TYPE 42	// packet type for wireless communication
+
+#define NUM_BOTS 17
+#define TOTAL_GAME_TIME 600*5 //in 100 ms intervals
+
+void packet_receive(char type, int source, unsigned char* packet, int length);
+
+volatile unsigned int preyTime[20];
+volatile int preyBot=-1;
+
+void waitKey(void) {
+  unsigned char buf[10];
+
+  //read from stdin
+   while(read(0, buf, 10)==10) {
+     printf("!");
+     fflush(stdout);
+   }
+
+  while(getchar()==-1);
+}
+
+void print_stats(void) {
+
+  int i=0;
+
+  for(i=0;i<NUM_BOTS;i++) {
+    printf("%5d ", preyTime[i]);
+  }
+
+  printf("\n");
+
+}
+
+int main(int argc, char *argv[])
+{
+    char send_buffer[2];    // holds data to send
+    int data_length;	// length of data received
+    unsigned char *packet_data;	// data received
+    int ret;
+    int i;
+    int winner, maxTime=0;
+
+    unsigned int channel = 0xF;
+
+    struct timespec delay, rem;
+
+    delay.tv_sec = 0;
+    delay.tv_nsec = 100000000;
+
+
+    if(argc > 1) {
+      channel = atoi(argv[1]);
+    }
+
+    printf("using wireless channel %d\n", channel);
+
+    wl_set_com_port("/dev/ttyUSB0");
+    // set up wireless
+    ret = wl_basic_init(&packet_receive);
+
+    if(ret) {
+      printf("ERROR: wl_basic_init failed. %d\n", ret);
+      return ret;
+    }
+    //wl_set_channel(channel);
+
+    for(i=0;i<NUM_BOTS;i++) {
+      preyTime[i]=0;
+    }
+
+    ret = 0;    
+    i=0;
+
+    printf("ready\n");
+
+    //TODO: use rtc to make this more accurate
+    while(i < TOTAL_GAME_TIME) {
+      wl_do();
+
+      if(ret==0) {
+	ret = nanosleep(&delay, &rem);
+      }
+      else {
+	ret = nanosleep(&rem, &rem);
+      }
+
+      //update counts when we have waited at least enough time
+      if(ret == 0 && preyBot >= 0) {
+	preyTime[preyBot]++;
+
+	if(i==0) { //time starts on the first real tag
+	  printf("\nThe race is on!\n");
+	}
+	i++;
+      }
+
+      if((TOTAL_GAME_TIME - i)%600 == 0) {
+	printf("%d minutes remaining!\n", (TOTAL_GAME_TIME - i)/600);
+      }
+
+      if(i%10==0) {
+	print_stats();
+	if( TOTAL_GAME_TIME - i <= 100)
+	  printf("%d...\n", (TOTAL_GAME_TIME - i)/10);
+      }
+    }
+
+
+    for(i=1;i<NUM_BOTS;i++) {
+      if(preyTime[i] > maxTime) {
+	maxTime = preyTime[i];
+	winner = i;
+      }
+    }
+
+    printf("\n\GAME OVER!n\nAAAAAAAAAAND the winner is\nBOT %d!!!!\n", winner);
+
+    return 0;
+}
+
+void packet_receive(char type, int source, unsigned char* packet, int length)
+{
+
+  if(type==42 && length>=2){
+    if(packet[0] == HUNTER_PREY_ACTION_TAG) {
+      //TODO: check tag
+
+    }
+    else if(packet[0] == HUNTER_PREY_ACTION_ACK) {
+
+      preyBot = packet[1];
+      printf("bot %d is prey!\n", preyBot);
+
+    }
+    else {
+      printf("got invalid packet! %c%d\n", packet[0], packet[1]);
+    }
+  }
+  else {
+    printf("got unknown packet! type=%d, length=%d\n", type, length);
+  }
+}
+
Index: trunk/code/behaviors/hunter_prey/ref/refBot.c
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/refBot.c	(nonexistent)
+++ trunk/code/behaviors/hunter_prey/ref/refBot.c	(revision 1850)
@@ -0,0 +1,165 @@
+/* Referee Bot for Hunter-Prey
+ * Ben Wasserman
+ * Robotics Club Colony Project 2010
+ */
+
+#include <dragonfly_lib.h>
+#include <wl_basic.h>
+#include "hunter_prey.h"
+
+int main(void){
+	
+	dragonfly_init(ALL_ON);
+	xbee_init();
+	wl_basic_init_default();
+	wl_set_channel(15);
+	rtc_init(SIXTEENTH_SECOND, NULL);
+	
+	int currentPrey, length = 0, index, lastTime = 0, lastAck = 0, preyTime[20], timeOfLastTag[20];
+	unsigned char *packet, lastTags[5], tags[20], acks[20], timesAsPrey[20];
+	float iAcks, iTags, ratio;
+	
+	for(currentPrey=19; currentPrey>0; currentPrey--){
+		preyTime[currentPrey] = 0;
+		tags[currentPrey] = 0;
+		acks[currentPrey] = 0;
+		timesAsPrey[currentPrey] = 0;
+		timeOfLastTag[currentPrey] = 0;
+	}
+	lastTags[0] = 0;
+	lastTags[1] = 0;
+	lastTags[2] = 0;
+	lastTags[3] = 0;
+	lastTags[4] = 0;
+	
+	delay_ms(1000);
+	
+	while(1){
+		length = 0;
+		if(rtc_get()%2){
+			orb1_set_color(GREEN);
+		}else{
+			orb1_set_color(BLUE);
+		}
+		delay_ms(10);
+		packet = wl_basic_do_default(&length);
+		switch(length){
+			case 0:
+				delay_ms(10);
+				orb2_set_color(BLUE);
+				break;
+			case 1:
+				usb_puts("\r\nGot packet of size 1: ");
+				usb_puti(packet[0]);
+				orb2_set_color(ORANGE);
+				break;
+			case 2:
+				usb_puts("\r\nGot packet of size 2: ");
+				usb_puti(packet[0]);
+				usb_putc('\t');
+				usb_puti(packet[0]);
+				orb2_set_color(ORANGE);
+				break;
+			case 3:
+				usb_puts("\r\nGot packet of size 3: Action: ");
+				switch(packet[0]){
+					case HUNTER_PREY_ACTION_TAG:
+						usb_puts("TAG");
+						lastTags[0] = lastTags[1];
+						lastTags[1] = lastTags[2];
+						lastTags[2] = lastTags[3];
+						lastTags[3] = lastTags[4];
+						lastTags[4] = packet[1];
+						if(packet[1] != currentPrey && (rtc_get() - timeOfLastTag[packet[1]]) > 16 && (rtc_get() - lastAck) > 48){
+							tags[packet[1]]++;
+						}else{
+							usb_puts(" CHEAT");
+						}
+						timeOfLastTag[packet[1]] = rtc_get();
+						orb2_set_color(PURPLE);
+						break;
+					case HUNTER_PREY_ACTION_ACK:
+						usb_puts("ACK");
+						if(lastTags[0]==packet[1] || lastTags[1]==packet[1] || lastTags[2]==packet[1] || lastTags[3]==packet[1] || lastTags[4]==packet[1]){
+							acks[packet[1]]++;
+							timesAsPrey[packet[2]]++;
+							currentPrey = packet[1];
+							lastAck = rtc_get();
+							usb_puts(" New Prey: ");
+							usb_puti(currentPrey);
+							orb2_set_color(GREEN);
+						}else{
+							usb_puts(" Spam!");
+							orb2_set_color(ORANGE);
+						}
+						break;
+					default:
+						usb_puts("Unknown: ");
+						usb_puti(packet[0]);
+						orb2_set_color(ORANGE);
+						break;
+				}
+				usb_puts(" Tagging Bot: ");
+				usb_puti(packet[1]);
+				usb_puts(" Sending Bot: ");
+				usb_puti(packet[2]);
+				if(packet[0] == HUNTER_PREY_ACTION_TAG && packet[1] != packet[2]){
+					usb_puts(" Tag error: mismatched IDs");
+				}
+				break;
+			default:
+				usb_puts("\nOversized packet. Contents: ");
+				for(index = 0; index < length; index++){
+					usb_puti(packet[index]);
+					usb_putc(' ');
+					orb2_set_color(ORANGE);
+				}
+		}
+		if(rtc_get() - 4 > lastTime){
+			preyTime[currentPrey]++;
+			lastTime = rtc_get();
+			if(timesAsPrey[currentPrey] == 0){
+				timesAsPrey[currentPrey] = 1;
+			}
+		}
+		if(button1_read()){
+			usb_puts("\r\n!!Game Stats!! Game time: ");
+			usb_puti(rtc_get() / 16);
+			usb_puts("\r\nBot\tTags\tAcks\tATR\tP Time\t#aP\tTp#\r\n");
+			for(index = 1; index < 20; index++){
+				if(tags[index]){
+					usb_puti(index);
+					if(index == currentPrey){
+						usb_putc('*');
+					}
+					usb_putc('\t');
+					usb_puti(tags[index]);
+					usb_putc('\t');
+					usb_puti(acks[index]);
+					usb_putc('\t');
+					iAcks = acks[index];
+					iTags = tags[index];
+					iAcks *= 1000;
+					iTags *= 1000;
+					ratio = iAcks / iTags;
+					usb_puti(ratio*1000);
+					usb_putc('\t');
+					usb_puti(preyTime[index]/4);
+					usb_putc('\t');
+					usb_puti(timesAsPrey[index]);
+					usb_putc('\t');
+					if(timesAsPrey[index]){
+						usb_puti((preyTime[index] / 4) / timesAsPrey[index]);
+					}else{
+						usb_puts("NaN");
+					}
+					usb_puts("\r\n");
+				}
+			}
+		}
+	}
+	
+	while(1);
+	
+	return 0;
+}

Property changes on: trunk/code/behaviors/hunter_prey/ref/refBot.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: trunk/code/behaviors/hunter_prey/ref/hunter_prey.c
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/hunter_prey.c	(nonexistent)
+++ trunk/code/behaviors/hunter_prey/ref/hunter_prey.c	(revision 1850)
@@ -0,0 +1,43 @@
+#include "hunter_prey.h"
+#include <dragonfly_lib.h>
+
+#define TAG_TIME 3
+#define TAG_RANGE 150
+
+/**** This file should not be edited! ****/
+
+
+/*
+ * The criteria for tagging are the following:
+ *   * The max bom is betweed 1 and 7
+ *   * The front rangefinder reads less than TAG_RANGE
+ *   * -1 values from the rangefinder are ignored
+ *   * These conditions are met across TAG_TIME calls to this function
+ */
+
+unsigned char hunter_prey_tagged(int max_bom, int frontRange) {
+
+  static int onTarget = 0;
+
+  if(max_bom < 7 && max_bom > 1 && frontRange > 0 && frontRange < TAG_RANGE) {
+    if(onTarget == 0) {
+      onTarget = TAG_TIME;
+      usb_puts("On target!\n");
+    }
+    else {
+      if(--onTarget <= 0) {
+	onTarget = 0;
+	usb_puts("TAG!\n");
+	return 1;
+      }
+    }
+  }
+  else{
+    //don't reset onTarget because the robot got too close
+    if(frontRange > 0)
+      onTarget = 0;
+  }
+
+  return 0;
+
+}

Property changes on: trunk/code/behaviors/hunter_prey/ref/hunter_prey.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: trunk/code/behaviors/hunter_prey/ref/Makefile.old
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/Makefile.old	(nonexistent)
+++ trunk/code/behaviors/hunter_prey/ref/Makefile.old	(revision 1850)
@@ -0,0 +1,11 @@
+CC = gcc
+CFLAGS = -Wall -g
+
+all: ref
+
+ref: *.c ../../libwireless/lib/*.c 
+	$(CC) $(CFLAGS) *.c -L../../libwireless/lib -o ref -DWL_DEBUG -lwireless -lpthread -ggdb
+
+clean:
+	rm -f *.o ref ../lib/*.o
+

Property changes on: trunk/code/behaviors/hunter_prey/ref/Makefile.old
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: trunk/code/behaviors/hunter_prey/ref/hunter_prey.h
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/hunter_prey.h	(nonexistent)
+++ trunk/code/behaviors/hunter_prey/ref/hunter_prey.h	(revision 1850)
@@ -0,0 +1,19 @@
+#ifndef _HUNTER_PREY_H
+#define _HUNTER_PREY_H
+
+#include <inttypes.h>
+
+/**** This file should not be edited! ****/
+
+/*
+ * The packet structure is 2 bytes
+ * byte 0 is the action, which is one of the values below
+ * byte 1 is the robot id
+ */
+
+#define HUNTER_PREY_ACTION_TAG 'T'
+#define HUNTER_PREY_ACTION_ACK 'A'
+
+unsigned char hunter_prey_tagged(int max_bom, int front_rangefinder);
+
+#endif

Property changes on: trunk/code/behaviors/hunter_prey/ref/hunter_prey.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: trunk/code/behaviors/hunter_prey/ref/Makefile
===================================================================
--- trunk/code/behaviors/hunter_prey/ref/Makefile	(revision 1849)
+++ trunk/code/behaviors/hunter_prey/ref/Makefile	(revision 1850)
@@ -1,11 +1,20 @@
-CC = gcc
-CFLAGS = -Wall -g
+# this is a local makefile
 
-all: ref
+# Relative path to the root directory (containing lib directory)
+ifndef COLONYROOT
+COLONYROOT := ..
 
-ref: *.c ../../libwireless/lib/*.c 
-	$(CC) $(CFLAGS) *.c -L../../libwireless/lib -o ref -DWL_DEBUG -lwireless -lpthread -ggdb
+# Target file name (without extension).
+TARGET = main
 
-clean:
-	rm -f *.o ref ../lib/*.o
+# Uncomment this to use the wireless library
+USE_WIRELESS = 1
 
+# com1 = serial port. Use lpt1 to connect to parallel port.
+AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
+
+else
+COLONYROOT := ../$(COLONYROOT)
+endif
+
+include $(COLONYROOT)/Makefile
\ No newline at end of file
