Revision 309
Brian committing from Eugene's account - Fixed Justin's port code. Wireless no longer needs gtkterm open to work with the baud rate. Untested on mac. Queue is thread safe on the computer (not robot). You may need to unplug and replug the xbee for it to work.
wireless.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 |
... | ... | |
98 | 98 |
* Initializes the wireless library. Must be called before any |
99 | 99 |
* other function. |
100 | 100 |
**/ |
101 |
void wl_init()
|
|
101 |
int wl_init()
|
|
102 | 102 |
{ |
103 | 103 |
int i; |
104 | 104 |
for (i = 0; i < WL_MAX_PACKET_GROUPS; i++) |
105 | 105 |
wl_packet_groups[i] = NULL; |
106 | 106 |
|
107 |
xbee_lib_init(); |
|
108 |
|
|
107 |
printf("calling xbee_lib_init\n"); |
|
108 |
if (xbee_lib_init() == -1) { |
|
109 |
return -1; |
|
110 |
} |
|
111 |
printf("xbee_lib_init finished\n"); |
|
112 |
|
|
109 | 113 |
//begin timeout timer |
110 | 114 |
#ifdef ROBOT |
111 | 115 |
#ifdef FIREFLY |
112 | 116 |
rtc_init(PRESCALE_DIV_256, 32, &timer_handler); |
113 | 117 |
#else |
114 |
rtc_init(HALF_SECOND, &timer_handler);
|
|
118 |
rtc_init(HALF_SECOND, &timer_handler); |
|
115 | 119 |
#endif |
116 | 120 |
#else |
117 |
|
|
121 |
|
|
118 | 122 |
//create our timer |
119 | 123 |
struct itimerval timer_val; |
120 | 124 |
struct timeval interval; |
... | ... | |
127 | 131 |
timer_val.it_value = first_time; |
128 | 132 |
if(setitimer(ITIMER_REAL,&timer_val,NULL)==-1) |
129 | 133 |
{ |
130 |
WL_DEBUG_PRINT("Error creating a timer.\r\n");
|
|
134 |
WL_DEBUG_PRINT("Error creating a timer.\r\n"); |
|
131 | 135 |
perror("Failure's cause"); |
132 |
exit(1);
|
|
136 |
exit(1); |
|
133 | 137 |
} |
134 | 138 |
|
135 | 139 |
//create signal handler |
... | ... | |
140 | 144 |
sigaction(SIGALRM, &wl_sig_act, 0); |
141 | 145 |
sigaction(SIGINT, &wl_sig_act, 0); |
142 | 146 |
#endif |
147 |
|
|
148 |
return 0; |
|
143 | 149 |
} |
144 | 150 |
|
145 | 151 |
/** |
... | ... | |
152 | 158 |
if (wl_packet_groups[i] != NULL && |
153 | 159 |
wl_packet_groups[i]->unregister != NULL) |
154 | 160 |
wl_packet_groups[i]->unregister(); |
155 |
|
|
161 |
|
|
156 | 162 |
xbee_terminate(); |
157 | 163 |
} |
158 | 164 |
|
... | ... | |
209 | 215 |
* |
210 | 216 |
* @return the 16-bit address of the XBee module. |
211 | 217 |
**/ |
212 |
unsigned int wl_get_xbee_id()
|
|
218 |
int wl_get_xbee_id() |
|
213 | 219 |
{ |
214 | 220 |
return xbee_get_address(); |
215 | 221 |
} |
... | ... | |
276 | 282 |
void wl_send_pan_packet(char group, char type, |
277 | 283 |
char* data, int len, char frame) |
278 | 284 |
{ |
279 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST,
|
|
285 |
wl_send_packet(group, type, data, len, XBEE_BROADCAST, |
|
280 | 286 |
XBEE_OPTIONS_NONE, frame); |
281 | 287 |
} |
282 | 288 |
|
... | ... | |
329 | 335 |
|
330 | 336 |
/** |
331 | 337 |
* Unregister a packet group from the wireless library. |
332 |
*
|
|
338 |
* |
|
333 | 339 |
* @param h the packet group to remove |
334 | 340 |
**/ |
335 | 341 |
void wl_unregister_packet_group(PacketGroupHandler* h) |
... | ... | |
367 | 373 |
wl_do_timeout(); |
368 | 374 |
wl_timeout = 0; |
369 | 375 |
} |
370 |
|
|
376 |
|
|
371 | 377 |
int len = xbee_get_packet(wl_buf); |
372 | 378 |
if (len < 0)//no packet received |
373 | 379 |
return; |
374 |
|
|
380 |
|
|
375 | 381 |
if (wl_buf[0] == XBEE_TX_STATUS) |
376 | 382 |
{ |
377 | 383 |
if (len != 3) |
... | ... | |
379 | 385 |
WL_DEBUG_PRINT("Transmit Status packet should be of length 3.\r\n"); |
380 | 386 |
return; |
381 | 387 |
} |
382 |
|
|
388 |
|
|
383 | 389 |
//the first four bits are the packet group |
384 | 390 |
//this only works with under 16 groups |
385 | 391 |
int group = (int)(wl_buf[1] >> 4); |
... | ... | |
398 | 404 |
WL_DEBUG_PRINT("Purged\r\n"); |
399 | 405 |
} |
400 | 406 |
} |
401 |
|
|
407 |
|
|
402 | 408 |
if (wl_packet_groups[group] != NULL && |
403 | 409 |
wl_packet_groups[group]->handle_response != NULL) |
404 | 410 |
wl_packet_groups[group]->handle_response( |
405 | 411 |
(int)wl_buf[1] & 0x0F, success); |
406 | 412 |
return; |
407 | 413 |
} |
408 |
|
|
414 |
|
|
409 | 415 |
if (wl_buf[0] == XBEE_RX) |
410 | 416 |
{ |
411 | 417 |
if (len < 7) |
... | ... | |
413 | 419 |
WL_DEBUG_PRINT("Packet is too small.\r\n"); |
414 | 420 |
return; |
415 | 421 |
} |
416 |
|
|
422 |
|
|
417 | 423 |
int source = ((int)wl_buf[1] << 8) + ((int)wl_buf[2]); |
418 |
|
|
424 |
|
|
419 | 425 |
/* |
420 | 426 |
//unused for now |
421 | 427 |
int signalStrength = wl_buf[3]; |
422 | 428 |
//1 for Address broadcast, 2 for PAN broadcast |
423 | 429 |
int options = wl_buf[4]; |
424 | 430 |
*/ |
425 |
|
|
431 |
|
|
426 | 432 |
int group = wl_buf[5]; |
427 | 433 |
int type = wl_buf[6]; |
428 | 434 |
int packetLen = len - 7; |
429 |
|
|
435 |
|
|
430 | 436 |
if (wl_packet_groups[group] != NULL |
431 | 437 |
&& wl_packet_groups[group]->handle_receive != NULL) |
432 |
wl_packet_groups[group]->handle_receive(type, source,
|
|
438 |
wl_packet_groups[group]->handle_receive(type, source, |
|
433 | 439 |
wl_buf + 7, packetLen); |
434 | 440 |
return; |
435 | 441 |
} |
436 |
|
|
442 |
|
|
437 | 443 |
WL_DEBUG_PRINT("Unexpected packet received from XBee.\r\n"); |
438 | 444 |
return; |
439 | 445 |
} |
440 | 446 |
|
441 | 447 |
|
442 | 448 |
#ifndef ROBOT |
443 |
void wl_set_com_port(char* port){ |
|
444 |
xbee_set_com_port(port); |
|
449 |
void wl_set_com_port(char* port) |
|
450 |
{ |
|
451 |
xbee_set_com_port(port); |
|
445 | 452 |
} |
446 | 453 |
#endif |
447 | 454 |
|
448 |
|
Also available in: Unified diff