Project

General

Profile

Revision 1786

Cleaned up joystickPlayer folder.

View differences:

trunk/code/behaviors/hunter_prey/joystickPlayer/main.c
1
#include <dragonfly_lib.h>
2
#include <wireless.h>
3
#include <wl_basic.h>
4
#include "hunter_prey.h"
5

  
6
/* Command Types */
7
#define CNTL_FORWARD 'f'
8
#define CNTL_STOP    's'
9
#define CNTL_BACK    'b'
10
#define CNTL_LEFT    'l'
11
#define CNTL_RIGHT   'r'
12
#define CNTL_BUZZ0   't'
13
#define CNTL_BUZZ1   'u'
14

  
15
#define WL_CNTL_GROUP 4
16

  
17
#define HUNTER 1
18
#define HUNTER_TAGGING 5
19
#define WAIT 2
20
#define PREY 3
21
#define DO_TAG 4
22

  
23
#define WL_CHANNEL 24
24
#define WAIT_DELAY_MS 2000
25

  
26
void colonet_receive (char type, int source, unsigned char* packet, int length);
27

  
28
PacketGroupHandler colonetPacketHandler = 
29
{
30
    WL_CNTL_GROUP,
31
    NULL,
32
    NULL,
33
    &colonet_receive,
34
    NULL
35
};
36

  
37
int state;
38

  
39
unsigned char* packet_data;
40
int data_length;
41

  
42
int main (void) {
43
    char send_buffer[2];
44
    int robotID;
45
    int curTime = 0, oldTime = 0;
46

  
47
    dragonfly_init(ALL_ON);
48
    xbee_init();
49
    wl_basic_init_default();
50

  
51
    wl_set_channel(WL_CHANNEL);
52

  
53
    // register handler for colonet stuff
54
    wl_register_packet_group(&colonetPacketHandler);
55

  
56
    robotID = get_robotid();
57
    usb_puts("robot ");
58
    usb_puti(robotID);
59
    usb_puts("entering main loop\n");
60

  
61
    state = HUNTER;
62
  
63
    while (1) {
64
	switch(state) {
65
	case HUNTER_TAGGING:
66
	    curTime = rtc_get();
67
					
68
	    if (packet_data && data_length == 2
69
		&& packet_data[0] == HUNTER_PREY_ACTION_ACK) {
70
		/* Check if we've received a new wireless packet */
71

  
72
		if (packet_data[1] == robotID) {
73
		    /* We've been ACKed, so we can now become the prey */
74
		    usb_puts("got ACK, going to PREY\n");
75
		    bom_on();
76
		    state = PREY;
77
		} else {
78
		    /* If we get an ACK with a different robotID, then someone beat us
79
		     * to the TAG, so we must wait */
80
		    usb_puts("someone else got ACK, going to WAIT\n");
81
		    state = WAIT;
82
		}
83

  
84
	    }
85
	    else if (curTime - oldTime > 1) {
86
		/* If 1 second has ellapsed, return to normal hunting state (we can
87
		 * TAG again now) */
88
		usb_puts("giving up on tag.. going to HUNTER\n");
89
		state = HUNTER;
90
	    }
91
	    else if (oldTime > curTime) {
92
		/* If for some reason the timer overflows, or the wireless library
93
		 * (which is also using the same timer) resets the timer,
94
		 * reinitialize the timer so that we don't wait too long for the
95
		 * timer to catch back up. */
96
		oldTime = curTime;
97
	    }
98
	    break;
99

  
100
	case HUNTER:
101
	    orbs_set_color(RED, RED);
102
	    if (packet_data && data_length == 2
103
		&& packet_data[0] == HUNTER_PREY_ACTION_ACK) {
104
		/* If we've received an ACK, we need to wait */
105
		usb_puts("going to WAIT\n");
106
		state = WAIT;
107
	    }
108
	    break;
109
	case PREY:
110
	    orbs_set_color(GREEN, GREEN);
111
	    if (packet_data && data_length == 2
112
		&& packet_data[0] == HUNTER_PREY_ACTION_ACK) {
113
		/* If we've received an ACK, we need to wait */
114
		usb_puts("SOMEONE ACK'd, going to WAIT\n");
115
		bom_off();
116
		state = WAIT;
117
	    }
118
	    else if (packet_data && data_length == 2
119
		     && packet_data[0] == HUNTER_PREY_ACTION_TAG) {
120
		/* Check if we've received a TAG. If so then send an
121
		 * ACK back */
122
		usb_puts("got TAG, sending ACK\n");
123
		send_buffer[0] = HUNTER_PREY_ACTION_ACK;
124
		send_buffer[1] = packet_data[1];
125
		wl_basic_send_global_packet(42, send_buffer, 2);
126
		bom_off();
127
		state = WAIT;
128
	    }
129
	    break;
130
	case WAIT:
131
	    motor1_set(0,0);
132
	    motor2_set(0,0);
133
	    orbs_set_color(BLUE, BLUE);
134
	    delay_ms(WAIT_DELAY_MS);
135
	    state = HUNTER;
136
	    break;
137
	case DO_TAG:
138
	    if (packet_data && data_length == 2
139
		&& packet_data[0] == HUNTER_PREY_ACTION_ACK) {
140
		/* If we've received an ACK, then someone beat us to
141
		 * the TAG and we need to wait. */
142
		usb_puts("someone beat us to TAG, going to WAIT\n");
143
		state = WAIT;
144
	    } else {
145
		/* Prepare and send the TAG packet */
146
		send_buffer[0] = HUNTER_PREY_ACTION_TAG;
147
		send_buffer[1] = robotID;
148
		wl_basic_send_global_packet(42, send_buffer, 2);
149
		usb_puts("sent TAG, going to HUNTER_TAGGING\n");
150
		oldTime = rtc_get();
151
		state = HUNTER_TAGGING;
152
	    }
153
	    break;
154

  
155
	default:
156
	    state = HUNTER;
157
	    break;
158
	}
159

  
160
	packet_data = wl_basic_do_default(&data_length);
161
    	delay_ms(50);
162
    }
163
}
164

  
165
/**
166
 *
167
 * This packet handler accepts colonet packets from discrete_control
168
 * in the colonet/robot/joystick folder 
169
 */
170
void colonet_receive (char type, int source, unsigned char* packet, int length) {
171
    int times;
172
    int frontIR = 0;
173
    int maxBOM = 0;
174
    static int last = 0;
175

  
176
    /* if we are in WAIT, don't move or tag */
177
    if(state == WAIT)
178
	return;
179

  
180
    /* usb_puti(type); usb_putc('\n'); */
181

  
182
    /* // If the robot is changing direction, stop it for a bit first */
183
    /* if (last != type) { */
184
    /* 	if( (last == CNTL_FORWARD || last == CNTL_BACK || last == CNTL_LEFT || last == CNTL_RIGHT) && */
185
    /* 	    (type == CNTL_FORWARD || type == CNTL_BACK || type == CNTL_LEFT || type == CNTL_RIGHT)) { */
186
    /* 	    usb_puts("x\n"); */
187
    /* 	    motor1_set(0,0); */
188
    /* 	    motor2_set(0,0); */
189
    /* 	    delay_ms(100); */
190
    /* 	} */
191
    /* } */
192

  
193
    /* last = type; */
194

  
195
    switch (type) {
196
    case CNTL_FORWARD:
197
	motor1_set(1, 250);
198
	motor2_set(1, 250);
199
	break;
200
    case CNTL_STOP:
201
	motor1_set(0, 0);
202
	motor2_set(0, 0);
203
	delay_ms(220); // 100:200
204
	break;
205
    case CNTL_BACK:
206
	motor1_set(0, 250);
207
	motor2_set(0, 250);
208
	break;
209
    case CNTL_LEFT:
210
	motor1_set(0, 250);
211
	motor2_set(1, 250);
212
	break;
213
    case CNTL_RIGHT:
214
	motor1_set(1, 250);
215
	motor2_set(0, 250);
216
	break;
217
    /* front trigger */
218
    case CNTL_BUZZ0: 
219
	if (state == HUNTER) {
220
	    usb_puts("trigger\n");
221
	    buzzer_chirp(50, 440);
222
	    /* try to tag 10 times in a row */
223
	    /* NOTE: this needs to be at least TAG_TIME times (from
224
	     * hunter_prey.c */
225
	    for(times = 0; times < 10; times++) {
226
		usb_puti(times);
227

  
228
		bom_refresh(BOM_ALL);
229
		frontIR = range_read_distance(IR2);
230
		maxBOM = get_max_bom();
231
		usb_puts(": IR=");
232
		usb_puti(frontIR);
233
		usb_puts(" BOM=");
234
		usb_puti(maxBOM);
235
		usb_putc('\n');
236
		if (hunter_prey_tagged(maxBOM, frontIR)) {
237
		    usb_puts("tagged!\n");
238
		    buzzer_chirp(100, 660);
239
		    state = DO_TAG;
240
		    break;
241
		}
242
		else {
243
		    buzzer_chirp(10, 349);
244
		}
245
	    }
246
	}
247
	break;
248
    default:
249
	motor1_set(0, 0);
250
	motor2_set(0, 0);
251
	break;
252
    }
253
}
254

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

  
4
#define TAG_TIME 3
5
#define TAG_RANGE 250
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 either value is -1, just return */
23
  if(max_bom == -1 || frontRange == -1) {
24
    return 0;
25
  }
26

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

  
46
  return 0;
47

  
48
}
trunk/code/behaviors/hunter_prey/joystickPlayer/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
trunk/code/behaviors/hunter_prey/joystickPlayer/README.txt
2 2
running hunter_prey It will be intended to work with hunter_prey/john,
3 3
so use that for the best results.
4 4

  
5
The subfolder joystickControl has the code that goes on the board
5
The subfolder "joystick" has the code that goes on the board
6 6
connected to the joystick. the Makefile in the subfolder needs some
7 7
colonet stuff so it is the old version, meaning you will have to make
8 8
and make dist in the library folders if it doesn't compile, although
9
doing a make in joystickPlayer first should take care of that.
9
doing a make in "robot" first should take care of that.

Also available in: Unified diff