Revision 1431 trunk/code/projects/hunter_prey/main.c

main.c (revision 1431)
2 2
#include <wireless.h>
3 3
#include <wl_basic.h>
4 4
#include "smart_run_around_fsm.h"
5
#include "hunter_prey.h"
5 6

  
6 7
#define CHAN 0xF
7
#define TAG_TIME 2
8
#define TAG_RANGE 200
9 8
#define TAG_PAUSE 2000
10 9

  
11 10
#define ROBOT_HUNTER 0
......
20 19
volatile uint8_t tagger;
21 20
uint8_t id;
22 21

  
23
void test_bom() {
22
void test_bom(void) {
24 23
  int i;
25 24
  int val;
26 25

  
......
74 73

  
75 74
  int frontRange;
76 75
  uint8_t angle;
77
  uint8_t i;
78
  int onTarget = 0;
79
  char packet[2];
80
  uint8_t bom = 0;
76
  unsigned char packet[2];
81 77
  
82 78
  //intialize the robot and wireless
83 79
  dragonfly_init(ALL_ON);
......
125 121
      usb_puts("sending ACK to ");
126 122
      usb_puti(tagger);
127 123
      usb_putc('\n');
128
      packet[0] = 'A';
124
      packet[0] = HUNTER_PREY_ACTION_ACK;
129 125
      packet[1] = tagger;
130
      wl_basic_send_global_packet (0, packet, 2);
126
      wl_basic_send_global_packet (0, &packet, 2);
131 127
      usb_puts("sent\n");
132 128
      robotState = ROBOT_HUNTER; //no longer prey
133 129
    }
......
155 151

  
156 152
      chase(angle);
157 153

  
154
      //debugging to determine tagging conditions
158 155
      if(angle < 7 && angle > 1) {
159 156
	if(color1 != RED) {
160 157
	  color1 = RED;
......
172 169
      usb_puti(frontRange);
173 170
      usb_putc('\n');
174 171

  
175
      if(frontRange > 0 && frontRange < TAG_RANGE) {
176
	if(color2 != RED) {
177
	  color2 = RED;
178
	  orb2_set_color(RED);
179
	}
180
      }
181
      else {
182
	if(color2 != BLUE) {
183
	  color2 = BLUE;
184
	  orb2_set_color(BLUE);
185
	}
186
      }
172
/*       if(frontRange > 0 && frontRange < TAG_RANGE) { */
173
/* 	if(color2 != RED) { */
174
/* 	  color2 = RED; */
175
/* 	  orb2_set_color(RED); */
176
/* 	} */
177
/*       } */
178
/*       else { */
179
/* 	if(color2 != BLUE) { */
180
/* 	  color2 = BLUE; */
181
/* 	  orb2_set_color(BLUE); */
182
/* 	} */
183
/*       } */
187 184

  
188 185

  
189 186

  
190
      if(angle < 7 && angle > 1 && frontRange > 0 && frontRange < TAG_RANGE) {
191
	if(onTarget == 0) {
192
	  onTarget = TAG_TIME;
193
	  if(color1 != GREEN) {
194
	    color1 = GREEN;
195
	    color2 = GREEN;
196
	    orb_set_color(GREEN);
197
	  }
198
	  usb_puts("On target!\n");
199
	}
200
	else {
201
	  if(--onTarget <= 0) {
202
	    orb_set_color(YELLOW);
203
	    color1 = YELLOW;
204
	    color2 = YELLOW;
205
	    usb_puts("TAG!\n");
206
	    tagAck = 0;
207
	    packet[0] = 'T';
208
	    packet[1] = id;
209
	    wl_basic_send_global_packet (0, packet, 2);
210
	    move(0,0);
211
	    while(!tagAck)
212
	      wl_do();
187
      if(hunter_prey_tagged(angle, frontRange)) {
188
	orb_set_color(YELLOW);
189
	color1 = YELLOW;
190
	color2 = YELLOW;
191
	usb_puts("TAG!\n");
192
	tagAck = 0;
193
	packet[0] = HUNTER_PREY_ACTION_TAG;
194
	packet[1] = id;
195
	wl_basic_send_global_packet (0, &packet, 2);
196
	move(0,0);
197
	while(!tagAck)
198
	  wl_do();
213 199

  
214
	    if(tagAck == id) { //if the ack was for us
215
	      usb_puts("ACK!\n");
200
	if(tagAck == id) { //if the ack was for us
201
	  usb_puts("ACK!\n");
216 202
	      
217
	      move(-230,0); //back up to give the new prey some room
218
	      delay_ms(TAG_PAUSE/2);
219
	      move(0,0);
203
	  move(-230,0); //back up to give the new prey some room
204
	  delay_ms(TAG_PAUSE/2);
205
	  move(0,0);
220 206

  
221
	      robotState = ROBOT_PREY;
222
	      bom_on();
223
	    }
224
	    else {
225
	      usb_puts("ACK failed!\n");
226
	    }
227
	  }
207
	  robotState = ROBOT_PREY;
208
	  bom_on();
228 209
	}
210
	else {
211
	  usb_puts("ACK failed!\n");
212
	}
229 213
      }
230
      else{
231
	//don't reset onTarget because the robot got too close
232
	if(frontRange > 0)
233
	  onTarget = 0;
234
      }
235

  
236 214
    }
237 215

  
238 216
    wl_do();
239 217
  }
240

  
218
  
241 219
  return 0;
242 220
}
243 221

  
244 222
void packet_receive(char type, int source, unsigned char* packet, int length)
245 223
{
224

  
246 225
  if(type==0 && length>=2){
247
    if(packet[0] == 'T') {
226
    if(packet[0] == HUNTER_PREY_ACTION_TAG) {
248 227
      if(robotState == ROBOT_PREY) {
249 228
	robotState = ROBOT_TAGGED;
250 229
	bom_off();
......
257 236
	usb_puts("tagged, but I'm not it!\n");
258 237
      }
259 238
    }
260
    else if(packet[0] == 'A' && robotState == ROBOT_HUNTER) {
239
    else if(packet[0] == HUNTER_PREY_ACTION_ACK && robotState == ROBOT_HUNTER) {
261 240
      tagAck = packet[1];
262 241
    }
263 242
    else {
......
276 255
  }
277 256
}
278 257

  
258
#define HP_BUF_IDX_ACTION 0

Also available in: Unified diff