root / branches / autonomous_recharging / code / projects / autonomous_recharging / charging_station / wl_token_ring.c @ 743
History | View | Annotate | Download (24.6 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
/**
|
27 |
* @file wl_token_ring.c
|
28 |
* @brief Token Ring Implementation
|
29 |
*
|
30 |
* Implementation of the token ring packet group.
|
31 |
*
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club
|
33 |
**/
|
34 |
|
35 |
#include <wl_token_ring.h> |
36 |
|
37 |
#include <stdlib.h> |
38 |
#include <stdio.h> |
39 |
|
40 |
#include <wl_defs.h> |
41 |
#include <wireless.h> |
42 |
#include <sensor_matrix.h> |
43 |
|
44 |
#ifdef ROBOT
|
45 |
#ifndef FIREFLY
|
46 |
#include <bom.h> |
47 |
#endif
|
48 |
#include <time.h> |
49 |
#endif
|
50 |
|
51 |
//TODO: why is this in both this file and sensor_matrix.c? If it is needed in both places,
|
52 |
// put it in sensor_matrix.h. This file already includes sensor_matrix.h
|
53 |
#define DEFAULT_SENSOR_MATRIX_SIZE 20 |
54 |
|
55 |
/*Ring States*/
|
56 |
|
57 |
#define NONMEMBER 0 |
58 |
#define MEMBER 1 |
59 |
#define JOINING 2 |
60 |
#define ACCEPTED 3 |
61 |
#define LEAVING 4 |
62 |
|
63 |
/*Frame Types*/
|
64 |
#define TOKEN_JOIN_ACCEPT_FRAME 1 |
65 |
#define WL_TOKEN_PASS_FRAME 2 |
66 |
|
67 |
/*Function Prototypes*/
|
68 |
|
69 |
/*Wireless Library Prototypes*/
|
70 |
static void wl_token_ring_timeout_handler(void); |
71 |
static void wl_token_ring_response_handler(int frame, int received); |
72 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length); |
73 |
static void wl_token_ring_cleanup(void); |
74 |
|
75 |
/*Helper Functions*/
|
76 |
static int wl_token_pass_token(void); |
77 |
static int get_token_distance(int robot1, int robot2); |
78 |
static void wl_token_get_token(void); |
79 |
|
80 |
/*Packet Handling Routines*/
|
81 |
static void wl_token_pass_receive(int source); |
82 |
static void wl_token_sensor_matrix_receive(int source, unsigned char* sensorData, int sensorDataLength); |
83 |
static void wl_token_bom_on_receive(int source); |
84 |
static void wl_token_join_receive(int source); |
85 |
static void wl_token_join_accept_receive(int source); |
86 |
|
87 |
/*Global Variables*/
|
88 |
|
89 |
//the robot we are waiting to say it has received the token. -1 if unspecified
|
90 |
static int wl_token_next_robot = -1; |
91 |
|
92 |
//true if the robot should be in the token ring, 0 otherwise
|
93 |
static int ringState = NONMEMBER; |
94 |
//the id of the robot who accepted us into the token ring, only used in ACCEPTED state
|
95 |
static int acceptor = -1; |
96 |
//id of the robot we are accepting
|
97 |
static int accepted = -1; |
98 |
|
99 |
//the counter for when we assume a robot is dead
|
100 |
static int deathDelay = -1; |
101 |
//the counter for joining, before we form our own token ring
|
102 |
static int joinDelay = -1; |
103 |
|
104 |
//current robot to check in the iterator
|
105 |
static int iteratorCount = 0; |
106 |
|
107 |
// the amount of time a robot has had its BOM on for
|
108 |
static int bom_on_count = 0; |
109 |
|
110 |
static void do_nothing(void) {} |
111 |
static int get_nothing(void) {return -1;} |
112 |
|
113 |
#ifdef ROBOT
|
114 |
#ifndef FIREFLY
|
115 |
static void (*bom_on_function) (void) = bom_on; |
116 |
static void (*bom_off_function) (void) = bom_off; |
117 |
static int (*get_max_bom_function) (void) = get_max_bom; |
118 |
#else
|
119 |
static void (*bom_on_function) (void) = do_nothing; |
120 |
static void (*bom_off_function) (void) = do_nothing; |
121 |
static int (*get_max_bom_function) (void) = get_nothing; |
122 |
#endif
|
123 |
#else
|
124 |
static void (*bom_on_function) (void) = do_nothing; |
125 |
static void (*bom_off_function) (void) = do_nothing; |
126 |
static int (*get_max_bom_function) (void) = get_nothing; |
127 |
#endif
|
128 |
|
129 |
static PacketGroupHandler wl_token_ring_handler =
|
130 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler, |
131 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
132 |
wl_token_ring_cleanup}; |
133 |
|
134 |
/**
|
135 |
* Causes the robot to join an existing token ring, or create one
|
136 |
* if no token ring exists. The token ring uses global and robot to robot
|
137 |
* packets, and does not rely on any PAN.
|
138 |
**/
|
139 |
int wl_token_ring_join()
|
140 |
{ |
141 |
WL_DEBUG_PRINT("Joining the token ring.\r\n");
|
142 |
|
143 |
ringState = JOINING; |
144 |
joinDelay = DEATH_DELAY * 2;
|
145 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) { |
146 |
return -1; |
147 |
} |
148 |
|
149 |
return 0; |
150 |
} |
151 |
|
152 |
/**
|
153 |
* Causes the robot to leave the token ring. The robot stops
|
154 |
* alerting others of its location, but continues storing the
|
155 |
* locations of other robots.
|
156 |
**/
|
157 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
158 |
// it reduces code size or not should be done to be sure.
|
159 |
void wl_token_ring_leave()
|
160 |
{ |
161 |
ringState = LEAVING; |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Initialize the token ring packet group and register it with the
|
166 |
* wireless library. The robot will not join a token ring.
|
167 |
**/
|
168 |
int wl_token_ring_register()
|
169 |
{ |
170 |
if (wl_get_xbee_id() > 0xFF) |
171 |
{ |
172 |
//Note: if this becomes an issue (unlikely), we could limit sensor information
|
173 |
//to half a byte and use 12 bits for the id
|
174 |
WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is ");
|
175 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
176 |
WL_DEBUG_PRINT(".\r\n");
|
177 |
return -1; |
178 |
} |
179 |
|
180 |
sensor_matrix_create(); |
181 |
//add ourselves to the sensor matrix
|
182 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 0);
|
183 |
|
184 |
wl_register_packet_group(&wl_token_ring_handler); |
185 |
|
186 |
return 0; |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Removes the packet group from the wireless library.
|
191 |
**/
|
192 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
193 |
// it reduces code size or not should be done to be sure.
|
194 |
void wl_token_ring_unregister()
|
195 |
{ |
196 |
wl_unregister_packet_group(&wl_token_ring_handler); |
197 |
} |
198 |
|
199 |
/**
|
200 |
* Sets the functions that are called when the BOM ought to be
|
201 |
* turned on or off. This could be used for things such as
|
202 |
* charging stations, which have multiple BOMs.
|
203 |
*
|
204 |
* @param on_function the function to be called when the BOM
|
205 |
* should be turned on
|
206 |
* @param off_function the function to be called when the BOM
|
207 |
* should be turned off
|
208 |
* @param max_bom_function the function to be called when a
|
209 |
* measurement of the maximum BOM reading is needed.
|
210 |
**/
|
211 |
void wl_token_ring_set_bom_functions(void (*on_function) (void), |
212 |
void (*off_function) (void), int (*max_bom_function) (void)) |
213 |
{ |
214 |
bom_on_function = on_function; |
215 |
bom_off_function = off_function; |
216 |
get_max_bom_function = max_bom_function; |
217 |
} |
218 |
|
219 |
/**
|
220 |
* Called to cleanup the token ring packet group.
|
221 |
**/
|
222 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
223 |
// it reduces code size or not should be done to be sure.
|
224 |
static void wl_token_ring_cleanup() |
225 |
{ |
226 |
} |
227 |
|
228 |
/**
|
229 |
* Called approximately every quarter second by the wireless library.
|
230 |
**/
|
231 |
static void wl_token_ring_timeout_handler() |
232 |
{ |
233 |
//someone is not responding, assume they are dead
|
234 |
if (deathDelay == 0) |
235 |
{ |
236 |
//pass the token to the next robot if we think someone has died
|
237 |
//also, declare that person dead, as long as it isn't us
|
238 |
if (wl_token_next_robot != wl_get_xbee_id())
|
239 |
{ |
240 |
sensor_matrix_set_in_ring(wl_token_next_robot, 0);
|
241 |
WL_DEBUG_PRINT("Robot ");
|
242 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
243 |
WL_DEBUG_PRINT(" has died.\r\n");
|
244 |
wl_token_next_robot = -1;
|
245 |
deathDelay = DEATH_DELAY; |
246 |
} |
247 |
|
248 |
// we may have been dropped from the ring when this is received
|
249 |
if (ringState == MEMBER) {
|
250 |
wl_token_pass_token(); |
251 |
} |
252 |
} |
253 |
|
254 |
//we must start our own token ring, no one is responding to us
|
255 |
if (joinDelay == 0) |
256 |
{ |
257 |
if (sensor_matrix_get_joined() == 0) |
258 |
{ |
259 |
WL_DEBUG_PRINT("Creating our own token ring, no robots seem to exist.\r\n");
|
260 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
|
261 |
ringState = MEMBER; |
262 |
//this will make us pass the token to ourself
|
263 |
//repeatedly, and other robots when they join
|
264 |
deathDelay = DEATH_DELAY; |
265 |
wl_token_next_robot = wl_get_xbee_id(); |
266 |
} |
267 |
else
|
268 |
{ |
269 |
WL_DEBUG_PRINT("Attempting to join the token ring again.\r\n");
|
270 |
//attempt to rejoin with a random delay
|
271 |
wl_token_ring_join(); |
272 |
joinDelay = rand() / (RAND_MAX / JOIN_DELAY) + 1;
|
273 |
} |
274 |
} |
275 |
|
276 |
if (deathDelay >= 0) { |
277 |
deathDelay--; |
278 |
} |
279 |
|
280 |
if (joinDelay >= 0) { |
281 |
joinDelay--; |
282 |
} |
283 |
|
284 |
if (bom_on_count >= 0) { |
285 |
bom_on_count++; |
286 |
} |
287 |
} |
288 |
|
289 |
/**
|
290 |
* Called when the XBee tells us if a packet we sent has been received.
|
291 |
*
|
292 |
* @param frame the frame number assigned when the packet was sent
|
293 |
* @param received 1 if the packet was received, 0 otherwise
|
294 |
**/
|
295 |
static void wl_token_ring_response_handler(int frame, int received) |
296 |
{ |
297 |
if (!received)
|
298 |
{ |
299 |
WL_DEBUG_PRINT("FAILED.\r\n");
|
300 |
} |
301 |
} |
302 |
|
303 |
/**
|
304 |
* Called when we recieve a token ring packet.
|
305 |
* @param type the type of the packet
|
306 |
* @param source the id of the robot who sent the packet
|
307 |
* @param packet the data in the packet
|
308 |
* @param length the length of the packet in bytes
|
309 |
**/
|
310 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length) |
311 |
{ |
312 |
switch (type)
|
313 |
{ |
314 |
case WL_TOKEN_PASS:
|
315 |
wl_token_pass_receive(source); |
316 |
break;
|
317 |
case WL_TOKEN_SENSOR_MATRIX:
|
318 |
wl_token_sensor_matrix_receive(source, packet, length); |
319 |
break;
|
320 |
case WL_TOKEN_BOM_ON:
|
321 |
//add the robot to the sensor matrix if it is not already there
|
322 |
wl_token_bom_on_receive(source); |
323 |
break;
|
324 |
case WL_TOKEN_JOIN:
|
325 |
wl_token_join_receive(source); |
326 |
break;
|
327 |
case WL_TOKEN_JOIN_ACCEPT:
|
328 |
wl_token_join_accept_receive(source); |
329 |
break;
|
330 |
default:
|
331 |
WL_DEBUG_PRINT("Unimplemented token ring packet received.\r\n");
|
332 |
break;
|
333 |
} |
334 |
} |
335 |
|
336 |
/**
|
337 |
* Returns the BOM reading robot source has for robot dest.
|
338 |
*
|
339 |
* @param source the robot that made the BOM reading
|
340 |
* @param dest the robot whose relative location is returned
|
341 |
*
|
342 |
* @return a BOM reading from robot source to robot dest,
|
343 |
* in the range 0-15, or -1 if it is unknown
|
344 |
**/
|
345 |
int wl_token_get_sensor_reading(int source, int dest) |
346 |
{ |
347 |
if (wl_token_is_robot_in_ring(dest) &&
|
348 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) { |
349 |
return sensor_matrix_get_reading(source, dest);
|
350 |
} |
351 |
|
352 |
return -1; |
353 |
} |
354 |
|
355 |
/**
|
356 |
* Returns the BOM reading we have for robot dest.
|
357 |
*
|
358 |
* @param dest the robot whose relative location is returned
|
359 |
*
|
360 |
* @return a BOM reading from us to robot dest, in the range
|
361 |
* 0-15, or -1 if it is unkown
|
362 |
**/
|
363 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
364 |
// it reduces code size or not should be done to be sure.
|
365 |
int wl_token_get_my_sensor_reading(int dest) |
366 |
{ |
367 |
return wl_token_get_sensor_reading(wl_get_xbee_id(), dest);
|
368 |
} |
369 |
|
370 |
|
371 |
/**
|
372 |
* Returns the number of robots in the token ring.
|
373 |
*
|
374 |
* @return the number of robots in the token ring
|
375 |
**/
|
376 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
377 |
// it reduces code size or not should be done to be sure.
|
378 |
int wl_token_get_robots_in_ring(void) |
379 |
{ |
380 |
return sensor_matrix_get_joined();
|
381 |
} |
382 |
|
383 |
/**
|
384 |
* Returns true if the specified robot is in the token ring, false
|
385 |
* otherwise.
|
386 |
*
|
387 |
* @param robot the robot to check for whether it is in the token ring
|
388 |
* @return nonzero if the robot is in the token ring, zero otherwise
|
389 |
**/
|
390 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
391 |
// it reduces code size or not should be done to be sure.
|
392 |
int wl_token_is_robot_in_ring(int robot) |
393 |
{ |
394 |
return sensor_matrix_get_in_ring(robot);
|
395 |
} |
396 |
|
397 |
/**
|
398 |
* Begins iterating through the robots in the token ring.
|
399 |
*
|
400 |
* @see wl_token_iterator_has_next, wl_token_iterator_next
|
401 |
**/
|
402 |
void wl_token_iterator_begin(void) |
403 |
{ |
404 |
int i = 0; |
405 |
//TODO: the compiler may or may not optimize this such that my comment is useless:
|
406 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring
|
407 |
// the overhead of a function call each iteration, call it only once before the loop and store
|
408 |
// the value in a variable and check against that variable in the loop condition
|
409 |
while (!sensor_matrix_get_in_ring(i) && i < sensor_matrix_get_size()) {
|
410 |
i++; |
411 |
} |
412 |
|
413 |
//TODO: if you do the above comment, then you can compare this to the variable also
|
414 |
if (i == sensor_matrix_get_size()) {
|
415 |
i = -1;
|
416 |
} |
417 |
|
418 |
iteratorCount = i; |
419 |
} |
420 |
|
421 |
/**
|
422 |
* Returns true if there are more robots in the token ring
|
423 |
* to iterate through, and false otherwise.
|
424 |
*
|
425 |
* @return nonzero if there are more robots to iterate through,
|
426 |
* zero otherwise
|
427 |
*
|
428 |
* @see wl_token_iterator_begin, wl_token_iterator_next
|
429 |
**/
|
430 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
431 |
// it reduces code size or not should be done to be sure.
|
432 |
int wl_token_iterator_has_next(void) |
433 |
{ |
434 |
return iteratorCount != -1; |
435 |
} |
436 |
|
437 |
/**
|
438 |
* Returns the next robot ID in the token ring.
|
439 |
*
|
440 |
* @return the next robot ID in the token ring, or -1 if none exists
|
441 |
*
|
442 |
* @see wl_token_iterator_begin, wl_token_iterator_has_next
|
443 |
**/
|
444 |
int wl_token_iterator_next(void) |
445 |
{ |
446 |
int result = iteratorCount;
|
447 |
if (result < 0) { |
448 |
return result;
|
449 |
} |
450 |
|
451 |
//TODO: the compiler may or may not optimize this such that my comment is useless:
|
452 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring
|
453 |
// the overhead of a function call each iteration, call it only once before the loop and store
|
454 |
// the value in a variable and check against that variable in the loop condition
|
455 |
iteratorCount++; |
456 |
while (!sensor_matrix_get_in_ring(iteratorCount)
|
457 |
&& iteratorCount < sensor_matrix_get_size()) { |
458 |
iteratorCount++; |
459 |
} |
460 |
|
461 |
//TODO: if you do the above comment, then you can compare this to the variable also
|
462 |
if (iteratorCount == sensor_matrix_get_size()) {
|
463 |
iteratorCount = -1;
|
464 |
} |
465 |
|
466 |
return result;
|
467 |
} |
468 |
|
469 |
/**
|
470 |
* Returns the number of robots currently in the token ring.
|
471 |
*
|
472 |
* @return the number of robots in the token ring
|
473 |
**/
|
474 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
475 |
// it reduces code size or not should be done to be sure.
|
476 |
int wl_token_get_num_robots(void) |
477 |
{ |
478 |
return sensor_matrix_get_joined();
|
479 |
} |
480 |
|
481 |
/**
|
482 |
* Returns the number of robots in the sensor matrix.
|
483 |
*
|
484 |
* @return the number of robots in the sensor matrix
|
485 |
**/
|
486 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if
|
487 |
// it reduces code size or not should be done to be sure.
|
488 |
int wl_token_get_matrix_size(void) |
489 |
{ |
490 |
return sensor_matrix_get_size();
|
491 |
} |
492 |
|
493 |
/**
|
494 |
* This method is called when we receive a token pass packet.
|
495 |
*
|
496 |
* @param source the robot who passed the token to us.
|
497 |
**/
|
498 |
static void wl_token_pass_receive(int source) |
499 |
{ |
500 |
WL_DEBUG_PRINT("Received token from ");
|
501 |
WL_DEBUG_PRINT_INT(source); |
502 |
WL_DEBUG_PRINT(", expected ");
|
503 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
504 |
WL_DEBUG_PRINT(".\n");
|
505 |
// this prevents two tokens from being passed around at a time (second clause is in case we are joining)
|
506 |
if ((source != wl_token_next_robot && wl_get_xbee_id() != wl_token_next_robot) && bom_on_count <= DEATH_DELAY / 2 && |
507 |
ringState != ACCEPTED) |
508 |
{ |
509 |
WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n");
|
510 |
WL_DEBUG_PRINT("There are probably two tokens going around, packet ignored.\n");
|
511 |
return;
|
512 |
} |
513 |
bom_on_count = -1;
|
514 |
deathDelay = -1;
|
515 |
sensor_matrix_set_in_ring(source, 1);
|
516 |
wl_token_get_token(); |
517 |
} |
518 |
|
519 |
/**
|
520 |
* This method is called when we receive a token pass packet.
|
521 |
* @param source is the robot it came from
|
522 |
* @param nextRobot is the robot the token was passed to
|
523 |
* @param sensorData a char with an id followed by a char with the sensor
|
524 |
* reading for that robot, repeated for sensorDataLength bytes
|
525 |
* @param sensorDataLength the length in bytes of sensorData
|
526 |
*/
|
527 |
static void wl_token_sensor_matrix_receive(int source, unsigned char* sensorData, int sensorDataLength) |
528 |
{ |
529 |
int i, j;
|
530 |
char nextRobot;
|
531 |
|
532 |
bom_on_count = -1;
|
533 |
deathDelay = -1;
|
534 |
sensor_matrix_set_in_ring(source, 1);
|
535 |
|
536 |
//with this packet, we are passed the id of the next robot in the ring
|
537 |
//and the sensor matrix, a list of id and sensor reading pairs (two bytes for both)
|
538 |
j = 0;
|
539 |
//TODO: the compiler may or may not optimize this such that my comment is useless:
|
540 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring
|
541 |
// the overhead of a function call each iteration, call it only once before the loop and store
|
542 |
// the value in a variable and check against that variable in the loop condition
|
543 |
for (i = 0; i < sensor_matrix_get_size(); i++) |
544 |
{ |
545 |
if (i == source) {
|
546 |
continue;
|
547 |
} |
548 |
|
549 |
//set the sensor information we receive
|
550 |
if (j < sensorDataLength / 2 && sensorData[2 * j] == i) |
551 |
{ |
552 |
//the robot we were going to accept has already been accepted
|
553 |
if (accepted == i)
|
554 |
{ |
555 |
accepted = -1;
|
556 |
WL_DEBUG_PRINT("Someone accepted the robot we did.\r\n");
|
557 |
} |
558 |
sensor_matrix_set_reading(source, i, |
559 |
sensorData[2 * j + 1]); |
560 |
if (!sensor_matrix_get_in_ring(i))
|
561 |
{ |
562 |
WL_DEBUG_PRINT("Robot ");
|
563 |
WL_DEBUG_PRINT_INT(i); |
564 |
WL_DEBUG_PRINT(" has been added to the sensor matrix of robot ");
|
565 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
566 |
WL_DEBUG_PRINT(" due to a packet from robot ");
|
567 |
WL_DEBUG_PRINT_INT(source); |
568 |
WL_DEBUG_PRINT(".\r\n");
|
569 |
} |
570 |
sensor_matrix_set_in_ring(i, 1);
|
571 |
j++; |
572 |
} |
573 |
else
|
574 |
{ |
575 |
if (sensor_matrix_get_in_ring(i))
|
576 |
{ |
577 |
WL_DEBUG_PRINT("Robot ");
|
578 |
WL_DEBUG_PRINT_INT(i); |
579 |
WL_DEBUG_PRINT(" has been removed from the sensor matrix of robot ");
|
580 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
581 |
WL_DEBUG_PRINT(" due to a packet from robot ");
|
582 |
WL_DEBUG_PRINT_INT(source); |
583 |
WL_DEBUG_PRINT(".\r\n");
|
584 |
sensor_matrix_set_in_ring(i, 0);
|
585 |
} |
586 |
|
587 |
if (i == wl_get_xbee_id() && ringState == MEMBER)
|
588 |
{ |
589 |
ringState = NONMEMBER; |
590 |
wl_token_ring_join(); |
591 |
|
592 |
WL_DEBUG_PRINT("We have been removed from the ring ");
|
593 |
WL_DEBUG_PRINT("and are rejoining.\r\n");
|
594 |
} |
595 |
|
596 |
//the person who accepted us is dead... let's ask again
|
597 |
if (i == acceptor)
|
598 |
{ |
599 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 0);
|
600 |
ringState = NONMEMBER; |
601 |
acceptor = -1;
|
602 |
wl_token_ring_join(); |
603 |
} |
604 |
} |
605 |
} |
606 |
|
607 |
// get the next robot in the token ring
|
608 |
i = source + 1;
|
609 |
while (1) |
610 |
{ |
611 |
if (i == sensor_matrix_get_size()) {
|
612 |
i = 0;
|
613 |
} |
614 |
|
615 |
if (sensor_matrix_get_in_ring(i) || i == source)
|
616 |
{ |
617 |
nextRobot = (char)i;
|
618 |
break;
|
619 |
} |
620 |
|
621 |
i++; |
622 |
} |
623 |
|
624 |
if (nextRobot != wl_get_xbee_id())
|
625 |
wl_token_next_robot = nextRobot; |
626 |
|
627 |
deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY; |
628 |
|
629 |
if (sensor_matrix_get_joined() == 0 && ringState == JOINING) |
630 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME); |
631 |
} |
632 |
|
633 |
/**
|
634 |
* Gets the distance in the token ring between two robots.
|
635 |
*
|
636 |
* @param robot1 the first robot
|
637 |
* @param robot2 the second robot
|
638 |
*
|
639 |
* @return the number of passes before the token is expected
|
640 |
* to reach robot2 from robot1
|
641 |
**/
|
642 |
static int get_token_distance(int robot1, int robot2) |
643 |
{ |
644 |
int curr = robot1 + 1; |
645 |
int count = 1; |
646 |
while (1) |
647 |
{ |
648 |
//TODO: the compiler may or may not optimize this such that my comment is useless:
|
649 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring
|
650 |
// the overhead of a function call each iteration, call it only once before the loop and store
|
651 |
// the value in a variable and check against that variable in the loop condition
|
652 |
if (curr == sensor_matrix_get_size())
|
653 |
curr = 0;
|
654 |
if (curr == robot2)
|
655 |
break;
|
656 |
if (sensor_matrix_get_in_ring(curr))
|
657 |
count++; |
658 |
curr++; |
659 |
} |
660 |
return count;
|
661 |
} |
662 |
|
663 |
/**
|
664 |
* Passes the token to the next robot in the token ring.
|
665 |
**/
|
666 |
static int wl_token_pass_token() |
667 |
{ |
668 |
char nextRobot = 0xFF; |
669 |
int i = wl_get_xbee_id() + 1; |
670 |
char buf[2 * sensor_matrix_get_size()]; |
671 |
if (accepted == -1) |
672 |
{ |
673 |
while (1) |
674 |
{ |
675 |
//TODO: the compiler may or may not optimize this such that my comment is useless:
|
676 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring
|
677 |
// the overhead of a function call each iteration, call it only once before the loop and store
|
678 |
// the value in a variable and check against that variable in the loop condition
|
679 |
if (i == sensor_matrix_get_size()) {
|
680 |
i = 0;
|
681 |
} |
682 |
|
683 |
if (sensor_matrix_get_in_ring(i))
|
684 |
{ |
685 |
nextRobot = (char)i;
|
686 |
break;
|
687 |
} |
688 |
|
689 |
i++; |
690 |
} |
691 |
} |
692 |
else
|
693 |
{ |
694 |
WL_DEBUG_PRINT("Accepting new robot, sending it the token.\r\n");
|
695 |
//add a new robot to the token ring
|
696 |
sensor_matrix_set_in_ring(accepted, 1);
|
697 |
nextRobot = accepted; |
698 |
accepted = -1;
|
699 |
} |
700 |
|
701 |
int j = 0; |
702 |
//TODO: the compiler may or may not optimize this such that my comment is useless:
|
703 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring
|
704 |
// the overhead of a function call each iteration, call it only once before the loop and store
|
705 |
// the value in a variable and check against that variable in the loop condition
|
706 |
for (i = 0; i < sensor_matrix_get_size(); i++) { |
707 |
if (sensor_matrix_get_in_ring(i) && i != wl_get_xbee_id())
|
708 |
{ |
709 |
buf[2*j] = i;
|
710 |
buf[2*j + 1] = sensor_matrix_get_reading(wl_get_xbee_id(), i); |
711 |
j++; |
712 |
} |
713 |
} |
714 |
|
715 |
int packetSize = 2 * j * sizeof(char); |
716 |
WL_DEBUG_PRINT("Passing the token to robot ");
|
717 |
WL_DEBUG_PRINT_INT(nextRobot); |
718 |
WL_DEBUG_PRINT(".\r\n");
|
719 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_SENSOR_MATRIX, buf, packetSize, 0) != 0) |
720 |
return -1; |
721 |
if (wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME)) |
722 |
return -1; |
723 |
|
724 |
wl_token_next_robot = nextRobot; |
725 |
deathDelay = DEATH_DELAY; |
726 |
|
727 |
return 0; |
728 |
} |
729 |
|
730 |
/**
|
731 |
* Called when a packet is received stating that another robot has turned
|
732 |
* its BOM on. Our BOM is then read, and the data is added to the sensor
|
733 |
* matrix.
|
734 |
*
|
735 |
* @param source the robot whose BOM is on
|
736 |
**/
|
737 |
static void wl_token_bom_on_receive(int source) |
738 |
{ |
739 |
WL_DEBUG_PRINT("Robot ");
|
740 |
WL_DEBUG_PRINT_INT(source); |
741 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n");
|
742 |
|
743 |
bom_on_count = 0;
|
744 |
|
745 |
sensor_matrix_set_reading(wl_get_xbee_id(), |
746 |
source, get_max_bom_function()); |
747 |
} |
748 |
|
749 |
/**
|
750 |
* This method is called when we receive the token. Upon receiving
|
751 |
* the token, we must send a BOM_ON packet, flash the BOM, and send
|
752 |
* the token to the next robot.
|
753 |
*
|
754 |
* If there is a pending request for the token, this is processed first.
|
755 |
**/
|
756 |
static void wl_token_get_token() |
757 |
{ |
758 |
WL_DEBUG_PRINT("We have the token.\r\n");
|
759 |
if (ringState == ACCEPTED)
|
760 |
{ |
761 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
|
762 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n");
|
763 |
ringState = MEMBER; |
764 |
joinDelay = -1;
|
765 |
} |
766 |
|
767 |
if (ringState == LEAVING || ringState == NONMEMBER)
|
768 |
{ |
769 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 0);
|
770 |
if (ringState == NONMEMBER)
|
771 |
{ |
772 |
WL_DEBUG_PRINT("We should have left the token ring, but didn't.\r\n");
|
773 |
} |
774 |
return;
|
775 |
} |
776 |
|
777 |
WL_DEBUG_PRINT("Our BOM has been flashed.\r\n");
|
778 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, NULL, 0, 0); |
779 |
|
780 |
bom_on_function(); |
781 |
#ifdef ROBOT
|
782 |
delay_ms(BOM_DELAY); |
783 |
#endif
|
784 |
bom_off_function(); |
785 |
|
786 |
if (!sensor_matrix_get_in_ring(wl_get_xbee_id()))
|
787 |
{ |
788 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n");
|
789 |
return;
|
790 |
} |
791 |
|
792 |
wl_token_pass_token(); |
793 |
} |
794 |
|
795 |
/**
|
796 |
* Called when a request to join the token ring is received.
|
797 |
* If we are the robot preceding the requester in the ring,
|
798 |
* we respond with a JOIN_ACCEPT packet and pass the token to
|
799 |
* this robot when we receive the token.
|
800 |
*
|
801 |
* @param source the robot who requested to join
|
802 |
**/
|
803 |
static void wl_token_join_receive(int source) |
804 |
{ |
805 |
WL_DEBUG_PRINT("Received joining request from robot ");
|
806 |
WL_DEBUG_PRINT_INT(source); |
807 |
WL_DEBUG_PRINT(".\r\n");
|
808 |
|
809 |
//we cannot accept the request if we are not a member
|
810 |
if (ringState != MEMBER)
|
811 |
return;
|
812 |
//if they didn't get our response, see if we should respond again
|
813 |
if (accepted == source)
|
814 |
accepted = -1;
|
815 |
//we can only accept one request at a time
|
816 |
if (accepted != -1) |
817 |
return;
|
818 |
|
819 |
//check if we are the preceding robot in the token ring
|
820 |
int i = source - 1; |
821 |
while (1) |
822 |
{ |
823 |
if (i < 0) |
824 |
i = sensor_matrix_get_size() - 1;
|
825 |
|
826 |
//we must send a join acceptance
|
827 |
if (i == wl_get_xbee_id())
|
828 |
break;
|
829 |
|
830 |
//another robot will handle it
|
831 |
if (sensor_matrix_get_in_ring(i))
|
832 |
return;
|
833 |
|
834 |
i--; |
835 |
} |
836 |
|
837 |
accepted = source; |
838 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT, |
839 |
NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME); |
840 |
|
841 |
WL_DEBUG_PRINT("Accepting robot ");
|
842 |
WL_DEBUG_PRINT_INT(source); |
843 |
WL_DEBUG_PRINT(" into the token ring.\r\n");
|
844 |
|
845 |
// the token ring has not started yet
|
846 |
if (sensor_matrix_get_joined() == 1) |
847 |
wl_token_pass_token(); |
848 |
} |
849 |
|
850 |
/**
|
851 |
* Called when we receive a JOIN_ACCEPT packet in attempting to join
|
852 |
* the token ring.
|
853 |
* Our attempt to join the ring is stopped, and we wait for the token.
|
854 |
*
|
855 |
* @param source the robot who accepted us
|
856 |
**/
|
857 |
static void wl_token_join_accept_receive(int source) |
858 |
{ |
859 |
WL_DEBUG_PRINT("Accepted into the token ring by robot ");
|
860 |
WL_DEBUG_PRINT_INT(source); |
861 |
WL_DEBUG_PRINT(".\r\n");
|
862 |
joinDelay = JOIN_DELAY; |
863 |
ringState = ACCEPTED; |
864 |
acceptor = source; |
865 |
|
866 |
//add ourselves to the token ring
|
867 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1);
|
868 |
} |