Revision 793
Updated bay board wireless library... Doesn't work at all.
wl_token_ring.c | ||
---|---|---|
42 | 42 |
#include <sensor_matrix.h> |
43 | 43 |
|
44 | 44 |
#ifdef ROBOT |
45 |
#ifndef FIREFLY |
|
46 |
#ifdef BAYBOARD |
|
47 |
#include <lbom.h> |
|
48 |
#else |
|
49 |
#include <bom.h> |
|
50 |
#endif |
|
51 |
#endif |
|
45 |
#ifndef FIREFLY |
|
46 |
#include <bom.h> |
|
47 |
#endif |
|
52 | 48 |
#include <time.h> |
53 | 49 |
#endif |
54 | 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 |
|
55 | 53 |
#define DEFAULT_SENSOR_MATRIX_SIZE 20 |
56 | 54 |
|
57 | 55 |
/*Ring States*/ |
... | ... | |
63 | 61 |
#define LEAVING 4 |
64 | 62 |
|
65 | 63 |
/*Frame Types*/ |
66 |
#define TOKEN_JOIN_ACCEPT_FRAME 1 |
|
64 |
#define TOKEN_JOIN_ACCEPT_FRAME 1 |
|
65 |
#define WL_TOKEN_PASS_FRAME 2 |
|
67 | 66 |
|
68 | 67 |
/*Function Prototypes*/ |
69 | 68 |
|
... | ... | |
79 | 78 |
static void wl_token_get_token(void); |
80 | 79 |
|
81 | 80 |
/*Packet Handling Routines*/ |
82 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength); |
|
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 | 83 |
static void wl_token_bom_on_receive(int source); |
84 | 84 |
static void wl_token_join_receive(int source); |
85 | 85 |
static void wl_token_join_accept_receive(int source); |
86 | 86 |
|
87 | 87 |
/*Global Variables*/ |
88 | 88 |
|
89 |
//the sensor matrix |
|
90 |
static SensorMatrix* sensorMatrix; |
|
91 |
|
|
92 | 89 |
//the robot we are waiting to say it has received the token. -1 if unspecified |
93 | 90 |
static int wl_token_next_robot = -1; |
94 | 91 |
|
... | ... | |
110 | 107 |
// the amount of time a robot has had its BOM on for |
111 | 108 |
static int bom_on_count = 0; |
112 | 109 |
|
110 |
#ifndef ROBOT |
|
113 | 111 |
static void do_nothing(void) {} |
114 | 112 |
static int get_nothing(void) {return -1;} |
113 |
#endif |
|
115 | 114 |
|
116 | 115 |
#ifdef ROBOT |
117 | 116 |
#ifndef FIREFLY |
118 |
#ifdef BAYBOARD |
|
119 | 117 |
static void (*bom_on_function) (void) = bom_on; |
120 | 118 |
static void (*bom_off_function) (void) = bom_off; |
121 |
static int (*get_max_bom_function) (void) = get_nothing; |
|
122 |
#else |
|
123 |
static void (*bom_on_function) (void) = bom_on; |
|
124 |
static void (*bom_off_function) (void) = bom_off; |
|
125 | 119 |
static int (*get_max_bom_function) (void) = get_max_bom; |
126 |
#endif |
|
127 | 120 |
#else |
128 | 121 |
static void (*bom_on_function) (void) = do_nothing; |
129 | 122 |
static void (*bom_off_function) (void) = do_nothing; |
... | ... | |
136 | 129 |
#endif |
137 | 130 |
|
138 | 131 |
static PacketGroupHandler wl_token_ring_handler = |
139 |
{wl_token_ring_timeout_handler, |
|
132 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler,
|
|
140 | 133 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
141 |
wl_token_ring_cleanup, WL_TOKEN_RING_GROUP};
|
|
134 |
wl_token_ring_cleanup};
|
|
142 | 135 |
|
143 | 136 |
/** |
144 | 137 |
* Causes the robot to join an existing token ring, or create one |
... | ... | |
163 | 156 |
* alerting others of its location, but continues storing the |
164 | 157 |
* locations of other robots. |
165 | 158 |
**/ |
159 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
160 |
// it reduces code size or not should be done to be sure. |
|
166 | 161 |
void wl_token_ring_leave() |
167 | 162 |
{ |
168 | 163 |
ringState = LEAVING; |
... | ... | |
184 | 179 |
return -1; |
185 | 180 |
} |
186 | 181 |
|
187 |
sensorMatrix = sensor_matrix_create();
|
|
182 |
sensor_matrix_create(); |
|
188 | 183 |
//add ourselves to the sensor matrix |
189 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
|
|
184 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 0); |
|
190 | 185 |
|
191 | 186 |
wl_register_packet_group(&wl_token_ring_handler); |
192 | 187 |
|
... | ... | |
196 | 191 |
/** |
197 | 192 |
* Removes the packet group from the wireless library. |
198 | 193 |
**/ |
194 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
195 |
// it reduces code size or not should be done to be sure. |
|
199 | 196 |
void wl_token_ring_unregister() |
200 | 197 |
{ |
201 | 198 |
wl_unregister_packet_group(&wl_token_ring_handler); |
... | ... | |
224 | 221 |
/** |
225 | 222 |
* Called to cleanup the token ring packet group. |
226 | 223 |
**/ |
224 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
225 |
// it reduces code size or not should be done to be sure. |
|
227 | 226 |
static void wl_token_ring_cleanup() |
228 | 227 |
{ |
229 |
sensor_matrix_destroy(sensorMatrix); |
|
230 | 228 |
} |
231 | 229 |
|
232 | 230 |
/** |
... | ... | |
241 | 239 |
//also, declare that person dead, as long as it isn't us |
242 | 240 |
if (wl_token_next_robot != wl_get_xbee_id()) |
243 | 241 |
{ |
244 |
sensor_matrix_set_in_ring(sensorMatrix, wl_token_next_robot, 0);
|
|
242 |
sensor_matrix_set_in_ring(wl_token_next_robot, 0); |
|
245 | 243 |
WL_DEBUG_PRINT("Robot "); |
246 | 244 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
247 | 245 |
WL_DEBUG_PRINT(" has died.\r\n"); |
... | ... | |
258 | 256 |
//we must start our own token ring, no one is responding to us |
259 | 257 |
if (joinDelay == 0) |
260 | 258 |
{ |
261 |
if (sensor_matrix_get_joined(sensorMatrix) == 0)
|
|
259 |
if (sensor_matrix_get_joined() == 0) |
|
262 | 260 |
{ |
263 | 261 |
WL_DEBUG_PRINT("Creating our own token ring, no robots seem to exist.\r\n"); |
264 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
|
|
262 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1); |
|
265 | 263 |
ringState = MEMBER; |
266 | 264 |
//this will make us pass the token to ourself |
267 | 265 |
//repeatedly, and other robots when they join |
... | ... | |
316 | 314 |
switch (type) |
317 | 315 |
{ |
318 | 316 |
case WL_TOKEN_PASS: |
319 |
if (length < 1) |
|
320 |
{ |
|
321 |
WL_DEBUG_PRINT("Malformed Token Pass packet received.\r\n"); |
|
322 |
return; |
|
323 |
} |
|
324 |
wl_token_pass_receive(source, packet[0], packet + 1, length - 1); |
|
317 |
wl_token_pass_receive(source); |
|
325 | 318 |
break; |
319 |
case WL_TOKEN_SENSOR_MATRIX: |
|
320 |
wl_token_sensor_matrix_receive(source, packet, length); |
|
321 |
break; |
|
326 | 322 |
case WL_TOKEN_BOM_ON: |
327 | 323 |
//add the robot to the sensor matrix if it is not already there |
328 | 324 |
wl_token_bom_on_receive(source); |
... | ... | |
352 | 348 |
{ |
353 | 349 |
if (wl_token_is_robot_in_ring(dest) && |
354 | 350 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) { |
355 |
return sensor_matrix_get_reading(sensorMatrix, source, dest);
|
|
351 |
return sensor_matrix_get_reading(source, dest); |
|
356 | 352 |
} |
357 | 353 |
|
358 | 354 |
return -1; |
... | ... | |
366 | 362 |
* @return a BOM reading from us to robot dest, in the range |
367 | 363 |
* 0-15, or -1 if it is unkown |
368 | 364 |
**/ |
365 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
366 |
// it reduces code size or not should be done to be sure. |
|
369 | 367 |
int wl_token_get_my_sensor_reading(int dest) |
370 | 368 |
{ |
371 | 369 |
return wl_token_get_sensor_reading(wl_get_xbee_id(), dest); |
... | ... | |
377 | 375 |
* |
378 | 376 |
* @return the number of robots in the token ring |
379 | 377 |
**/ |
378 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
379 |
// it reduces code size or not should be done to be sure. |
|
380 | 380 |
int wl_token_get_robots_in_ring(void) |
381 | 381 |
{ |
382 |
return sensor_matrix_get_joined(sensorMatrix);
|
|
382 |
return sensor_matrix_get_joined(); |
|
383 | 383 |
} |
384 | 384 |
|
385 | 385 |
/** |
... | ... | |
389 | 389 |
* @param robot the robot to check for whether it is in the token ring |
390 | 390 |
* @return nonzero if the robot is in the token ring, zero otherwise |
391 | 391 |
**/ |
392 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
393 |
// it reduces code size or not should be done to be sure. |
|
392 | 394 |
int wl_token_is_robot_in_ring(int robot) |
393 | 395 |
{ |
394 |
return sensor_matrix_get_in_ring(sensorMatrix, robot);
|
|
396 |
return sensor_matrix_get_in_ring(robot); |
|
395 | 397 |
} |
396 | 398 |
|
397 | 399 |
/** |
... | ... | |
402 | 404 |
void wl_token_iterator_begin(void) |
403 | 405 |
{ |
404 | 406 |
int i = 0; |
405 |
|
|
406 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) { |
|
407 |
//TODO: the compiler may or may not optimize this such that my comment is useless: |
|
408 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring |
|
409 |
// the overhead of a function call each iteration, call it only once before the loop and store |
|
410 |
// the value in a variable and check against that variable in the loop condition |
|
411 |
while (!sensor_matrix_get_in_ring(i) && i < sensor_matrix_get_size()) { |
|
407 | 412 |
i++; |
408 | 413 |
} |
409 | 414 |
|
410 |
if (i == sensor_matrix_get_size(sensorMatrix)) { |
|
415 |
//TODO: if you do the above comment, then you can compare this to the variable also |
|
416 |
if (i == sensor_matrix_get_size()) { |
|
411 | 417 |
i = -1; |
412 | 418 |
} |
413 | 419 |
|
... | ... | |
423 | 429 |
* |
424 | 430 |
* @see wl_token_iterator_begin, wl_token_iterator_next |
425 | 431 |
**/ |
432 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
433 |
// it reduces code size or not should be done to be sure. |
|
426 | 434 |
int wl_token_iterator_has_next(void) |
427 | 435 |
{ |
428 | 436 |
return iteratorCount != -1; |
... | ... | |
442 | 450 |
return result; |
443 | 451 |
} |
444 | 452 |
|
453 |
//TODO: the compiler may or may not optimize this such that my comment is useless: |
|
454 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring |
|
455 |
// the overhead of a function call each iteration, call it only once before the loop and store |
|
456 |
// the value in a variable and check against that variable in the loop condition |
|
445 | 457 |
iteratorCount++; |
446 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount)
|
|
447 |
&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) {
|
|
458 |
while (!sensor_matrix_get_in_ring(iteratorCount) |
|
459 |
&& iteratorCount < sensor_matrix_get_size()) { |
|
448 | 460 |
iteratorCount++; |
449 | 461 |
} |
450 | 462 |
|
451 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) { |
|
463 |
//TODO: if you do the above comment, then you can compare this to the variable also |
|
464 |
if (iteratorCount == sensor_matrix_get_size()) { |
|
452 | 465 |
iteratorCount = -1; |
453 | 466 |
} |
454 | 467 |
|
... | ... | |
460 | 473 |
* |
461 | 474 |
* @return the number of robots in the token ring |
462 | 475 |
**/ |
476 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
477 |
// it reduces code size or not should be done to be sure. |
|
463 | 478 |
int wl_token_get_num_robots(void) |
464 | 479 |
{ |
465 |
return sensor_matrix_get_joined(sensorMatrix);
|
|
480 |
return sensor_matrix_get_joined(); |
|
466 | 481 |
} |
467 | 482 |
|
468 | 483 |
/** |
... | ... | |
470 | 485 |
* |
471 | 486 |
* @return the number of robots in the sensor matrix |
472 | 487 |
**/ |
488 |
//TODO: this function is so simple, it *may* be beneficial to inline this function. testing of if |
|
489 |
// it reduces code size or not should be done to be sure. |
|
473 | 490 |
int wl_token_get_matrix_size(void) |
474 | 491 |
{ |
475 |
return sensor_matrix_get_size(sensorMatrix);
|
|
492 |
return sensor_matrix_get_size(); |
|
476 | 493 |
} |
477 | 494 |
|
478 | 495 |
/** |
479 | 496 |
* This method is called when we receive a token pass packet. |
497 |
* |
|
498 |
* @param source the robot who passed the token to us. |
|
499 |
**/ |
|
500 |
static void wl_token_pass_receive(int source) |
|
501 |
{ |
|
502 |
WL_DEBUG_PRINT("Received token from "); |
|
503 |
WL_DEBUG_PRINT_INT(source); |
|
504 |
WL_DEBUG_PRINT(", expected "); |
|
505 |
WL_DEBUG_PRINT_INT(wl_token_next_robot); |
|
506 |
WL_DEBUG_PRINT(".\n"); |
|
507 |
// this prevents two tokens from being passed around at a time (second clause is in case we are joining) |
|
508 |
if ((source != wl_token_next_robot && wl_get_xbee_id() != wl_token_next_robot) && bom_on_count <= DEATH_DELAY / 2 && |
|
509 |
ringState != ACCEPTED) |
|
510 |
{ |
|
511 |
WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n"); |
|
512 |
WL_DEBUG_PRINT("There are probably two tokens going around, packet ignored.\n"); |
|
513 |
return; |
|
514 |
} |
|
515 |
bom_on_count = -1; |
|
516 |
deathDelay = -1; |
|
517 |
sensor_matrix_set_in_ring(source, 1); |
|
518 |
wl_token_get_token(); |
|
519 |
} |
|
520 |
|
|
521 |
/** |
|
522 |
* This method is called when we receive a token pass packet. |
|
480 | 523 |
* @param source is the robot it came from |
481 | 524 |
* @param nextRobot is the robot the token was passed to |
482 | 525 |
* @param sensorData a char with an id followed by a char with the sensor |
483 | 526 |
* reading for that robot, repeated for sensorDataLength bytes |
484 | 527 |
* @param sensorDataLength the length in bytes of sensorData |
485 | 528 |
*/ |
486 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength)
|
|
529 |
static void wl_token_sensor_matrix_receive(int source, unsigned char* sensorData, int sensorDataLength)
|
|
487 | 530 |
{ |
488 | 531 |
int i, j; |
532 |
char nextRobot; |
|
489 | 533 |
|
490 |
// this prevents two tokens from being passed around at a time (second clause is in case we are joining) |
|
491 |
if (source != wl_token_next_robot && bom_on_count <= DEATH_DELAY / 2 && |
|
492 |
ringState != ACCEPTED) |
|
493 |
{ |
|
494 |
WL_DEBUG_PRINT("Received token pass when a robot should not have died yet.\n"); |
|
495 |
WL_DEBUG_PRINT("There are probably two tokens going around, packet ignored.\n"); |
|
496 |
return; |
|
497 |
} |
|
498 |
|
|
499 | 534 |
bom_on_count = -1; |
500 | 535 |
deathDelay = -1; |
501 |
WL_DEBUG_PRINT("Received the token from robot"); |
|
502 |
WL_DEBUG_PRINT_INT(source); |
|
503 |
WL_DEBUG_PRINT(", next robot is "); |
|
504 |
WL_DEBUG_PRINT_INT((int)nextRobot); |
|
505 |
WL_DEBUG_PRINT(" \r\n"); |
|
506 |
sensor_matrix_set_in_ring(sensorMatrix, source, 1); |
|
536 |
sensor_matrix_set_in_ring(source, 1); |
|
507 | 537 |
|
508 | 538 |
//with this packet, we are passed the id of the next robot in the ring |
509 | 539 |
//and the sensor matrix, a list of id and sensor reading pairs (two bytes for both) |
510 | 540 |
j = 0; |
511 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
|
541 |
//TODO: the compiler may or may not optimize this such that my comment is useless: |
|
542 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring |
|
543 |
// the overhead of a function call each iteration, call it only once before the loop and store |
|
544 |
// the value in a variable and check against that variable in the loop condition |
|
545 |
for (i = 0; i < sensor_matrix_get_size(); i++) |
|
512 | 546 |
{ |
513 | 547 |
if (i == source) { |
514 | 548 |
continue; |
... | ... | |
523 | 557 |
accepted = -1; |
524 | 558 |
WL_DEBUG_PRINT("Someone accepted the robot we did.\r\n"); |
525 | 559 |
} |
526 |
sensor_matrix_set_reading(sensorMatrix, source, i,
|
|
560 |
sensor_matrix_set_reading(source, i, |
|
527 | 561 |
sensorData[2 * j + 1]); |
528 |
sensor_matrix_set_in_ring(sensorMatrix, i, 1); |
|
562 |
if (!sensor_matrix_get_in_ring(i)) |
|
563 |
{ |
|
564 |
WL_DEBUG_PRINT("Robot "); |
|
565 |
WL_DEBUG_PRINT_INT(i); |
|
566 |
WL_DEBUG_PRINT(" has been added to the sensor matrix of robot "); |
|
567 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
|
568 |
WL_DEBUG_PRINT(" due to a packet from robot "); |
|
569 |
WL_DEBUG_PRINT_INT(source); |
|
570 |
WL_DEBUG_PRINT(".\r\n"); |
|
571 |
} |
|
572 |
sensor_matrix_set_in_ring(i, 1); |
|
529 | 573 |
j++; |
530 | 574 |
} |
531 | 575 |
else |
532 | 576 |
{ |
533 |
if (sensor_matrix_get_in_ring(sensorMatrix, i))
|
|
577 |
if (sensor_matrix_get_in_ring(i)) |
|
534 | 578 |
{ |
535 | 579 |
WL_DEBUG_PRINT("Robot "); |
536 | 580 |
WL_DEBUG_PRINT_INT(i); |
... | ... | |
539 | 583 |
WL_DEBUG_PRINT(" due to a packet from robot "); |
540 | 584 |
WL_DEBUG_PRINT_INT(source); |
541 | 585 |
WL_DEBUG_PRINT(".\r\n"); |
542 |
sensor_matrix_set_in_ring(sensorMatrix, i, 0);
|
|
586 |
sensor_matrix_set_in_ring(i, 0); |
|
543 | 587 |
} |
544 | 588 |
|
545 | 589 |
if (i == wl_get_xbee_id() && ringState == MEMBER) |
... | ... | |
554 | 598 |
//the person who accepted us is dead... let's ask again |
555 | 599 |
if (i == acceptor) |
556 | 600 |
{ |
557 |
sensor_matrix_set_in_ring(sensorMatrix, |
|
558 |
wl_get_xbee_id(), 1); |
|
601 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 0); |
|
559 | 602 |
ringState = NONMEMBER; |
560 | 603 |
acceptor = -1; |
561 | 604 |
wl_token_ring_join(); |
562 | 605 |
} |
563 | 606 |
} |
564 | 607 |
} |
608 |
|
|
609 |
// get the next robot in the token ring |
|
610 |
i = source + 1; |
|
611 |
while (1) |
|
612 |
{ |
|
613 |
if (i == sensor_matrix_get_size()) { |
|
614 |
i = 0; |
|
615 |
} |
|
565 | 616 |
|
566 |
wl_token_next_robot = nextRobot; |
|
617 |
if (sensor_matrix_get_in_ring(i) || i == source) |
|
618 |
{ |
|
619 |
nextRobot = (char)i; |
|
620 |
break; |
|
621 |
} |
|
567 | 622 |
|
623 |
i++; |
|
624 |
} |
|
625 |
|
|
626 |
if (nextRobot != wl_get_xbee_id()) |
|
627 |
wl_token_next_robot = nextRobot; |
|
628 |
|
|
568 | 629 |
deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY; |
569 | 630 |
|
570 |
//we have the token |
|
571 |
if (wl_token_next_robot == wl_get_xbee_id()) { |
|
572 |
wl_token_get_token(); |
|
573 |
} |
|
631 |
if (sensor_matrix_get_joined() == 0 && ringState == JOINING) |
|
632 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME); |
|
574 | 633 |
} |
575 | 634 |
|
576 | 635 |
/** |
... | ... | |
588 | 647 |
int count = 1; |
589 | 648 |
while (1) |
590 | 649 |
{ |
591 |
if (curr == sensor_matrix_get_size(sensorMatrix)) |
|
650 |
//TODO: the compiler may or may not optimize this such that my comment is useless: |
|
651 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring |
|
652 |
// the overhead of a function call each iteration, call it only once before the loop and store |
|
653 |
// the value in a variable and check against that variable in the loop condition |
|
654 |
if (curr == sensor_matrix_get_size()) |
|
592 | 655 |
curr = 0; |
593 | 656 |
if (curr == robot2) |
594 | 657 |
break; |
595 |
if (sensor_matrix_get_in_ring(sensorMatrix, curr))
|
|
658 |
if (sensor_matrix_get_in_ring(curr)) |
|
596 | 659 |
count++; |
597 | 660 |
curr++; |
598 | 661 |
} |
... | ... | |
604 | 667 |
**/ |
605 | 668 |
static int wl_token_pass_token() |
606 | 669 |
{ |
607 |
char nextRobot; |
|
670 |
char nextRobot = 0xFF;
|
|
608 | 671 |
int i = wl_get_xbee_id() + 1; |
672 |
char buf[2 * sensor_matrix_get_size()]; |
|
609 | 673 |
if (accepted == -1) |
610 | 674 |
{ |
611 | 675 |
while (1) |
612 | 676 |
{ |
613 |
if (i == sensor_matrix_get_size(sensorMatrix)) { |
|
677 |
//TODO: the compiler may or may not optimize this such that my comment is useless: |
|
678 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring |
|
679 |
// the overhead of a function call each iteration, call it only once before the loop and store |
|
680 |
// the value in a variable and check against that variable in the loop condition |
|
681 |
if (i == sensor_matrix_get_size()) { |
|
614 | 682 |
i = 0; |
615 | 683 |
} |
616 | 684 |
|
617 |
if (sensor_matrix_get_in_ring(sensorMatrix, i))
|
|
685 |
if (sensor_matrix_get_in_ring(i)) |
|
618 | 686 |
{ |
619 | 687 |
nextRobot = (char)i; |
620 | 688 |
break; |
... | ... | |
627 | 695 |
{ |
628 | 696 |
WL_DEBUG_PRINT("Accepting new robot, sending it the token.\r\n"); |
629 | 697 |
//add a new robot to the token ring |
630 |
sensor_matrix_set_in_ring(sensorMatrix, accepted, 1);
|
|
698 |
sensor_matrix_set_in_ring(accepted, 1); |
|
631 | 699 |
nextRobot = accepted; |
632 | 700 |
accepted = -1; |
633 | 701 |
} |
634 | 702 |
|
635 |
//we don't include ourself |
|
636 |
int packetSize = 1 + 2 * (sensor_matrix_get_joined(sensorMatrix) - 1); |
|
637 |
char* buf = (char*)malloc(packetSize * sizeof(char)); |
|
638 |
if (!buf) |
|
639 |
{ |
|
640 |
WL_DEBUG_PRINT_INT(packetSize); |
|
641 |
WL_DEBUG_PRINT("Out of memory - pass token.\r\n"); |
|
642 |
free(buf); |
|
643 |
return -1; |
|
644 |
} |
|
645 |
buf[0] = nextRobot; |
|
646 |
|
|
647 | 703 |
int j = 0; |
648 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) { |
|
649 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id()) |
|
704 |
//TODO: the compiler may or may not optimize this such that my comment is useless: |
|
705 |
// instead of calling sensor_matrix_get_size every iteration of the while loop and incurring |
|
706 |
// the overhead of a function call each iteration, call it only once before the loop and store |
|
707 |
// the value in a variable and check against that variable in the loop condition |
|
708 |
for (i = 0; i < sensor_matrix_get_size(); i++) { |
|
709 |
if (sensor_matrix_get_in_ring(i) && i != wl_get_xbee_id()) |
|
650 | 710 |
{ |
651 |
buf[2*j + 1] = i;
|
|
652 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i);
|
|
711 |
buf[2*j] = i; |
|
712 |
buf[2*j + 1] = sensor_matrix_get_reading(wl_get_xbee_id(), i);
|
|
653 | 713 |
j++; |
654 | 714 |
} |
655 | 715 |
} |
656 | 716 |
|
717 |
int packetSize = 2 * j * sizeof(char); |
|
657 | 718 |
WL_DEBUG_PRINT("Passing the token to robot "); |
658 |
WL_DEBUG_PRINT_INT(buf[0]);
|
|
719 |
WL_DEBUG_PRINT_INT(nextRobot);
|
|
659 | 720 |
WL_DEBUG_PRINT(".\r\n"); |
660 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, buf, packetSize, 3) != 0) { |
|
661 |
free(buf); |
|
721 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_SENSOR_MATRIX, buf, packetSize, 0) != 0) |
|
662 | 722 |
return -1; |
663 |
} |
|
723 |
if (wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, NULL, 0, nextRobot, WL_TOKEN_PASS_FRAME)) |
|
724 |
return -1; |
|
664 | 725 |
|
665 | 726 |
wl_token_next_robot = nextRobot; |
666 | 727 |
deathDelay = DEATH_DELAY; |
667 |
free(buf); |
|
668 | 728 |
|
669 | 729 |
return 0; |
670 | 730 |
} |
... | ... | |
684 | 744 |
|
685 | 745 |
bom_on_count = 0; |
686 | 746 |
|
687 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(),
|
|
747 |
sensor_matrix_set_reading(wl_get_xbee_id(), |
|
688 | 748 |
source, get_max_bom_function()); |
689 | 749 |
} |
690 | 750 |
|
... | ... | |
700 | 760 |
WL_DEBUG_PRINT("We have the token.\r\n"); |
701 | 761 |
if (ringState == ACCEPTED) |
702 | 762 |
{ |
703 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
|
|
763 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1); |
|
704 | 764 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n"); |
705 | 765 |
ringState = MEMBER; |
706 | 766 |
joinDelay = -1; |
... | ... | |
708 | 768 |
|
709 | 769 |
if (ringState == LEAVING || ringState == NONMEMBER) |
710 | 770 |
{ |
711 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0);
|
|
771 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 0); |
|
712 | 772 |
if (ringState == NONMEMBER) |
713 | 773 |
{ |
714 | 774 |
WL_DEBUG_PRINT("We should have left the token ring, but didn't.\r\n"); |
... | ... | |
725 | 785 |
#endif |
726 | 786 |
bom_off_function(); |
727 | 787 |
|
728 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id()))
|
|
788 |
if (!sensor_matrix_get_in_ring(wl_get_xbee_id())) |
|
729 | 789 |
{ |
730 | 790 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n"); |
731 | 791 |
return; |
... | ... | |
763 | 823 |
while (1) |
764 | 824 |
{ |
765 | 825 |
if (i < 0) |
766 |
i = sensor_matrix_get_size(sensorMatrix) - 1; |
|
826 |
i = sensor_matrix_get_size() - 1; |
|
827 |
|
|
767 | 828 |
//we must send a join acceptance |
768 | 829 |
if (i == wl_get_xbee_id()) |
769 | 830 |
break; |
770 | 831 |
|
771 | 832 |
//another robot will handle it |
772 |
if (sensor_matrix_get_in_ring(sensorMatrix, i))
|
|
833 |
if (sensor_matrix_get_in_ring(i)) |
|
773 | 834 |
return; |
835 |
|
|
774 | 836 |
i--; |
775 | 837 |
} |
776 | 838 |
|
... | ... | |
783 | 845 |
WL_DEBUG_PRINT(" into the token ring.\r\n"); |
784 | 846 |
|
785 | 847 |
// the token ring has not started yet |
786 |
if (sensor_matrix_get_joined(sensorMatrix) == 1)
|
|
848 |
if (sensor_matrix_get_joined() == 1) |
|
787 | 849 |
wl_token_pass_token(); |
788 | 850 |
} |
789 | 851 |
|
... | ... | |
804 | 866 |
acceptor = source; |
805 | 867 |
|
806 | 868 |
//add ourselves to the token ring |
807 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1);
|
|
869 |
sensor_matrix_set_in_ring(wl_get_xbee_id(), 1); |
|
808 | 870 |
} |
Also available in: Unified diff