Project

General

Profile

Revision 1852

Moved the old ref code to oldRef, and deleted the files from ref (the .old files).

View differences:

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

  
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

  
12 0

  
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/john/main.c
2 2
#include <wl_basic.h>
3 3
#include "hunter_prey.h"
4 4

  
5
#define WL_CHANNEL 12
5
#define WL_CHANNEL 15
6 6
#define HUNTER 3
7 7
#define PREY 1
8 8
#define PREHUNTER 2
......
14 14
int data_length;
15 15

  
16 16
/*  Data buffer used to send packets */
17
char send_buffer[2];
17
char send_buffer[3];
18 18

  
19 19
int main(void)
20 20
{
......
45 45
			orb1_set_color(YELLOW);
46 46
			bom_on();
47 47
			state=PREY;
48
			delay_ms(50);
48
			motor_l_set(200, BACKWARD);
49
			motor_r_set(200, BACKWARD);
50
			delay_ms(1500);
49 51
			while(wl_basic_do_default(&data_length));
50 52
			orb1_set_color(GREEN);
51 53
			rtc_reset();
......
64 66
                                packet_data = wl_basic_do_default(&data_length);
65 67
                                if(packet_data != 0)
66 68
                                {
67
                                        if(data_length == 2 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
69
                                        if(data_length == 3 && packet_data[0] == HUNTER_PREY_ACTION_TAG)
68 70
                                        {
69 71
                                                send_buffer[0] = HUNTER_PREY_ACTION_ACK;
70 72
                                                send_buffer[1] = packet_data[1];
71
                                                wl_basic_send_global_packet(42, send_buffer, 2);
73
												send_buffer[2] = id;
74
                                                
75
												(42, send_buffer, 3);
72 76
                                                state=PREHUNTER;
73 77
                                                motor_r_set(0,0);motor_l_set(0,0);
74 78
                                                break;
......
160 164
			if(button2_read()==1 || (tag && time>16)){
161 165
				send_buffer[0]=HUNTER_PREY_ACTION_TAG;
162 166
				send_buffer[1]=id;
163
				wl_basic_send_global_packet( 42, send_buffer, 2 );
167
				send_buffer[2]=id;
168
				wl_basic_send_global_packet( 42, send_buffer, 3 );
164 169
				rtc_reset();
165 170
				state=WAITING;
166 171
				orb1_set_color(PURPLE);

Also available in: Unified diff