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