root / branches / autonomous_recharging / code / projects / autonomous_recharging / dragonfly / wl_recharge_group.c @ 120
History | View | Annotate | Download (11.3 KB)
1 |
#include "wl_recharge_group.h" |
---|---|
2 |
|
3 |
#include <wireless.h> |
4 |
#include <wl_defs.h> |
5 |
|
6 |
#include <serial.h> |
7 |
|
8 |
#include "recharge_defs.h" |
9 |
|
10 |
// Function prototypes
|
11 |
void wl_recharge_timeout_handler(void); |
12 |
void wl_recharge_response_handler(int frame, int received); |
13 |
void wl_recharge_handle_receive(char type, int source, unsigned char* packet, |
14 |
int length);
|
15 |
void wl_recharge_cleanup(void); |
16 |
|
17 |
// messgae handlers
|
18 |
void wl_recharge_available_station(int source); |
19 |
void wl_recharge_request_accepted(int source); |
20 |
void wl_recharge_verify(int source); |
21 |
void wl_recharge_cancel(int source); |
22 |
void wl_recharge_seeking(int source); |
23 |
void wl_recharge_docked(int source); |
24 |
|
25 |
// sending packets
|
26 |
void wl_recharge_send_poll(void); |
27 |
void wl_recharge_send_request(int dest); |
28 |
void wl_recharge_send_verify(int dest); |
29 |
void wl_recharge_send_cancel(int dest); |
30 |
void wl_recharge_send_seeking(int dest); |
31 |
void wl_recharge_send_docked(int dest); |
32 |
|
33 |
// the handler
|
34 |
PacketGroupHandler wl_recharge_handler = |
35 |
{WL_RECHARGE_GROUP, wl_recharge_timeout_handler, |
36 |
wl_recharge_response_handler, wl_recharge_handle_receive, |
37 |
wl_recharge_cleanup}; |
38 |
|
39 |
//timing
|
40 |
//timer ticks between polls
|
41 |
#define POLL_DELAY 4 |
42 |
#define REQUEST_DELAY 6 |
43 |
#define VERIFY_DELAY 32 |
44 |
#define VERIFY_FREQUENCY 11 |
45 |
|
46 |
//frames
|
47 |
#define REQUEST_FRAME 1 |
48 |
#define VERIFY_FRAME 2 |
49 |
#define CANCEL_FRAME 3 |
50 |
#define SEEKING_FRAME 4 |
51 |
#define DOCKED_FRAME 5 |
52 |
|
53 |
//global varaibles
|
54 |
//state variables
|
55 |
int recharge_state = NOT_RECHARGING;
|
56 |
int station = -1; |
57 |
|
58 |
//counter variables
|
59 |
int pollCount = 0; |
60 |
int requestCount = 0; |
61 |
int verifyCount = 0; |
62 |
|
63 |
|
64 |
/**
|
65 |
* Register this packet group with the wireless library.
|
66 |
* This function must be called before any other wl_recharge
|
67 |
* function. Also, wl_token_ring_register must be called
|
68 |
* before this function.
|
69 |
*
|
70 |
* @see wl_recharge_unregister
|
71 |
**/
|
72 |
void wl_recharge_register(void) |
73 |
{ |
74 |
wl_register_packet_group(&wl_recharge_handler); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* Called to unregister this packet group with the wireless
|
79 |
* library.
|
80 |
*
|
81 |
* @see wl_recharge_register
|
82 |
**/
|
83 |
void wl_recharge_unregister(void) |
84 |
{ |
85 |
wl_unregister_packet_group(&wl_recharge_handler); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Begin the procedure to charge.
|
90 |
**/
|
91 |
void wl_recharge_begin(void) |
92 |
{ |
93 |
RECHARGE_DEBUG_PRINT("Beginning to recharge.\n");
|
94 |
recharge_state = POLLING; |
95 |
station = -1;
|
96 |
pollCount = 0;
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* Begin the procedure to stop charging. If we are currently docked,
|
101 |
* we wait for departureDelay before alerting the station of our
|
102 |
* departure. This is so that we have time to leave.
|
103 |
**/
|
104 |
void wl_recharge_stop()
|
105 |
{ |
106 |
recharge_state = NOT_RECHARGING; |
107 |
} |
108 |
|
109 |
/**
|
110 |
* Call when the robot has docked with the charging station.
|
111 |
* This changes the state to DOCKED.
|
112 |
**/
|
113 |
void wl_recharge_dock(void) |
114 |
{ |
115 |
if (recharge_state != SEEKING)
|
116 |
{ |
117 |
RECHARGE_DEBUG_PRINT("Unexpected docking occurred.\n");
|
118 |
return;
|
119 |
} |
120 |
|
121 |
recharge_state = DOCKED; |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Call this when the robot has departed from the charging
|
126 |
* station. This changes the state to DEPARTING.
|
127 |
**/
|
128 |
void wl_recharge_depart(void) |
129 |
{ |
130 |
if (recharge_state != DOCKED)
|
131 |
{ |
132 |
RECHARGE_DEBUG_PRINT("Unexpected departure occurred.\n");
|
133 |
return;
|
134 |
} |
135 |
|
136 |
recharge_state = DEPARTING; |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Returns the current state of the robot, as one of the constants
|
141 |
* defined for a state -
|
142 |
* NOT_CHARGING, POLLING, REQUESTING, SEEKING, DOCKED, or DEPARTING.
|
143 |
*
|
144 |
* @return the current state of the robot
|
145 |
**/
|
146 |
int wl_recharge_get_state(void) |
147 |
{ |
148 |
return recharge_state;
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* Returns the station we are currently homing to, or -1
|
153 |
* if no such station exists.
|
154 |
*
|
155 |
* @return the 16-bit XBee address of the charging station
|
156 |
**/
|
157 |
int wl_recharge_get_station(void) |
158 |
{ |
159 |
return station;
|
160 |
} |
161 |
|
162 |
|
163 |
/**
|
164 |
* Sends a request polling for charging stations.
|
165 |
**/
|
166 |
void wl_recharge_send_poll(void) |
167 |
{ |
168 |
wl_send_global_packet(WL_RECHARGE_GROUP, WL_RECHARGE_POLL_STATIONS, |
169 |
0, 0, 0); |
170 |
} |
171 |
|
172 |
/**
|
173 |
* Sends a request to seek a specific charging station.
|
174 |
*
|
175 |
* @param dest the charging station to seek
|
176 |
**/
|
177 |
void wl_recharge_send_request(int dest) |
178 |
{ |
179 |
wl_send_robot_to_robot_global_packet(WL_RECHARGE_GROUP, WL_RECHARGE_REQUEST, |
180 |
0, 0, dest, REQUEST_FRAME); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* Sends a request to verify what the charging station
|
185 |
* believes we are doing.
|
186 |
*
|
187 |
* @param dest the charging station to check
|
188 |
**/
|
189 |
void wl_recharge_send_verify(int dest) |
190 |
{ |
191 |
wl_send_robot_to_robot_global_packet(WL_RECHARGE_GROUP, WL_RECHARGE_VERIFY, |
192 |
0, 0, dest, VERIFY_FRAME); |
193 |
} |
194 |
|
195 |
/**
|
196 |
* Sends a packet to the charging station that we are
|
197 |
* no longer attempting to charge from it.
|
198 |
**/
|
199 |
void wl_recharge_send_cancel(int dest) |
200 |
{ |
201 |
wl_send_robot_to_robot_global_packet(WL_RECHARGE_GROUP, WL_RECHARGE_CANCEL, |
202 |
0, 0, dest, CANCEL_FRAME); |
203 |
} |
204 |
|
205 |
/**
|
206 |
* Sends a packet to the charging station that we are
|
207 |
* attempting to dock with it.
|
208 |
*
|
209 |
* @param dest the charging station
|
210 |
**/
|
211 |
void wl_recharge_send_seeking(int dest) |
212 |
{ |
213 |
wl_send_robot_to_robot_global_packet(WL_RECHARGE_GROUP, WL_RECHARGE_SEEKING, |
214 |
0, 0, dest, SEEKING_FRAME); |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Sends a packet to the charging station that
|
219 |
* we have docked with it.
|
220 |
*
|
221 |
* @param dest the charging station we have docked with
|
222 |
**/
|
223 |
void wl_recharge_send_docked(int dest) |
224 |
{ |
225 |
wl_send_robot_to_robot_global_packet(WL_RECHARGE_GROUP, WL_RECHARGE_DOCKED, |
226 |
0, 0, dest, DOCKED_FRAME); |
227 |
} |
228 |
|
229 |
/**
|
230 |
* Called when this packet is unregistered with the wireless library.
|
231 |
**/
|
232 |
void wl_recharge_cleanup(void) |
233 |
{ |
234 |
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Called when the timer goes off in the wireless library.
|
239 |
**/
|
240 |
void wl_recharge_timeout_handler(void) |
241 |
{ |
242 |
switch (recharge_state)
|
243 |
{ |
244 |
case POLLING:
|
245 |
if (pollCount <= 0) |
246 |
{ |
247 |
wl_recharge_send_poll(); |
248 |
pollCount = POLL_DELAY + 1;
|
249 |
} |
250 |
pollCount--; |
251 |
break;
|
252 |
|
253 |
case REQUESTING:
|
254 |
if (requestCount <= 0) |
255 |
{ |
256 |
RECHARGE_DEBUG_PRINT("Station timed out ");
|
257 |
RECHARGE_DEBUG_PRINT("on request.\n");
|
258 |
//request failed
|
259 |
station = -1;
|
260 |
recharge_state = POLLING; |
261 |
break;
|
262 |
} |
263 |
requestCount--; |
264 |
break;
|
265 |
|
266 |
case SEEKING:
|
267 |
if (verifyCount <= 0) |
268 |
{ |
269 |
RECHARGE_DEBUG_PRINT("Station timed out ");
|
270 |
RECHARGE_DEBUG_PRINT("on seeking verify.\n");
|
271 |
//station is down
|
272 |
station = -1;
|
273 |
recharge_state = POLLING; |
274 |
break;
|
275 |
} |
276 |
if (verifyCount % VERIFY_FREQUENCY == 0 && verifyCount != VERIFY_DELAY) |
277 |
wl_recharge_send_verify(station); |
278 |
verifyCount--; |
279 |
break;
|
280 |
|
281 |
case DEPARTING:
|
282 |
break;
|
283 |
} |
284 |
} |
285 |
|
286 |
/**
|
287 |
* Called when a response is received regarding a packet that was sent.
|
288 |
*
|
289 |
* @param frame the frame number for the packet
|
290 |
* @param received true if the packet was received, false otherwise
|
291 |
**/
|
292 |
void wl_recharge_response_handler(int frame, int received) |
293 |
{ |
294 |
if (received)
|
295 |
return;
|
296 |
if (!received)
|
297 |
{ |
298 |
RECHARGE_DEBUG_PRINT("Sent packet not recieved.\n");
|
299 |
switch (frame)
|
300 |
{ |
301 |
case REQUEST_FRAME:
|
302 |
break;
|
303 |
case VERIFY_FRAME:
|
304 |
break;
|
305 |
case CANCEL_FRAME:
|
306 |
case SEEKING_FRAME:
|
307 |
case DOCKED_FRAME:
|
308 |
break;
|
309 |
} |
310 |
} |
311 |
} |
312 |
|
313 |
/**
|
314 |
* Handles receiving an error packet.
|
315 |
*
|
316 |
* @param type the packet type
|
317 |
* @param source the 16-bit address of the packet's sender
|
318 |
* @param packet the packet data
|
319 |
* @param length the length in bytes of the packet
|
320 |
**/
|
321 |
void wl_recharge_handle_receive(char type, int source, unsigned char* packet, |
322 |
int length)
|
323 |
{ |
324 |
switch (type)
|
325 |
{ |
326 |
case WL_RECHARGE_POLL_STATIONS:
|
327 |
//since we aren't a station, we ignore this packet
|
328 |
break;
|
329 |
case WL_RECHARGE_STATION_AVAILABLE:
|
330 |
wl_recharge_available_station(source); |
331 |
break;
|
332 |
case WL_RECHARGE_REQUEST:
|
333 |
RECHARGE_DEBUG_PRINT("Only stations should receive WL_RECHARGE_REQUEST.\n");
|
334 |
break;
|
335 |
case WL_RECHARGE_REQUEST_ACCEPT:
|
336 |
wl_recharge_request_accepted(source); |
337 |
break;
|
338 |
case WL_RECHARGE_VERIFY:
|
339 |
wl_recharge_verify(source); |
340 |
break;
|
341 |
case WL_RECHARGE_CANCEL:
|
342 |
wl_recharge_cancel(source); |
343 |
break;
|
344 |
case WL_RECHARGE_SEEKING:
|
345 |
wl_recharge_seeking(source); |
346 |
break;
|
347 |
case WL_RECHARGE_DOCKED:
|
348 |
wl_recharge_docked(source); |
349 |
break;
|
350 |
default:
|
351 |
RECHARGE_DEBUG_PRINT("Error packet of unknown type received.\n");
|
352 |
break;
|
353 |
} |
354 |
} |
355 |
|
356 |
/**
|
357 |
* Called when we receive a packet alerting us that
|
358 |
* a station is available for charging.
|
359 |
*
|
360 |
* @param source the station which has available bays
|
361 |
**/
|
362 |
void wl_recharge_available_station(int source) |
363 |
{ |
364 |
if (recharge_state != POLLING)
|
365 |
return;
|
366 |
station = source; |
367 |
recharge_state = REQUESTING; |
368 |
requestCount = REQUEST_DELAY; |
369 |
wl_recharge_send_request(station); |
370 |
} |
371 |
|
372 |
/**
|
373 |
* Called when we receive a packet saying our request
|
374 |
* to charge has been accepted.
|
375 |
*
|
376 |
* @param source the station which has accepted our request
|
377 |
**/
|
378 |
void wl_recharge_request_accepted(int source) |
379 |
{ |
380 |
if (recharge_state != REQUESTING)
|
381 |
{ |
382 |
RECHARGE_DEBUG_PRINT("Accepted when we weren't requesting.\n");
|
383 |
return;
|
384 |
} |
385 |
if (station != source)
|
386 |
{ |
387 |
RECHARGE_DEBUG_PRINT("Accepted by a different station.\n");
|
388 |
return;
|
389 |
} |
390 |
|
391 |
recharge_state = SEEKING; |
392 |
verifyCount = VERIFY_DELAY; |
393 |
} |
394 |
|
395 |
/**
|
396 |
* Called when we receive a request to verify our status
|
397 |
* in the docking procedure.
|
398 |
*
|
399 |
* @param source the station verifying our request
|
400 |
**/
|
401 |
void wl_recharge_verify(int source) |
402 |
{ |
403 |
if (source != station)
|
404 |
{ |
405 |
RECHARGE_DEBUG_PRINT("Received verify from unassociated station.\n");
|
406 |
wl_recharge_send_cancel(source); |
407 |
return;
|
408 |
} |
409 |
|
410 |
switch (recharge_state)
|
411 |
{ |
412 |
case SEEKING:
|
413 |
wl_recharge_send_seeking(station); |
414 |
break;
|
415 |
case DOCKED:
|
416 |
wl_recharge_send_docked(station); |
417 |
break;
|
418 |
default:
|
419 |
RECHARGE_DEBUG_PRINT("Cancelled docking procedure.\n");
|
420 |
wl_recharge_send_cancel(station); |
421 |
break;
|
422 |
} |
423 |
} |
424 |
|
425 |
/**
|
426 |
* Called when we receive a packet cancelling our
|
427 |
* docking procedure.
|
428 |
*
|
429 |
* @param source the station cancelling the procedure
|
430 |
**/
|
431 |
void wl_recharge_cancel(int source) |
432 |
{ |
433 |
if (station != source)
|
434 |
{ |
435 |
RECHARGE_DEBUG_PRINT("Received cancel from station we weren't docking with.\n");
|
436 |
return;
|
437 |
} |
438 |
|
439 |
RECHARGE_DEBUG_PRINT("Received cancellation from the charging station.\n");
|
440 |
|
441 |
verifyCount = VERIFY_DELAY; |
442 |
switch (recharge_state)
|
443 |
{ |
444 |
case REQUESTING:
|
445 |
station = -1;
|
446 |
recharge_state = POLLING; |
447 |
break;
|
448 |
case SEEKING:
|
449 |
station = -1;
|
450 |
recharge_state = POLLING; |
451 |
break;
|
452 |
case DOCKED:
|
453 |
RECHARGE_DEBUG_PRINT("Charging station kicked us out.\n");
|
454 |
wl_recharge_depart(); |
455 |
break;
|
456 |
case DEPARTING:
|
457 |
//ignore, since we are already departing
|
458 |
break;
|
459 |
default:
|
460 |
RECHARGE_DEBUG_PRINT("Received unexpected cancellation.\n");
|
461 |
break;
|
462 |
} |
463 |
} |
464 |
|
465 |
/**
|
466 |
* Called when we receive a packet stating that the
|
467 |
* station believes we are seeking them.
|
468 |
*
|
469 |
* @param source the station we are seeking
|
470 |
**/
|
471 |
void wl_recharge_seeking(int source) |
472 |
{ |
473 |
if (station != source)
|
474 |
{ |
475 |
RECHARGE_DEBUG_PRINT("Received seeking alert from station we aren't seeking.\n");
|
476 |
wl_recharge_send_cancel(source); |
477 |
return;
|
478 |
} |
479 |
|
480 |
verifyCount = VERIFY_DELAY; |
481 |
switch (recharge_state)
|
482 |
{ |
483 |
case SEEKING:
|
484 |
break;
|
485 |
case DOCKED:
|
486 |
case DEPARTING:
|
487 |
wl_recharge_send_docked(station); |
488 |
break;
|
489 |
default:
|
490 |
RECHARGE_DEBUG_PRINT("Received unexpected seeking confirmation.\n");
|
491 |
break;
|
492 |
} |
493 |
} |
494 |
|
495 |
/**
|
496 |
* Called when we receive a packet stating that
|
497 |
* we are docked with a station.
|
498 |
*
|
499 |
* @param source the station we are docked with
|
500 |
**/
|
501 |
void wl_recharge_docked(int source) |
502 |
{ |
503 |
if (station != source)
|
504 |
{ |
505 |
RECHARGE_DEBUG_PRINT("Unknown station believes we are docked.\n");
|
506 |
wl_recharge_send_cancel(source); |
507 |
return;
|
508 |
} |
509 |
|
510 |
verifyCount = VERIFY_DELAY; |
511 |
switch (recharge_state)
|
512 |
{ |
513 |
case DOCKED:
|
514 |
case DEPARTING:
|
515 |
break;
|
516 |
case SEEKING:
|
517 |
//not good - we can't immediately proceed to seeking again
|
518 |
//we will leave the station and repoll it
|
519 |
RECHARGE_DEBUG_PRINT("Seeking, but station believes we are docked. ");
|
520 |
RECHARGE_DEBUG_PRINT("Leaving station and repolling.\n");
|
521 |
wl_recharge_send_cancel(source); |
522 |
wl_recharge_begin(); |
523 |
break;
|
524 |
default:
|
525 |
RECHARGE_DEBUG_PRINT("Unexpected docking verification received.\n");
|
526 |
break;
|
527 |
} |
528 |
} |
529 |
|