Revision 668
Updated port of wireless library to bay boards.
branches/autonomous_recharging/code/projects/libwireless/robotTest/main.c | ||
---|---|---|
2 | 2 |
#include <wireless.h> |
3 | 3 |
#include <wl_token_ring.h> |
4 | 4 |
|
5 |
int main(int argcc, char* argv)
|
|
5 |
int main(int argc, char** argv)
|
|
6 | 6 |
{ |
7 | 7 |
if (argc < 2) |
8 | 8 |
return; |
branches/autonomous_recharging/code/projects/libwireless/lib/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 |
... | ... | |
64 | 64 |
/*Function Prototypes*/ |
65 | 65 |
|
66 | 66 |
/*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); |
|
67 |
static void wl_token_ring_timeout_handler(void); |
|
68 |
static void wl_token_ring_response_handler(int frame, int received); |
|
69 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length); |
|
70 |
static void wl_token_ring_cleanup(void); |
|
72 | 71 |
|
73 | 72 |
/*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); |
|
73 |
static int wl_token_pass_token(void);
|
|
74 |
static int get_token_distance(int robot1, int robot2);
|
|
75 |
static void wl_token_get_token(void);
|
|
77 | 76 |
|
78 | 77 |
/*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); |
|
78 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength);
|
|
79 |
static void wl_token_bom_on_receive(int source);
|
|
80 |
static void wl_token_join_receive(int source);
|
|
81 |
static void wl_token_join_accept_receive(int source);
|
|
83 | 82 |
|
84 | 83 |
/*Global Variables*/ |
85 | 84 |
|
86 | 85 |
//the sensor matrix |
87 |
SensorMatrix* sensorMatrix; |
|
86 |
static SensorMatrix* sensorMatrix;
|
|
88 | 87 |
|
89 | 88 |
//the robot we are waiting to say it has received the token. -1 if unspecified |
90 |
int wl_token_next_robot = -1; |
|
89 |
static int wl_token_next_robot = -1;
|
|
91 | 90 |
|
92 | 91 |
//true if the robot should be in the token ring, 0 otherwise |
93 |
int ringState = NONMEMBER; |
|
92 |
static int ringState = NONMEMBER;
|
|
94 | 93 |
//the id of the robot who accepted us into the token ring, only used in ACCEPTED state |
95 |
int acceptor = -1; |
|
94 |
static int acceptor = -1;
|
|
96 | 95 |
//id of the robot we are accepting |
97 |
int accepted = -1; |
|
96 |
static int accepted = -1;
|
|
98 | 97 |
|
99 | 98 |
//the counter for when we assume a robot is dead |
100 |
int deathDelay = -1; |
|
99 |
static int deathDelay = -1;
|
|
101 | 100 |
//the counter for joining, before we form our own token ring |
102 |
int joinDelay = -1; |
|
101 |
static int joinDelay = -1;
|
|
103 | 102 |
|
104 | 103 |
//current robot to check in the iterator |
105 |
int iteratorCount = 0; |
|
104 |
static int iteratorCount = 0;
|
|
106 | 105 |
|
107 | 106 |
// the amount of time a robot has had its BOM on for |
108 |
int bom_on_count = 0; |
|
107 |
static int bom_on_count = 0;
|
|
109 | 108 |
|
110 |
void do_nothing(void) {} |
|
111 |
int get_nothing(void) {return -1;} |
|
109 |
static void do_nothing(void) {}
|
|
110 |
static int get_nothing(void) {return -1;}
|
|
112 | 111 |
|
113 | 112 |
#ifdef ROBOT |
114 | 113 |
#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; |
|
114 |
static void (*bom_on_function) (void) = bom_on;
|
|
115 |
static void (*bom_off_function) (void) = bom_off;
|
|
116 |
static int (*get_max_bom_function) (void) = get_max_bom;
|
|
118 | 117 |
#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; |
|
118 |
static void (*bom_on_function) (void) = do_nothing;
|
|
119 |
static void (*bom_off_function) (void) = do_nothing;
|
|
120 |
static int (*get_max_bom_function) (void) = get_nothing;
|
|
122 | 121 |
#endif |
123 | 122 |
#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; |
|
123 |
static void (*bom_on_function) (void) = do_nothing;
|
|
124 |
static void (*bom_off_function) (void) = do_nothing;
|
|
125 |
static int (*get_max_bom_function) (void) = get_nothing;
|
|
127 | 126 |
#endif |
128 | 127 |
|
129 |
PacketGroupHandler wl_token_ring_handler = |
|
130 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler,
|
|
128 |
static PacketGroupHandler wl_token_ring_handler =
|
|
129 |
{WL_TOKEN_RING_GROUP, wl_token_ring_timeout_handler, |
|
131 | 130 |
wl_token_ring_response_handler, wl_token_ring_receive_handler, |
132 | 131 |
wl_token_ring_cleanup}; |
133 | 132 |
|
134 | 133 |
/** |
134 |
* Causes the robot to join an existing token ring, or create one |
|
135 |
* if no token ring exists. The token ring uses global and robot to robot |
|
136 |
* packets, and does not rely on any PAN. |
|
137 |
**/ |
|
138 |
int wl_token_ring_join() |
|
139 |
{ |
|
140 |
WL_DEBUG_PRINT("Joining the token ring.\r\n"); |
|
141 |
|
|
142 |
ringState = JOINING; |
|
143 |
joinDelay = DEATH_DELAY * 2; |
|
144 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN, NULL, 0, 0) != 0) { |
|
145 |
return -1; |
|
146 |
} |
|
147 |
|
|
148 |
return 0; |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* Causes the robot to leave the token ring. The robot stops |
|
153 |
* alerting others of its location, but continues storing the |
|
154 |
* locations of other robots. |
|
155 |
**/ |
|
156 |
void wl_token_ring_leave() |
|
157 |
{ |
|
158 |
ringState = LEAVING; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
135 | 162 |
* Initialize the token ring packet group and register it with the |
136 | 163 |
* wireless library. The robot will not join a token ring. |
137 | 164 |
**/ |
138 |
void wl_token_ring_register()
|
|
165 |
int wl_token_ring_register()
|
|
139 | 166 |
{ |
140 | 167 |
if (wl_get_xbee_id() > 0xFF) |
141 | 168 |
{ |
... | ... | |
144 | 171 |
WL_DEBUG_PRINT("XBee ID must be single byte for token ring, is "); |
145 | 172 |
WL_DEBUG_PRINT_INT(wl_get_xbee_id()); |
146 | 173 |
WL_DEBUG_PRINT(".\r\n"); |
147 |
return; |
|
174 |
return -1;
|
|
148 | 175 |
} |
149 |
|
|
176 |
|
|
150 | 177 |
sensorMatrix = sensor_matrix_create(); |
151 | 178 |
//add ourselves to the sensor matrix |
152 | 179 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 0); |
153 | 180 |
|
154 | 181 |
wl_register_packet_group(&wl_token_ring_handler); |
182 |
|
|
183 |
return 0; |
|
155 | 184 |
} |
156 | 185 |
|
157 | 186 |
/** |
... | ... | |
164 | 193 |
|
165 | 194 |
/** |
166 | 195 |
* 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
|
|
196 |
* turned on or off. This could be used for things such as |
|
168 | 197 |
* charging stations, which have multiple BOMs. |
169 | 198 |
* |
170 | 199 |
* @param on_function the function to be called when the BOM |
... | ... | |
185 | 214 |
/** |
186 | 215 |
* Called to cleanup the token ring packet group. |
187 | 216 |
**/ |
188 |
void wl_token_ring_cleanup() |
|
217 |
static void wl_token_ring_cleanup()
|
|
189 | 218 |
{ |
190 | 219 |
sensor_matrix_destroy(sensorMatrix); |
191 | 220 |
} |
... | ... | |
193 | 222 |
/** |
194 | 223 |
* Called approximately every quarter second by the wireless library. |
195 | 224 |
**/ |
196 |
void wl_token_ring_timeout_handler() |
|
225 |
static void wl_token_ring_timeout_handler()
|
|
197 | 226 |
{ |
198 | 227 |
//someone is not responding, assume they are dead |
199 | 228 |
if (deathDelay == 0) |
... | ... | |
209 | 238 |
wl_token_next_robot = -1; |
210 | 239 |
deathDelay = DEATH_DELAY; |
211 | 240 |
} |
212 |
|
|
241 |
|
|
213 | 242 |
// we may have been dropped from the ring when this is received |
214 |
if (ringState == MEMBER) |
|
243 |
if (ringState == MEMBER) {
|
|
215 | 244 |
wl_token_pass_token(); |
245 |
} |
|
216 | 246 |
} |
217 | 247 |
|
218 | 248 |
//we must start our own token ring, no one is responding to us |
... | ... | |
237 | 267 |
} |
238 | 268 |
} |
239 | 269 |
|
240 |
if (deathDelay >= 0) |
|
270 |
if (deathDelay >= 0) {
|
|
241 | 271 |
deathDelay--; |
242 |
if (joinDelay >= 0) |
|
272 |
} |
|
273 |
|
|
274 |
if (joinDelay >= 0) { |
|
243 | 275 |
joinDelay--; |
244 |
if (bom_on_count >= 0) |
|
276 |
} |
|
277 |
|
|
278 |
if (bom_on_count >= 0) { |
|
245 | 279 |
bom_on_count++; |
280 |
} |
|
246 | 281 |
} |
247 | 282 |
|
248 | 283 |
/** |
249 | 284 |
* Called when the XBee tells us if a packet we sent has been received. |
250 |
*
|
|
285 |
* |
|
251 | 286 |
* @param frame the frame number assigned when the packet was sent |
252 | 287 |
* @param received 1 if the packet was received, 0 otherwise |
253 | 288 |
**/ |
254 |
void wl_token_ring_response_handler(int frame, int received) |
|
289 |
static void wl_token_ring_response_handler(int frame, int received)
|
|
255 | 290 |
{ |
256 | 291 |
if (!received) |
257 | 292 |
{ |
... | ... | |
266 | 301 |
* @param packet the data in the packet |
267 | 302 |
* @param length the length of the packet in bytes |
268 | 303 |
**/ |
269 |
void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, |
|
270 |
int length) |
|
304 |
static void wl_token_ring_receive_handler(char type, int source, unsigned char* packet, int length) |
|
271 | 305 |
{ |
272 | 306 |
switch (type) |
273 | 307 |
{ |
... | ... | |
296 | 330 |
} |
297 | 331 |
|
298 | 332 |
/** |
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 | 333 |
* Returns the BOM reading robot source has for robot dest. |
324 | 334 |
* |
325 | 335 |
* @param source the robot that made the BOM reading |
... | ... | |
331 | 341 |
int wl_token_get_sensor_reading(int source, int dest) |
332 | 342 |
{ |
333 | 343 |
if (wl_token_is_robot_in_ring(dest) && |
334 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) |
|
344 |
(source == wl_get_xbee_id() || wl_token_is_robot_in_ring(source))) {
|
|
335 | 345 |
return sensor_matrix_get_reading(sensorMatrix, source, dest); |
346 |
} |
|
347 |
|
|
336 | 348 |
return -1; |
337 | 349 |
} |
338 | 350 |
|
339 | 351 |
/** |
340 | 352 |
* Returns the BOM reading we have for robot dest. |
341 |
*
|
|
353 |
* |
|
342 | 354 |
* @param dest the robot whose relative location is returned |
343 | 355 |
* |
344 | 356 |
* @return a BOM reading from us to robot dest, in the range |
... | ... | |
349 | 361 |
return wl_token_get_sensor_reading(wl_get_xbee_id(), dest); |
350 | 362 |
} |
351 | 363 |
|
364 |
|
|
352 | 365 |
/** |
366 |
* Returns the number of robots in the token ring. |
|
367 |
* |
|
368 |
* @return the number of robots in the token ring |
|
369 |
**/ |
|
370 |
int wl_token_get_robots_in_ring(void) |
|
371 |
{ |
|
372 |
return sensor_matrix_get_joined(sensorMatrix); |
|
373 |
} |
|
374 |
|
|
375 |
/** |
|
376 |
* Returns true if the specified robot is in the token ring, false |
|
377 |
* otherwise. |
|
378 |
* |
|
379 |
* @param robot the robot to check for whether it is in the token ring |
|
380 |
* @return nonzero if the robot is in the token ring, zero otherwise |
|
381 |
**/ |
|
382 |
int wl_token_is_robot_in_ring(int robot) |
|
383 |
{ |
|
384 |
return sensor_matrix_get_in_ring(sensorMatrix, robot); |
|
385 |
} |
|
386 |
|
|
387 |
/** |
|
388 |
* Begins iterating through the robots in the token ring. |
|
389 |
* |
|
390 |
* @see wl_token_iterator_has_next, wl_token_iterator_next |
|
391 |
**/ |
|
392 |
void wl_token_iterator_begin(void) |
|
393 |
{ |
|
394 |
int i = 0; |
|
395 |
|
|
396 |
while (!sensor_matrix_get_in_ring(sensorMatrix, i) && i < sensor_matrix_get_size(sensorMatrix)) { |
|
397 |
i++; |
|
398 |
} |
|
399 |
|
|
400 |
if (i == sensor_matrix_get_size(sensorMatrix)) { |
|
401 |
i = -1; |
|
402 |
} |
|
403 |
|
|
404 |
iteratorCount = i; |
|
405 |
} |
|
406 |
|
|
407 |
/** |
|
408 |
* Returns true if there are more robots in the token ring |
|
409 |
* to iterate through, and false otherwise. |
|
410 |
* |
|
411 |
* @return nonzero if there are more robots to iterate through, |
|
412 |
* zero otherwise |
|
413 |
* |
|
414 |
* @see wl_token_iterator_begin, wl_token_iterator_next |
|
415 |
**/ |
|
416 |
int wl_token_iterator_has_next(void) |
|
417 |
{ |
|
418 |
return iteratorCount != -1; |
|
419 |
} |
|
420 |
|
|
421 |
/** |
|
422 |
* Returns the next robot ID in the token ring. |
|
423 |
* |
|
424 |
* @return the next robot ID in the token ring, or -1 if none exists |
|
425 |
* |
|
426 |
* @see wl_token_iterator_begin, wl_token_iterator_has_next |
|
427 |
**/ |
|
428 |
int wl_token_iterator_next(void) |
|
429 |
{ |
|
430 |
int result = iteratorCount; |
|
431 |
if (result < 0) { |
|
432 |
return result; |
|
433 |
} |
|
434 |
|
|
435 |
iteratorCount++; |
|
436 |
while (!sensor_matrix_get_in_ring(sensorMatrix, iteratorCount) |
|
437 |
&& iteratorCount < sensor_matrix_get_size(sensorMatrix)) { |
|
438 |
iteratorCount++; |
|
439 |
} |
|
440 |
|
|
441 |
if (iteratorCount == sensor_matrix_get_size(sensorMatrix)) { |
|
442 |
iteratorCount = -1; |
|
443 |
} |
|
444 |
|
|
445 |
return result; |
|
446 |
} |
|
447 |
|
|
448 |
/** |
|
449 |
* Returns the number of robots currently in the token ring. |
|
450 |
* |
|
451 |
* @return the number of robots in the token ring |
|
452 |
**/ |
|
453 |
int wl_token_get_num_robots(void) |
|
454 |
{ |
|
455 |
return sensor_matrix_get_joined(sensorMatrix); |
|
456 |
} |
|
457 |
|
|
458 |
/** |
|
459 |
* Returns the number of robots in the sensor matrix. |
|
460 |
* |
|
461 |
* @return the number of robots in the sensor matrix |
|
462 |
**/ |
|
463 |
int wl_token_get_matrix_size(void) |
|
464 |
{ |
|
465 |
return sensor_matrix_get_size(sensorMatrix); |
|
466 |
} |
|
467 |
|
|
468 |
/** |
|
353 | 469 |
* This method is called when we receive a token pass packet. |
354 | 470 |
* @param source is the robot it came from |
355 | 471 |
* @param nextRobot is the robot the token was passed to |
... | ... | |
357 | 473 |
* reading for that robot, repeated for sensorDataLength bytes |
358 | 474 |
* @param sensorDataLength the length in bytes of sensorData |
359 | 475 |
*/ |
360 |
void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength) |
|
476 |
static void wl_token_pass_receive(int source, char nextRobot, unsigned char* sensorData, int sensorDataLength)
|
|
361 | 477 |
{ |
362 | 478 |
int i, j; |
363 | 479 |
|
... | ... | |
384 | 500 |
j = 0; |
385 | 501 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
386 | 502 |
{ |
387 |
if (i == source) |
|
503 |
if (i == source) {
|
|
388 | 504 |
continue; |
389 |
|
|
505 |
} |
|
506 |
|
|
390 | 507 |
//set the sensor information we receive |
391 | 508 |
if (j < sensorDataLength / 2 && sensorData[2 * j] == i) |
392 | 509 |
{ |
... | ... | |
419 | 536 |
{ |
420 | 537 |
ringState = NONMEMBER; |
421 | 538 |
wl_token_ring_join(); |
422 |
|
|
539 |
|
|
423 | 540 |
WL_DEBUG_PRINT("We have been removed from the ring "); |
424 | 541 |
WL_DEBUG_PRINT("and are rejoining.\r\n"); |
425 | 542 |
} |
426 |
|
|
543 |
|
|
427 | 544 |
//the person who accepted us is dead... let's ask again |
428 | 545 |
if (i == acceptor) |
429 | 546 |
{ |
... | ... | |
437 | 554 |
} |
438 | 555 |
|
439 | 556 |
wl_token_next_robot = nextRobot; |
440 |
|
|
557 |
|
|
441 | 558 |
deathDelay = get_token_distance(wl_get_xbee_id(), nextRobot) * DEATH_DELAY; |
442 |
|
|
559 |
|
|
443 | 560 |
//we have the token |
444 |
if (wl_token_next_robot == wl_get_xbee_id()) |
|
561 |
if (wl_token_next_robot == wl_get_xbee_id()) {
|
|
445 | 562 |
wl_token_get_token(); |
563 |
} |
|
446 | 564 |
} |
447 | 565 |
|
448 | 566 |
/** |
... | ... | |
454 | 572 |
* @return the number of passes before the token is expected |
455 | 573 |
* to reach robot2 from robot1 |
456 | 574 |
**/ |
457 |
int get_token_distance(int robot1, int robot2) |
|
575 |
static int get_token_distance(int robot1, int robot2)
|
|
458 | 576 |
{ |
459 | 577 |
int curr = robot1 + 1; |
460 | 578 |
int count = 1; |
... | ... | |
474 | 592 |
/** |
475 | 593 |
* Passes the token to the next robot in the token ring. |
476 | 594 |
**/ |
477 |
void wl_token_pass_token()
|
|
595 |
static int wl_token_pass_token()
|
|
478 | 596 |
{ |
479 | 597 |
char nextRobot; |
480 | 598 |
int i = wl_get_xbee_id() + 1; |
... | ... | |
482 | 600 |
{ |
483 | 601 |
while (1) |
484 | 602 |
{ |
485 |
if (i == sensor_matrix_get_size(sensorMatrix)) |
|
603 |
if (i == sensor_matrix_get_size(sensorMatrix)) {
|
|
486 | 604 |
i = 0; |
605 |
} |
|
606 |
|
|
487 | 607 |
if (sensor_matrix_get_in_ring(sensorMatrix, i)) |
488 | 608 |
{ |
489 | 609 |
nextRobot = (char)i; |
490 | 610 |
break; |
491 | 611 |
} |
612 |
|
|
492 | 613 |
i++; |
493 | 614 |
} |
494 | 615 |
} |
... | ... | |
508 | 629 |
{ |
509 | 630 |
WL_DEBUG_PRINT_INT(packetSize); |
510 | 631 |
WL_DEBUG_PRINT("Out of memory - pass token.\r\n"); |
511 |
return; |
|
632 |
free(buf); |
|
633 |
return -1; |
|
512 | 634 |
} |
513 | 635 |
buf[0] = nextRobot; |
514 | 636 |
|
515 | 637 |
int j = 0; |
516 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) |
|
638 |
for (i = 0; i < sensor_matrix_get_size(sensorMatrix); i++) {
|
|
517 | 639 |
if (sensor_matrix_get_in_ring(sensorMatrix, i) && i != wl_get_xbee_id()) |
518 | 640 |
{ |
519 | 641 |
buf[2*j + 1] = i; |
520 | 642 |
buf[2*j + 2] = sensor_matrix_get_reading(sensorMatrix, wl_get_xbee_id(), i); |
521 | 643 |
j++; |
522 | 644 |
} |
523 |
|
|
645 |
} |
|
646 |
|
|
524 | 647 |
WL_DEBUG_PRINT("Passing the token to robot "); |
525 | 648 |
WL_DEBUG_PRINT_INT(buf[0]); |
526 | 649 |
WL_DEBUG_PRINT(".\r\n"); |
527 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, |
|
528 |
buf, packetSize, 3); |
|
650 |
if (wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_PASS, buf, packetSize, 3) != 0) { |
|
651 |
free(buf); |
|
652 |
return -1; |
|
653 |
} |
|
529 | 654 |
|
530 | 655 |
wl_token_next_robot = nextRobot; |
531 | 656 |
deathDelay = DEATH_DELAY; |
532 | 657 |
free(buf); |
658 |
|
|
659 |
return 0; |
|
533 | 660 |
} |
534 | 661 |
|
535 | 662 |
/** |
... | ... | |
539 | 666 |
* |
540 | 667 |
* @param source the robot whose BOM is on |
541 | 668 |
**/ |
542 |
void wl_token_bom_on_receive(int source) |
|
669 |
static void wl_token_bom_on_receive(int source)
|
|
543 | 670 |
{ |
544 | 671 |
WL_DEBUG_PRINT("Robot "); |
545 | 672 |
WL_DEBUG_PRINT_INT(source); |
546 | 673 |
WL_DEBUG_PRINT(" has flashed its bom.\r\n"); |
547 |
|
|
674 |
|
|
548 | 675 |
bom_on_count = 0; |
549 | 676 |
|
550 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(),
|
|
677 |
sensor_matrix_set_reading(sensorMatrix, wl_get_xbee_id(), |
|
551 | 678 |
source, get_max_bom_function()); |
552 | 679 |
} |
553 | 680 |
|
... | ... | |
555 | 682 |
* This method is called when we receive the token. Upon receiving |
556 | 683 |
* the token, we must send a BOM_ON packet, flash the BOM, and send |
557 | 684 |
* the token to the next robot. |
558 |
*
|
|
685 |
* |
|
559 | 686 |
* If there is a pending request for the token, this is processed first. |
560 | 687 |
**/ |
561 |
void wl_token_get_token() |
|
688 |
static void wl_token_get_token()
|
|
562 | 689 |
{ |
563 | 690 |
WL_DEBUG_PRINT("We have the token.\r\n"); |
564 | 691 |
if (ringState == ACCEPTED) |
565 | 692 |
{ |
566 |
sensor_matrix_set_in_ring(sensorMatrix, |
|
567 |
wl_get_xbee_id(), 1); |
|
693 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1); |
|
568 | 694 |
WL_DEBUG_PRINT("Now a member of the token ring.\r\n"); |
569 | 695 |
ringState = MEMBER; |
570 | 696 |
joinDelay = -1; |
... | ... | |
579 | 705 |
} |
580 | 706 |
return; |
581 | 707 |
} |
582 |
|
|
708 |
|
|
583 | 709 |
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); |
|
710 |
wl_send_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_BOM_ON, NULL, 0, 0); |
|
586 | 711 |
|
587 | 712 |
bom_on_function(); |
588 | 713 |
#ifdef ROBOT |
589 | 714 |
delay_ms(BOM_DELAY); |
590 | 715 |
#endif |
591 | 716 |
bom_off_function(); |
592 |
|
|
717 |
|
|
593 | 718 |
if (!sensor_matrix_get_in_ring(sensorMatrix, wl_get_xbee_id())) |
594 | 719 |
{ |
595 | 720 |
WL_DEBUG_PRINT("Removed from sensor matrix while flashing BOM.\r\n"); |
596 | 721 |
return; |
597 | 722 |
} |
598 |
|
|
723 |
|
|
599 | 724 |
wl_token_pass_token(); |
600 | 725 |
} |
601 | 726 |
|
... | ... | |
607 | 732 |
* |
608 | 733 |
* @param source the robot who requested to join |
609 | 734 |
**/ |
610 |
void wl_token_join_receive(int source) |
|
735 |
static void wl_token_join_receive(int source)
|
|
611 | 736 |
{ |
612 | 737 |
WL_DEBUG_PRINT("Received joining request from robot "); |
613 | 738 |
WL_DEBUG_PRINT_INT(source); |
... | ... | |
622 | 747 |
//we can only accept one request at a time |
623 | 748 |
if (accepted != -1) |
624 | 749 |
return; |
625 |
|
|
750 |
|
|
626 | 751 |
//check if we are the preceding robot in the token ring |
627 | 752 |
int i = source - 1; |
628 | 753 |
while (1) |
... | ... | |
642 | 767 |
accepted = source; |
643 | 768 |
wl_send_robot_to_robot_global_packet(WL_TOKEN_RING_GROUP, WL_TOKEN_JOIN_ACCEPT, |
644 | 769 |
NULL, 0, source, TOKEN_JOIN_ACCEPT_FRAME); |
645 |
|
|
770 |
|
|
646 | 771 |
WL_DEBUG_PRINT("Accepting robot "); |
647 | 772 |
WL_DEBUG_PRINT_INT(source); |
648 | 773 |
WL_DEBUG_PRINT(" into the token ring.\r\n"); |
... | ... | |
659 | 784 |
* |
660 | 785 |
* @param source the robot who accepted us |
661 | 786 |
**/ |
662 |
void wl_token_join_accept_receive(int source) |
|
787 |
static void wl_token_join_accept_receive(int source)
|
|
663 | 788 |
{ |
664 | 789 |
WL_DEBUG_PRINT("Accepted into the token ring by robot "); |
665 | 790 |
WL_DEBUG_PRINT_INT(source); |
... | ... | |
671 | 796 |
//add ourselves to the token ring |
672 | 797 |
sensor_matrix_set_in_ring(sensorMatrix, wl_get_xbee_id(), 1); |
673 | 798 |
} |
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 |
|
branches/autonomous_recharging/code/projects/libwireless/lib/wireless.h | ||
---|---|---|
107 | 107 |
* @param packet the packet received |
108 | 108 |
* @param length the length of the packet |
109 | 109 |
**/ |
110 |
void (*handle_receive) (char type, int source, unsigned char* packet, |
|
111 |
int length); |
|
112 |
|
|
110 |
void (*handle_receive) (char type, int source, unsigned char* packet, int length); |
|
111 |
|
|
113 | 112 |
/** |
114 | 113 |
* Called for any cleanup when the network is turned off. |
115 | 114 |
**/ |
116 | 115 |
void (*unregister) (void); |
117 |
|
|
116 |
|
|
118 | 117 |
} PacketGroupHandler; |
119 | 118 |
|
120 | 119 |
/**@brief Initialize the wireless library **/ |
... | ... | |
129 | 128 |
void wl_unregister_packet_group(PacketGroupHandler* h); |
130 | 129 |
|
131 | 130 |
/**@brief Send a packet to a specific robot in any PAN **/ |
132 |
void wl_send_robot_to_robot_global_packet(char group, char type, |
|
133 |
char* data, int len, int dest, char frame); |
|
131 |
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame); |
|
134 | 132 |
/**@brief Send a packet to a specific robot in our PAN **/ |
135 |
void wl_send_robot_to_robot_packet(char group, char type, |
|
136 |
char* data, int len, int dest, char frame); |
|
133 |
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame); |
|
137 | 134 |
/**@brief Send a packet to all robots **/ |
138 |
void wl_send_global_packet(char group, char type, |
|
139 |
char* data, int len, char frame); |
|
135 |
int wl_send_global_packet(char group, char type, char* data, int len, char frame); |
|
140 | 136 |
/**@brief Send a packet to all robots in our PAN **/ |
141 |
void wl_send_pan_packet(char group, char type, |
|
142 |
char* data, int len, char frame); |
|
137 |
void wl_send_pan_packet(char group, char type, char* data, int len, char frame); |
|
143 | 138 |
|
144 | 139 |
/**@brief Set the PAN we are using **/ |
145 |
void wl_set_pan(int pan);
|
|
140 |
int wl_set_pan(int pan);
|
|
146 | 141 |
/**@brief Get the PAN we are using **/ |
147 | 142 |
int wl_get_pan(void); |
148 | 143 |
/**@brief Set the channel we are using **/ |
149 |
void wl_set_channel(int channel);
|
|
144 |
int wl_set_channel(int channel);
|
|
150 | 145 |
/**@brief Get the channel we are using **/ |
151 | 146 |
int wl_get_channel(void); |
152 | 147 |
/**@brief Get the 16-bit address of the XBee module **/ |
branches/autonomous_recharging/code/projects/libwireless/lib/xbee.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 |
... | ... | |
55 | 55 |
#include <string.h> |
56 | 56 |
|
57 | 57 |
#define XBEE_FRAME_START 0x7E |
58 |
#define XBEE_GET_PACKET_TIMEOUT 1000 |
|
58 | 59 |
|
59 | 60 |
/*Frame Types*/ |
60 | 61 |
#define XBEE_FRAME_STATUS 0x8A |
... | ... | |
69 | 70 |
/*Internal Function Prototypes*/ |
70 | 71 |
|
71 | 72 |
/*I/O Functions*/ |
72 |
void xbee_send(char* buf, int size);
|
|
73 |
void xbee_send_string(char* c);
|
|
73 |
static int xbee_send(char* buf, int size);
|
|
74 |
static int xbee_send_string(char* c);
|
|
74 | 75 |
|
75 | 76 |
#ifndef ROBOT |
76 |
void xbee_read(char* buf, int size);
|
|
77 |
static int xbee_read(char* buf, int size);
|
|
77 | 78 |
#endif |
78 | 79 |
|
79 | 80 |
/*Command Mode Functions |
80 | 81 |
* Called during initialization. |
81 | 82 |
*/ |
82 |
void xbee_enter_command_mode(void);
|
|
83 |
void xbee_exit_command_mode(void);
|
|
84 |
void xbee_enter_api_mode(void);
|
|
85 |
void xbee_exit_api_mode(void);
|
|
86 |
void xbee_wait_for_string(char* s, int len);
|
|
87 |
void xbee_wait_for_ok(void);
|
|
83 |
static int xbee_enter_command_mode(void);
|
|
84 |
static int xbee_exit_command_mode(void);
|
|
85 |
static int xbee_enter_api_mode(void);
|
|
86 |
static int xbee_exit_api_mode(void);
|
|
87 |
static int xbee_wait_for_string(char* s, int len);
|
|
88 |
static int xbee_wait_for_ok(void);
|
|
88 | 89 |
|
89 | 90 |
/*API Mode Functions*/ |
90 | 91 |
|
91 |
int xbee_handle_packet(char* packet, int len); |
|
92 |
void xbee_handle_at_command_response(char* command, char result, |
|
93 |
char* extra, int extraLen); |
|
94 |
void xbee_handle_status(char status); |
|
95 |
int xbee_verify_checksum(char* packet, int len); |
|
96 |
char xbee_compute_checksum(char* packet, int len); |
|
97 |
void xbee_send_frame(char* buf, int len); |
|
98 |
void xbee_send_read_at_command(char* command); |
|
99 |
void xbee_send_modify_at_command(char* command, char* value); |
|
92 |
static int xbee_handle_packet(char* packet, int len); |
|
93 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen); |
|
94 |
static void xbee_handle_status(char status); |
|
95 |
static int xbee_verify_checksum(char* packet, int len); |
|
96 |
static char xbee_compute_checksum(char* packet, int len); |
|
97 |
static int xbee_send_frame(char* buf, int len); |
|
98 |
static int xbee_send_read_at_command(char* command); |
|
99 |
static int xbee_send_modify_at_command(char* command, char* value); |
|
100 | 100 |
|
101 | 101 |
/*Global Variables*/ |
102 | 102 |
|
103 | 103 |
#ifndef ROBOT |
104 |
char* xbee_com_port = XBEE_PORT_DEFAULT; |
|
105 |
int xbee_stream; |
|
106 |
pthread_t* xbee_listen_thread; |
|
104 |
static char* xbee_com_port = XBEE_PORT_DEFAULT;
|
|
105 |
static int xbee_stream;
|
|
106 |
static pthread_t* xbee_listen_thread;
|
|
107 | 107 |
#endif |
108 | 108 |
|
109 | 109 |
// TODO: is this a good size? |
... | ... | |
117 | 117 |
|
118 | 118 |
|
119 | 119 |
//used to store packets as they are read |
120 |
char xbee_buf[128]; |
|
121 |
int currentBufPos = 0; |
|
120 |
static char xbee_buf[128];
|
|
121 |
static int currentBufPos = 0;
|
|
122 | 122 |
|
123 | 123 |
//XBee status |
124 |
unsigned int xbee_panID = XBEE_PAN_DEFAULT; |
|
125 |
unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT; |
|
126 |
int xbee_channel = XBEE_CHANNEL_DEFAULT; |
|
127 |
int xbee_pending_channel = XBEE_CHANNEL_DEFAULT; |
|
128 |
unsigned int xbee_address = 0; |
|
124 |
static unsigned int xbee_panID = XBEE_PAN_DEFAULT;
|
|
125 |
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
|
|
126 |
static int xbee_channel = XBEE_CHANNEL_DEFAULT;
|
|
127 |
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT;
|
|
128 |
static volatile unsigned int xbee_address = 0;
|
|
129 | 129 |
|
130 |
void printHex(char * s, int len) { |
|
131 |
int i; |
|
132 |
for (i = 0; i < len; i++) { |
|
133 |
printf("0x%x ", (int)(s[i])); |
|
134 |
} |
|
135 |
printf("\n"); |
|
136 |
} |
|
137 |
|
|
138 |
|
|
130 | 139 |
/*Function Implementations*/ |
131 | 140 |
|
132 | 141 |
#ifdef ROBOT |
... | ... | |
135 | 144 |
* Interrupt for the robot. Adds bytes received from the xbee |
136 | 145 |
* to the buffer. |
137 | 146 |
**/ |
138 |
#ifndef FIREFLY
|
|
139 |
ISR(USART1_RX_vect)
|
|
147 |
#ifdef FIREFLY |
|
148 |
SIGNAL(SIG_USART0_RECV)
|
|
140 | 149 |
{ |
141 |
char c = UDR1;
|
|
150 |
char c = UDR0;
|
|
142 | 151 |
arrival_buf[buffer_last] = c; |
143 | 152 |
int t = buffer_last + 1; |
144 | 153 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
150 | 159 |
buffer_last = t; |
151 | 160 |
} |
152 | 161 |
#else |
153 |
SIGNAL(SIG_USART0_RECV) |
|
162 |
#ifdef BAYBOARD |
|
163 |
// TODO: interrupt handler for bayboard, should be similar to above and below |
|
164 |
#else |
|
165 |
ISR(USART1_RX_vect) |
|
154 | 166 |
{ |
155 |
char c = UDR0;
|
|
167 |
char c = UDR1;
|
|
156 | 168 |
arrival_buf[buffer_last] = c; |
157 | 169 |
int t = buffer_last + 1; |
158 | 170 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
164 | 176 |
buffer_last = t; |
165 | 177 |
} |
166 | 178 |
#endif |
179 |
#endif |
|
167 | 180 |
|
168 | 181 |
#else |
169 | 182 |
|
183 |
// Computer code |
|
184 |
|
|
170 | 185 |
/** |
171 | 186 |
* Thread that listens to the xbee. |
172 | 187 |
**/ |
173 |
void* listen_to_xbee(void* x) |
|
188 |
static void* listen_to_xbee(void* x)
|
|
174 | 189 |
{ |
175 | 190 |
char c; |
176 | 191 |
while (1) |
177 | 192 |
{ |
178 |
xbee_read(&c, 1); |
|
193 |
if (xbee_read(&c, 1) != 0) { |
|
194 |
fprintf(stderr, "xbee_read failed.\n"); |
|
195 |
return NULL; |
|
196 |
} |
|
197 |
|
|
198 |
//DEBUGGING PRINT |
|
199 |
//printf("interrupt: %c (%d)\n", c, (int)c); |
|
179 | 200 |
arrival_buf[buffer_last] = c; |
180 | 201 |
int t = buffer_last + 1; |
181 | 202 |
if (t == XBEE_BUFFER_SIZE) |
... | ... | |
185 | 206 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
186 | 207 |
} |
187 | 208 |
buffer_last = t; |
209 |
|
|
210 |
usleep(1000); |
|
188 | 211 |
} |
189 |
return 0; |
|
212 |
|
|
213 |
return NULL; |
|
190 | 214 |
} |
191 | 215 |
|
192 | 216 |
#endif |
... | ... | |
194 | 218 |
/** |
195 | 219 |
* Initializes the XBee library so that other functions may be used. |
196 | 220 |
**/ |
197 |
int xbee_lib_init(void)
|
|
221 |
int xbee_lib_init() |
|
198 | 222 |
{ |
199 | 223 |
arrival_buf[0] = 'A'; |
200 | 224 |
arrival_buf[1] = 'A'; |
201 | 225 |
arrival_buf[2] = 'A'; |
202 |
#ifdef ROBOT
|
|
226 |
#ifdef ROBOT |
|
203 | 227 |
|
204 | 228 |
//enable the receiving interrupt |
205 |
#ifdef FIREFLY
|
|
229 |
#ifdef FIREFLY |
|
206 | 230 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
207 |
#else |
|
231 |
#else |
|
232 |
#ifdef BAYBOARD |
|
233 |
//TODO: enable the receiving interrupt on the bayboard |
|
234 |
#else |
|
208 | 235 |
UCSR1B |= _BV(RXCIE); |
209 |
#endif |
|
236 |
#endif |
|
237 |
#endif |
|
210 | 238 |
sei(); |
211 |
#else
|
|
239 |
#else |
|
212 | 240 |
printf("Connecting to port %s.\n", xbee_com_port); |
213 | 241 |
xbee_stream = open(xbee_com_port, O_RDWR); |
214 | 242 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
215 | 243 |
{ |
216 | 244 |
printf("Failed to open connection to XBee on port %s\r\n", xbee_com_port); |
217 | 245 |
return -1; |
246 |
} else { |
|
247 |
printf("Successfully opened connection to XBee on port %s\r\n", xbee_com_port); |
|
218 | 248 |
} |
219 |
|
|
249 |
|
|
220 | 250 |
// set baud rate, etc. correctly |
221 | 251 |
struct termios options; |
222 | 252 |
|
... | ... | |
238 | 268 |
{ |
239 | 269 |
fprintf(stderr, "Error setting attributes.\n"); |
240 | 270 |
return -1; |
271 |
} else { |
|
272 |
//printf("Successfully set termios attributes.\n"); |
|
241 | 273 |
} |
242 | 274 |
|
243 | 275 |
//lockf(xbee_stream, F_LOCK, 0); |
244 |
|
|
245 |
xbee_listen_thread = |
|
246 |
(pthread_t*)malloc(sizeof(pthread_t)); |
|
276 |
|
|
277 |
xbee_listen_thread = (pthread_t*)malloc(sizeof(pthread_t)); |
|
247 | 278 |
if (xbee_listen_thread == NULL) |
248 | 279 |
{ |
249 | 280 |
fprintf(stderr, "%s: Malloc failed.\n", __FUNCTION__); |
250 | 281 |
return -1; |
251 | 282 |
} |
252 |
|
|
253 |
int ret = pthread_create(xbee_listen_thread, NULL, |
|
254 |
listen_to_xbee, NULL); |
|
283 |
|
|
284 |
int ret = pthread_create(xbee_listen_thread, NULL, listen_to_xbee, NULL); |
|
255 | 285 |
if (ret) |
256 | 286 |
{ |
257 | 287 |
fprintf(stderr, "Failed to create listener thread.\r\n"); |
258 | 288 |
return -1; |
289 |
} else { |
|
290 |
//printf("Successfully created listener thread.\n"); |
|
259 | 291 |
} |
260 |
|
|
261 |
#endif |
|
262 |
xbee_enter_command_mode(); |
|
263 |
xbee_enter_api_mode(); |
|
264 |
xbee_exit_command_mode(); |
|
265 |
xbee_send_read_at_command("MY"); |
|
266 |
|
|
292 |
#endif |
|
293 |
|
|
294 |
//DEBUGGING PRINT |
|
295 |
//printf("about to call xbee_enter_command_mode\n"); |
|
296 |
|
|
297 |
if (xbee_enter_command_mode() != 0) { |
|
298 |
#ifndef ROBOT |
|
299 |
printf("Error returned from xbee_enter_command_mode\n"); |
|
300 |
#endif |
|
301 |
return -1; |
|
302 |
} |
|
303 |
|
|
304 |
//DEBUGGING PRINT |
|
305 |
//printf("about to call xbee_enter_api_mode\n"); |
|
306 |
|
|
307 |
if (xbee_enter_api_mode() != 0) { |
|
308 |
#ifndef ROBOT |
|
309 |
printf("Error returned from xbee_enter_api_mode\n"); |
|
310 |
#endif |
|
311 |
return -1; |
|
312 |
} |
|
313 |
|
|
314 |
//DEBUGGING PRINT |
|
315 |
//printf("about to call xbee_exit_command_mode\n"); |
|
316 |
|
|
317 |
if (xbee_exit_command_mode() != 0) { |
|
318 |
#ifndef ROBOT |
|
319 |
printf("Error returned from xbee_exit_command_mode\n"); |
|
320 |
#endif |
|
321 |
return -1; |
|
322 |
} |
|
323 |
|
|
324 |
//DEBUGGING PRINT |
|
325 |
//printf("about to call xbee_send_read_at_command\n"); |
|
326 |
|
|
327 |
if (xbee_send_read_at_command("MY")) { |
|
328 |
#ifndef ROBOT |
|
329 |
printf("Error returned from xbee_send_read_at_command\n"); |
|
330 |
#endif |
|
331 |
return -1; |
|
332 |
} |
|
333 |
|
|
334 |
#ifndef ROBOT |
|
335 |
//printf("About to enter while loop to get xbee_address.\n"); |
|
336 |
int i; |
|
337 |
for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) { |
|
338 |
ret = xbee_get_packet(NULL); |
|
339 |
|
|
340 |
usleep(1000); |
|
341 |
|
|
342 |
if (ret == -1) { |
|
343 |
//printf("xbee_get_packet(NULL) failed.\n"); |
|
344 |
return -1; |
|
345 |
} |
|
346 |
} |
|
347 |
|
|
348 |
// printf("After exiting while loop to get xbee_address.\n"); |
|
349 |
#else |
|
267 | 350 |
//wait to return until the address is set |
268 |
while (xbee_address == 0) xbee_get_packet(NULL); |
|
351 |
while (xbee_address == 0) { |
|
352 |
xbee_get_packet(NULL); |
|
353 |
} |
|
354 |
#endif |
|
269 | 355 |
|
356 |
#ifndef ROBOT |
|
357 |
if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out. |
|
270 | 358 |
|
359 |
printf("xbee_get_packet timed out.\n"); |
|
360 |
return -1; |
|
361 |
} else { |
|
362 |
return 0; |
|
363 |
} |
|
364 |
#else |
|
271 | 365 |
return 0; |
366 |
#endif |
|
272 | 367 |
} |
273 | 368 |
|
274 | 369 |
/** |
... | ... | |
287 | 382 |
|
288 | 383 |
/** |
289 | 384 |
* Send a buffer buf of size bytes to the XBee. |
290 |
*
|
|
385 |
* |
|
291 | 386 |
* @param buf the buffer of data to send |
292 | 387 |
* @param size the number of bytes to send |
293 | 388 |
**/ |
294 |
void xbee_send(char* buf, int size)
|
|
389 |
static int xbee_send(char* buf, int size)
|
|
295 | 390 |
{ |
296 |
#ifdef ROBOT
|
|
391 |
#ifdef ROBOT |
|
297 | 392 |
int i; |
298 |
for (i = 0; i < size; i++) |
|
393 |
for (i = 0; i < size; i++) {
|
|
299 | 394 |
xbee_putc(buf[i]); |
300 |
#else |
|
395 |
} |
|
396 |
|
|
397 |
return 0; |
|
398 |
|
|
399 |
#else |
|
400 |
//DEBUGGING PRINT |
|
401 |
//printf("in xbee_send "); |
|
402 |
//printHex(buf, size); |
|
403 |
|
|
301 | 404 |
int ret = write(xbee_stream, buf, size); |
302 | 405 |
//success |
303 | 406 |
if (ret == size) |
304 |
return; |
|
407 |
return 0;
|
|
305 | 408 |
if (ret == -1) |
306 | 409 |
{ |
307 | 410 |
//interrupted by system signal, probably timer interrupt. |
308 | 411 |
//just try again |
309 | 412 |
if (errno == 4) |
310 | 413 |
{ |
311 |
xbee_send(buf, size); |
|
312 |
return; |
|
414 |
return xbee_send(buf, size); |
|
313 | 415 |
} |
314 | 416 |
printf("Failed to write to xbee, error %i.\r\n", errno); |
315 |
return; |
|
417 |
return -1;
|
|
316 | 418 |
} |
317 | 419 |
|
318 | 420 |
//write was interrupted after writing ret bytes |
319 |
xbee_send(buf + ret, size - ret); |
|
320 |
#endif
|
|
421 |
return xbee_send(buf + ret, size - ret);
|
|
422 |
#endif |
|
321 | 423 |
} |
322 | 424 |
|
323 | 425 |
/** |
... | ... | |
325 | 427 |
* |
326 | 428 |
* @param c the string to send to the XBEE |
327 | 429 |
**/ |
328 |
void xbee_send_string(char* c)
|
|
430 |
static int xbee_send_string(char* c)
|
|
329 | 431 |
{ |
330 |
xbee_send(c, strlen(c)); |
|
432 |
return xbee_send(c, strlen(c));
|
|
331 | 433 |
} |
332 | 434 |
|
333 | 435 |
#ifndef ROBOT |
334 |
void xbee_read(char* buf, int size)
|
|
436 |
static int xbee_read(char* buf, int size)
|
|
335 | 437 |
{ |
336 |
if (read(xbee_stream, buf, size) == -1) |
|
438 |
if (read(xbee_stream, buf, size) == -1) {
|
|
337 | 439 |
printf("Failed to read from xbee.\r\n"); |
440 |
return -1; |
|
441 |
} |
|
442 |
|
|
443 |
return 0; |
|
338 | 444 |
} |
339 | 445 |
#endif |
340 | 446 |
|
341 | 447 |
/** |
342 | 448 |
* Enter into command mode. |
343 | 449 |
**/ |
344 |
void xbee_enter_command_mode()
|
|
450 |
static int xbee_enter_command_mode()
|
|
345 | 451 |
{ |
346 |
xbee_send_string("+++"); |
|
347 |
xbee_wait_for_ok(); |
|
452 |
if (xbee_send_string("+++") != 0) { |
|
453 |
return -1; |
|
454 |
} |
|
455 |
|
|
456 |
#ifndef ROBOT |
|
457 |
// printf("In xbee_enter_command_mode about to call xbee_wait_for_ok()\n"); |
|
458 |
#endif |
|
459 |
|
|
460 |
if (xbee_wait_for_ok() != 0) { |
|
461 |
#ifndef ROBOT |
|
462 |
printf("xbee_wait_for_ok failed.\n"); |
|
463 |
#endif |
|
464 |
return -1; |
|
465 |
} else { |
|
466 |
return 0; |
|
467 |
} |
|
348 | 468 |
} |
349 | 469 |
|
350 | 470 |
/** |
351 | 471 |
* Exit from command mode. |
352 | 472 |
**/ |
353 |
void xbee_exit_command_mode()
|
|
473 |
static int xbee_exit_command_mode()
|
|
354 | 474 |
{ |
355 |
xbee_send_string("ATCN\r"); |
|
475 |
if (xbee_send_string("ATCN\r") != 0) { |
|
476 |
return -1; |
|
477 |
} |
|
478 |
|
|
356 | 479 |
xbee_wait_for_ok(); |
480 |
|
|
481 |
return 0; |
|
357 | 482 |
} |
358 | 483 |
|
359 | 484 |
/** |
360 | 485 |
* Enter API mode. |
361 | 486 |
**/ |
362 |
void xbee_enter_api_mode()
|
|
487 |
static int xbee_enter_api_mode()
|
|
363 | 488 |
{ |
364 |
xbee_send_string("ATAP 1\r"); |
|
489 |
if (xbee_send_string("ATAP 1\r") != 0) { |
|
490 |
return -1; |
|
491 |
} |
|
365 | 492 |
xbee_wait_for_ok(); |
493 |
|
|
494 |
return 0; |
|
366 | 495 |
} |
367 | 496 |
|
368 | 497 |
/** |
369 | 498 |
* Exit API mode. (warning - does not check for response) |
370 | 499 |
**/ |
371 |
void xbee_exit_api_mode()
|
|
500 |
static int xbee_exit_api_mode()
|
|
372 | 501 |
{ |
373 |
xbee_send_string("ATAP 0\r"); |
|
502 |
return xbee_send_string("ATAP 0\r");
|
|
374 | 503 |
} |
375 | 504 |
|
376 | 505 |
/** |
377 | 506 |
* Wait until the string "OK\r" is received from the XBee. |
378 | 507 |
**/ |
379 |
void xbee_wait_for_ok()
|
|
508 |
static int xbee_wait_for_ok()
|
|
380 | 509 |
{ |
381 |
xbee_wait_for_string("OK\r", 3); |
|
510 |
//DEBUGGING PRINT |
|
511 |
//printf("\tin xbee_wait_for_ok\n"); |
|
512 |
return xbee_wait_for_string("OK\r", 3); |
|
382 | 513 |
} |
383 | 514 |
|
384 | 515 |
/** |
... | ... | |
388 | 519 |
* @param s the string to receive |
389 | 520 |
* @param len the length of the string |
390 | 521 |
**/ |
391 |
void xbee_wait_for_string(char* s, int len)
|
|
522 |
static int xbee_wait_for_string(char* s, int len)
|
|
392 | 523 |
{ |
524 |
//DEBUGGING PRINT |
|
525 |
//printf("\t in xbee_wait_for_string\n"); |
|
526 |
|
|
527 |
#ifndef ROBOT |
|
528 |
//printf("In xbee_wait_for_string.\n"); |
|
529 |
#endif |
|
530 |
|
|
393 | 531 |
char* curr = s; |
394 |
while (curr - s < len) |
|
395 |
{ |
|
532 |
while (curr - s < len) { |
|
396 | 533 |
// check if buffer is empty |
397 |
if (buffer_last == buffer_first) |
|
398 |
continue; |
|
399 |
char c = arrival_buf[buffer_first++]; |
|
400 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
401 |
buffer_first = 0; |
|
402 |
if (c == *curr) |
|
403 |
curr++; |
|
404 |
else |
|
405 |
curr = s; |
|
534 |
if (buffer_last != buffer_first) { |
|
535 |
char c = arrival_buf[buffer_first++]; |
|
536 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
537 |
buffer_first = 0; |
|
538 |
} |
|
539 |
|
|
540 |
//DEBUGGING PRINT |
|
541 |
//printf("\t\t c is %c (%d)\n", c, (int)c); |
|
542 |
|
|
543 |
if (c == *curr) { |
|
544 |
curr++; |
|
545 |
} else { |
|
546 |
#ifndef ROBOT |
|
547 |
//return -1; // Computer is less forgiving. |
|
548 |
curr = s; |
|
549 |
#else |
|
550 |
curr = s; |
|
551 |
#endif |
|
552 |
} |
|
553 |
} // else buffer is empty. |
|
554 |
|
|
555 |
#ifndef ROBOT |
|
556 |
usleep(100); |
|
557 |
#endif |
|
406 | 558 |
} |
559 |
|
|
560 |
return 0; |
|
407 | 561 |
} |
408 | 562 |
|
409 | 563 |
/** |
... | ... | |
455 | 609 |
* @param len the size in bytes of the packet data |
456 | 610 |
* |
457 | 611 |
**/ |
458 |
void xbee_send_frame(char* buf, int len)
|
|
612 |
static int xbee_send_frame(char* buf, int len)
|
|
459 | 613 |
{ |
614 |
//printf("in %s and len is %d\n", __FUNCTION__, len); |
|
615 |
|
|
460 | 616 |
char prefix[3]; |
461 | 617 |
prefix[0] = XBEE_FRAME_START; |
462 | 618 |
prefix[1] = (len & 0xFF00) >> 8; |
463 | 619 |
prefix[2] = len & 0xFF; |
464 | 620 |
char checksum = xbee_compute_checksum(buf, len); |
465 |
xbee_send(prefix, 3); |
|
466 |
xbee_send(buf, len); |
|
467 |
xbee_send(&checksum, 1); |
|
621 |
|
|
622 |
if (xbee_send(prefix, 3) != 0) { |
|
623 |
return -1; |
|
624 |
} |
|
625 |
|
|
626 |
if (xbee_send(buf, len) != 0) { |
|
627 |
return -1; |
|
628 |
} |
|
629 |
|
|
630 |
if (xbee_send(&checksum, 1) != 0) { |
|
631 |
return -1; |
|
632 |
} |
|
633 |
|
|
634 |
return 0; |
|
468 | 635 |
} |
469 | 636 |
|
470 | 637 |
/** |
... | ... | |
474 | 641 |
* use ID to read the PAN ID and MY to return the XBee ID. |
475 | 642 |
* See the XBee reference guide for a complete listing. |
476 | 643 |
**/ |
477 |
void xbee_send_read_at_command(char* command)
|
|
644 |
static int xbee_send_read_at_command(char* command)
|
|
478 | 645 |
{ |
479 |
xbee_send_modify_at_command(command, NULL); |
|
646 |
return xbee_send_modify_at_command(command, NULL);
|
|
480 | 647 |
} |
481 | 648 |
|
482 | 649 |
/** |
... | ... | |
486 | 653 |
* @param value the value to pass as a parameter |
487 | 654 |
* (or NULL if there is no parameter) |
488 | 655 |
**/ |
489 |
void xbee_send_modify_at_command(char* command, char* value)
|
|
656 |
static int xbee_send_modify_at_command(char* command, char* value)
|
|
490 | 657 |
{ |
658 |
//printf("in %s with command %s and value %s\n", __FUNCTION__, command, value); |
|
659 |
|
|
491 | 660 |
char buf[16]; |
492 | 661 |
int i; |
493 |
|
|
662 |
|
|
494 | 663 |
buf[0] = XBEE_FRAME_AT_COMMAND; |
495 | 664 |
buf[1] = 1; |
496 | 665 |
buf[2] = command[0]; |
... | ... | |
502 | 671 |
if (valueLen > 8) |
503 | 672 |
{ |
504 | 673 |
WL_DEBUG_PRINT("AT Command too large.\r\n"); |
505 |
return; |
|
674 |
return -1;
|
|
506 | 675 |
} |
507 |
for (i = 0; i < valueLen; i++) |
|
676 |
|
|
677 |
for (i = 0; i < valueLen; i++) { |
|
508 | 678 |
buf[4 + i] = value[i]; |
679 |
} |
|
509 | 680 |
} |
510 |
xbee_send_frame(buf, 4 + valueLen); |
|
681 |
|
|
682 |
return xbee_send_frame(buf, 4 + valueLen); |
|
511 | 683 |
} |
512 | 684 |
|
513 | 685 |
/** |
514 | 686 |
* Send the specified packet. |
515 |
*
|
|
687 |
* |
|
516 | 688 |
* @param packet the packet data to send |
517 | 689 |
* @param len the number of bytes in the packet |
518 |
*
|
|
690 |
* |
|
519 | 691 |
* @param dest the ID of the XBee to send the packet to, |
Also available in: Unified diff