Revision 1617
wl: clean up unit tests
branches/wireless/code/lib/include/libwireless/wireless.h | ||
---|---|---|
1 | 1 |
/** |
2 |
* Copyright (c) 2007 Colony Project
|
|
3 |
* |
|
2 |
* Copyright (c) 2009 Colony Project
|
|
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 |
... | ... | |
25 | 25 |
|
26 | 26 |
/** |
27 | 27 |
* @file wireless.h |
28 |
* @brief Contains definitions for the wireless library.
|
|
28 |
* @brief Contains definitions for using the wireless library
|
|
29 | 29 |
* |
30 |
* Contains functions for the wireless library. |
|
30 |
* Contains definitions for interfacing with the |
|
31 |
* wireless library. |
|
31 | 32 |
* |
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club
|
|
33 |
* @author Colony Project, CMU Robotics Club |
|
33 | 34 |
**/ |
34 | 35 |
|
35 | 36 |
#ifndef WIRELESS_H |
36 | 37 |
#define WIRELESS_H |
37 | 38 |
|
38 |
//Note: If this is raised above 16, we will need to do |
|
39 |
//something about frame numbers for TX Status packets. |
|
40 |
/** |
|
41 |
* The maximum number of packet groups. |
|
42 |
**/ |
|
43 |
//TODO: a PacketGroupHandler is at least 10 bytes (I don't know if function pointers are 2 bytes |
|
44 |
// or 4 bytes). That means that in the c file, your array of packet groups is at least 160 bytes. |
|
45 |
// Normally that might be fine (the robot's avr chips have 4k SRAM), but austin's chip only has |
|
46 |
// 1k SRAM, so if this number can be reduced or if the size of the struct could be reduced, that would be a plus. |
|
47 |
#define WL_MAX_PACKET_GROUPS 16 |
|
39 |
// need this for C99 int types |
|
40 |
#ifndef STDINT_H |
|
41 |
#define STDINT_H |
|
42 |
#include <stdint.h> |
|
43 |
#endif |
|
48 | 44 |
|
45 |
|
|
49 | 46 |
/** |
50 | 47 |
* @defgroup wireless Wireless |
51 |
* @brief Wireless definitions.
|
|
48 |
* @brief Interface with the wireless library
|
|
52 | 49 |
* |
53 |
* Contains functions and definitions for dealing with wireless functionality.<br><br>
|
|
50 |
* Interface with the wireless library.
|
|
54 | 51 |
* |
55 |
* The wireless library provides a modular method for dealing with |
|
56 |
* wireless packets, by allowing packet groups to be registered. |
|
57 |
* A packet group is a collection of packets which share a packet |
|
58 |
* group code. Each packet in the group also has a type. A packet |
|
59 |
* group code and type are sent with each packet. When a packet |
|
60 |
* with a group code registered in the wireless library is |
|
61 |
* received, the corresponding event handler is called. The |
|
62 |
* event handler uses the packet type and other information |
|
63 |
* stored in the packet to respond.<br><br> |
|
64 |
* |
|
65 |
* This architecture allows different wireless functionality to be |
|
66 |
* defined and handled separately, making it simpler and more |
|
67 |
* efficient to take advantage of the XBee's wireless functionality. |
|
68 |
* |
|
69 | 52 |
* @{ |
70 | 53 |
**/ |
54 |
|
|
55 |
/**@defgroup wl_defines Public Constants |
|
56 |
* @{ **/ |
|
57 |
|
|
58 |
/**@brief basic group code **/ |
|
59 |
#define BASIC UINT8_C(0) |
|
71 | 60 |
|
61 |
/**@brief global scope **/ |
|
62 |
#define GLOBAL UINT8_C(0) |
|
63 |
|
|
64 |
/**@brief local pan scope **/ |
|
65 |
#define PAN UINT8_C(1) |
|
66 |
|
|
67 |
/**@brief broadcast address **/ |
|
68 |
#define BROADCAST UINT16_C(0xFFFF) |
|
69 |
|
|
70 |
/**@brief reliable (TCP) mode **/ |
|
71 |
#define RELIABLE UINT8_C(0) |
|
72 |
|
|
73 |
/**@brief fast (UDP) mode **/ |
|
74 |
#define FAST UINT8_C(1) |
|
75 |
|
|
76 |
/**@brief standard priority **/ |
|
77 |
#define NORMAL_PRIORITY UINT8_C(0) |
|
78 |
|
|
79 |
/**@brief high priority, handle immediately **/ |
|
80 |
#define HIGH_PRIORITY UINT8_C(1) |
|
81 |
|
|
82 |
/**@brief packet still in sending phase **/ |
|
83 |
#define SENDING UINT8_C(0) |
|
84 |
|
|
85 |
/**@brief packet was send successfully **/ |
|
86 |
#define ACK_OK UINT8_C(1) |
|
87 |
|
|
88 |
/**@brief packet failure - no acknowledgment **/ |
|
89 |
#define ACK_FAILURE UINT8_C(2) |
|
90 |
|
|
91 |
/**@brief packet failure - network too busy **/ |
|
92 |
#define CCA_FAILURE UINT8_C(3) |
|
93 |
|
|
94 |
/**@brief packet handler function pointer type **/ |
|
95 |
#define FUNC func |
|
96 |
|
|
97 |
/**@brief packet handler function pointer type **/ |
|
98 |
#define FNPTR void (*FUNC)(uint8_t* data,uint8_t length,uint8_t source) |
|
99 |
|
|
100 |
// TODO: this max may be too big b/c the packet handler array stores 3*MAX_PACKET_GROUPS bytes |
|
101 |
/**@brief maximum number of packet groups, size of handler array **/ |
|
102 |
#define MAX_PACKET_GROUPS UINT8_C(16) |
|
103 |
|
|
104 |
/**@brief init_flag when library has not been initialized **/ |
|
105 |
#define INIT_NO UINT8_C(0) |
|
106 |
|
|
107 |
/**@brief init_flag when library has been initialized **/ |
|
108 |
#define INIT_YES UINT8_C(1) |
|
109 |
|
|
110 |
/**@brief numer of reliable sending retries **/ |
|
111 |
#define NUM_RETRIES UINT8_C(3) |
|
112 |
|
|
113 |
/**@} **/ // end defines group |
|
114 |
|
|
72 | 115 |
/** |
73 | 116 |
* @struct PacketGroupHandler |
74 | 117 |
* A PacketGroupHandler represents a packet group, and is used to |
75 | 118 |
* register a packet group with the wireless library. It contains |
76 |
* handlers for various events which can occur related to a packet
|
|
77 |
* group. |
|
119 |
* a function pointer to the packet handler function and the priority
|
|
120 |
* of the packet group.
|
|
78 | 121 |
**/ |
79 |
//TODO: the order of member variables in this struct should be changed in case the compile packs the struct |
|
80 |
// In order to achieve the best packing, the variables should be listed in order of decreasing memory size. |
|
81 |
// Thus, pointers should be first, followed by int, followed by char. |
|
82 |
typedef struct |
|
83 |
{ |
|
84 |
/** |
|
85 |
* The group code for this packet group. This number |
|
86 |
* must be unique. The maximum number of packet groups |
|
87 |
* is defined by WL_MAX_PACKET_GROUPS. |
|
88 |
**/ |
|
89 |
//TODO: if this number must be less than or equal to WL_MAX_PACKET_GROUPS, don't you only need |
|
90 |
// one byte for it and it can be made an unsigned char? |
|
91 |
unsigned int groupCode; |
|
122 |
typedef struct { |
|
123 |
|
|
124 |
// function pointer of handler for this packet group |
|
125 |
FNPTR; |
|
92 | 126 |
|
93 |
/** |
|
94 |
* Called every half second (not in interrupt, |
|
95 |
* but in wl_do). |
|
96 |
**/ |
|
97 |
void (*timeout_handler) (void); |
|
127 |
// priority for this packet group |
|
128 |
uint8_t priority; |
|
129 |
|
|
130 |
} PacketGroupHandler; |
|
98 | 131 |
|
99 |
/** |
|
100 |
* Called when a transmit status packet is received |
|
101 |
* from the XBee where the first four bits of the frame |
|
102 |
* are the group code. |
|
103 |
* |
|
104 |
* @param frame the last four bits of the frame |
|
105 |
* @param received is true if we received an ack, 0 if |
|
106 |
* we did not. |
|
107 |
**/ |
|
108 |
void (*handle_response) (int frame, int received); |
|
132 |
static PacketGroupHandler wl_packet_handlers[MAX_PACKET_GROUPS]; |
|
109 | 133 |
|
110 |
/** |
|
111 |
* Called when we receive a packet from this group. |
|
112 |
* |
|
113 |
* @param type the packet type |
|
114 |
* @param source the 16-bit address of the XBee this |
|
115 |
* packet was sent from |
|
116 |
* @param packet the packet received |
|
117 |
* @param length the length of the packet |
|
118 |
**/ |
|
119 |
void (*handle_receive) (char type, int source, unsigned char* packet, int length); |
|
134 |
/**@defgroup wl_functions Public API Functions |
|
135 |
* @{ **/ |
|
136 |
|
|
137 |
// the init function |
|
120 | 138 |
|
121 |
/** |
|
122 |
* Called for any cleanup when the network is turned off. |
|
123 |
**/ |
|
124 |
void (*unregister) (void); |
|
139 |
/**@brief Initialize wireless. **/ |
|
140 |
int8_t wl_init(void); |
|
125 | 141 |
|
126 |
} PacketGroupHandler; |
|
142 |
/**@brief Terminate wireless. **/ |
|
143 |
int8_t wl_terminate(void); |
|
127 | 144 |
|
128 |
/**@brief Initialize the wireless library **/ |
|
129 |
int wl_init(void); |
|
130 |
/**@brief Uninitialize the wireless library **/ |
|
131 |
void wl_terminate(void); |
|
132 |
/**@brief Perform wireless library functionality **/ |
|
133 |
void wl_do(void); |
|
134 |
/**@brief Register a packet group with the wireless library **/ |
|
135 |
void wl_register_packet_group(PacketGroupHandler* h); |
|
136 |
/**@brief Unregister a packet group with the wireless library **/ |
|
137 |
void wl_unregister_packet_group(PacketGroupHandler* h); |
|
138 | 145 |
|
139 |
/**@brief Send a packet to a specific robot in any PAN **/ |
|
140 |
int wl_send_robot_to_robot_global_packet(char group, char type, char* data, int len, int dest, char frame); |
|
141 |
/**@brief Send a packet to a specific robot in our PAN **/ |
|
142 |
int wl_send_robot_to_robot_packet(char group, char type, char* data, int len, int dest, char frame); |
|
143 |
/**@brief Send a packet to all robots **/ |
|
144 |
int wl_send_global_packet(char group, char type, char* data, int len, char frame); |
|
145 |
/**@brief Send a packet to all robots in our PAN **/ |
|
146 |
void wl_send_pan_packet(char group, char type, char* data, int len, char frame); |
|
146 |
// the send functions |
|
147 | 147 |
|
148 |
/**@brief Set the PAN we are using **/ |
|
149 |
int wl_set_pan(int pan); |
|
150 |
/**@brief Get the PAN we are using **/ |
|
151 |
int wl_get_pan(void); |
|
152 |
/**@brief Set the channel we are using **/ |
|
153 |
int wl_set_channel(int channel); |
|
154 |
/**@brief Get the channel we are using **/ |
|
155 |
int wl_get_channel(void); |
|
156 |
/**@brief Get the 16-bit address of the XBee module **/ |
|
157 |
int wl_get_xbee_id(void); |
|
158 |
/**@brief Set the com port on a computer, undefined on the robot.**/ |
|
159 |
void wl_set_com_port(char* port); |
|
148 |
/**@brief The core send function. This will take all possible arguments and send all types of packets. **/ |
|
149 |
int16_t wl_send(uint8_t *data, uint8_t length, uint8_t group, uint8_t scope, uint16_t dest, uint8_t mode); |
|
160 | 150 |
|
161 |
/** @} **/ // end defgroup |
|
151 |
/**@brief Wrapper for core send function that will send a global packet across the current channel. **/ |
|
152 |
int16_t wl_send_global(uint8_t *data, uint8_t length, uint8_t group); |
|
162 | 153 |
|
154 |
/**@brief Wrapper for core send function that will send a packet across the current channel on the current pan. **/ |
|
155 |
int16_t wl_send_pan(uint8_t *data, uint8_t length, uint8_t group); |
|
156 |
|
|
157 |
/**@brief Wrapper for core send function that will send a packet across the current channel to a specific robot. **/ |
|
158 |
int16_t wl_send_robot(uint8_t *data, uint8_t length, uint8_t group, uint16_t dest, uint8_t mode); |
|
159 |
|
|
160 |
/**@brief Default (i.e. basic) send wrapper. **/ |
|
161 |
int16_t wl_send_basic(uint8_t *data, uint8_t length); |
|
162 |
|
|
163 |
|
|
164 |
// the ack function |
|
165 |
|
|
166 |
/**@brief Returns the number of acknowledgment errors. **/ |
|
167 |
uint8_t wl_ack_error(void); |
|
168 |
|
|
169 |
/**@brief Checks a specific packet for the acknowledgement status. **/ |
|
170 |
int8_t wl_ack_check(uint8_t packet); |
|
171 |
|
|
172 |
/**@brief Resets acknowledgement statistics back to zero. **/ |
|
173 |
void wl_ack_reset(void); |
|
174 |
|
|
175 |
|
|
176 |
// the receive functions |
|
177 |
|
|
178 |
/**@brief The main receive function. Dispatches packets for registered handlers and returns next basic packet if available. **/ |
|
179 |
int8_t wl_get(char *data, uint8_t length); |
|
180 |
|
|
181 |
/**@brief Returns the next basic packet if available. **/ |
|
182 |
int8_t wl_get_basic(char *data, uint8_t length); |
|
183 |
|
|
184 |
/**@brief Dispatches packets for registered handlers. **/ |
|
185 |
int8_t wl_dispatch(void); |
|
186 |
|
|
187 |
|
|
188 |
// the group register function |
|
189 |
|
|
190 |
/**@brief Function to register new packet handlers (for non-default groups only). **/ |
|
191 |
int8_t wl_register_handler(uint8_t group, FNPTR, uint8_t priority); |
|
192 |
|
|
193 |
/**@brief Function to unregister existing packet handlers (for non-default groups only). **/ |
|
194 |
int8_t wl_unregister_handler(uint8_t group); |
|
195 |
|
|
196 |
/**@} **/ //end functions group |
|
197 |
|
|
198 |
/**@} **/ //end wireless group |
|
199 |
|
|
163 | 200 |
#endif |
164 |
|
branches/wireless/code/lib/include/libwireless/wl_defs.h | ||
---|---|---|
1 | 1 |
/** |
2 |
* Copyright (c) 2007 Colony Project
|
|
2 |
* Copyright (c) 2009 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 |
... | ... | |
27 | 27 |
* @file wl_defs.h |
28 | 28 |
* @brief Definitions for Wireless |
29 | 29 |
* |
30 |
* Contains definitions for wireless packet groups, packet types, |
|
31 |
* debugging information, etc. |
|
30 |
* Contains definitions for wireless, debugging information, etc. |
|
32 | 31 |
* |
33 |
* @author Brian Coltin, Colony Project, CMU Robotics Club
|
|
32 |
* @author Colony Project, CMU Robotics Club |
|
34 | 33 |
**/ |
35 | 34 |
|
36 | 35 |
#ifndef WL_DEFS_H |
37 | 36 |
#define WL_DEFS_H |
38 | 37 |
|
38 |
// create main wireless page documentation |
|
39 |
|
|
40 |
/** @mainpage The Wireless Library |
|
41 |
* |
|
42 |
* This is the documentation for the Colony wireless library.\n\n |
|
43 |
* Xbee documentation can be found under @ref xbee \n |
|
44 |
* Higher level wireless documentation can be found under @ref wireless \n |
|
45 |
**/ |
|
46 |
|
|
47 |
|
|
48 |
// need this for C99 int types |
|
49 |
#ifndef STDINT_H |
|
50 |
#define STDINT_H |
|
51 |
#include <stdint.h> |
|
52 |
#endif |
|
53 |
|
|
54 |
// ***** TODO: these next two defines should be moved to makefile options |
|
55 |
|
|
39 | 56 |
//comment out this line if using a computer hooked up to an xbee |
40 | 57 |
//#define ROBOT |
41 | 58 |
|
42 | 59 |
//uncomment this line for debug information |
43 | 60 |
//#define WL_DEBUG |
44 | 61 |
|
45 |
//return value definitions |
|
62 |
/**@addtogroup wireless |
|
63 |
* @{ **/ |
|
64 |
|
|
65 |
/**@defgroup wl_error Error Codes |
|
66 |
* @{ **/ |
|
67 |
|
|
68 |
/** @brief Success **/ |
|
69 |
#define WL_SUCCESS INT8_C(0) |
|
70 |
|
|
46 | 71 |
/** @brief Error code for init failure **/ |
47 |
#define WL_ERROR_INIT_FAILED 1 |
|
72 |
#define WL_ERROR_INIT_FAILED INT8_C(-1) |
|
73 |
|
|
48 | 74 |
/** @brief Error code for duplicate init calls **/ |
49 |
#define WL_ERROR_INIT_ALREADY_INITD 2 |
|
75 |
#define WL_ERROR_INIT_ALREADY_INITD INT8_C(-2) |
|
76 |
|
|
50 | 77 |
/** @brief Error code for not calling init **/ |
51 |
#define WL_ERROR_LIBRARY_NOT_INITD 3
|
|
78 |
#define WL_ERROR_LIBRARY_NOT_INITD INT8_C(-3)
|
|
52 | 79 |
|
53 |
// Packet Groups and Types |
|
80 |
/** @brief Error code for failed termination **/ |
|
81 |
#define WL_ERROR_TERMINATION_FAILED INT8_C(-4) |
|
54 | 82 |
|
55 |
// Error group
|
|
56 |
#define WL_ERROR_GROUP 1
|
|
83 |
/** @brief Error code for failed packet group registration **/
|
|
84 |
#define WL_ERROR_FAILED_REGISTRATION INT8_C(-5)
|
|
57 | 85 |
|
58 |
#define WL_ERROR_STRING_TYPE 1 |
|
86 |
/** @brief Error code for failed packet group registration **/ |
|
87 |
#define WL_ERROR_FAILED_UNREGISTRATION INT8_C(-6) |
|
59 | 88 |
|
60 |
// Token Ring group
|
|
61 |
#define WL_TOKEN_RING_GROUP 2
|
|
89 |
/** @brief Error in arguments **/
|
|
90 |
#define WL_ERROR_ARGUMENT INT8_C(-7)
|
|
62 | 91 |
|
63 |
#define WL_TOKEN_PASS 1 |
|
64 |
#define WL_TOKEN_SENSOR_MATRIX 2 |
|
65 |
#define WL_TOKEN_BOM_ON 3 |
|
66 |
#define WL_TOKEN_JOIN 4 |
|
67 |
#define WL_TOKEN_JOIN_ACCEPT 5 |
|
92 |
/** @brief Error code for send failure **/ |
|
93 |
#define WL_ERROR_SEND INT8_C(-10) |
|
68 | 94 |
|
69 |
// timing constants |
|
70 |
#ifndef FIREFLY |
|
71 |
#define BOM_DELAY 100 |
|
72 |
#else |
|
73 |
#define BOM_DELAY 200 |
|
74 |
#endif |
|
95 |
/** @brief Error code for a bad group number **/ |
|
96 |
#define WL_ERROR_BAD_GROUP INT8_C(-11) |
|
75 | 97 |
|
76 |
#define DEATH_DELAY 4
|
|
77 |
#define JOIN_DELAY 8
|
|
98 |
/** @brief Error code for a bad scope **/
|
|
99 |
#define WL_ERROR_SCOPE INT8_C(-12)
|
|
78 | 100 |
|
101 |
/** @brief Error code for a bad robot address **/ |
|
102 |
#define WL_ERROR_ADDRESS INT8_C(-13) |
|
103 |
|
|
104 |
/** @brief Error code for a bad mode **/ |
|
105 |
#define WL_ERROR_MODE INT8_C(-14) |
|
106 |
|
|
107 |
/** @brief Error code for received paacket destination too small for received packet **/ |
|
108 |
#define WL_ERROR_TOO_SMALL INT8_C(-15) |
|
109 |
|
|
110 |
/** @brief Error code for sending buffer full (the packet was sent, but no retries will be made). **/ |
|
111 |
#define WL_ERROR_SENDING_BUFFER_FULL INT8_C(-16) |
|
112 |
|
|
113 |
/** @brief Error code for failed xbee command **/ |
|
114 |
#define WL_ERROR_XBEE_COMMAND INT8_C(-17) |
|
115 |
|
|
116 |
/** @brief Error code for failed 16bit xbee command **/ |
|
117 |
#define WL_ERROR_XBEE_COMMAND_16BIT 0xFEFE |
|
118 |
|
|
119 |
/** @brief Error code for bad baud rate **/ |
|
120 |
#define WL_ERROR_BAUD INT8_C(-18) |
|
121 |
|
|
122 |
/**@} */ // end error group |
|
123 |
|
|
124 |
// TODO: is this a good size? |
|
125 |
/*Buffer sizes*/ |
|
126 |
#define PACKET_BUFFER_SIZE 128 |
|
127 |
|
|
128 |
/**@} */ // end wireless group |
|
129 |
|
|
79 | 130 |
#ifdef WL_DEBUG |
80 | 131 |
|
81 | 132 |
#ifdef ROBOT |
82 | 133 |
#include <serial.h> |
83 |
#endif |
|
84 |
|
|
85 |
#ifdef ROBOT |
|
86 | 134 |
#define WL_DEBUG_PRINT( s ) usb_puts( s ) |
135 |
#define WL_DEBUG_PRINT_CHAR( c ) usb_putc( c ) |
|
136 |
#define WL_DEBUG_PRINT_INT( i ) usb_puti(i) |
|
87 | 137 |
#else |
88 | 138 |
#define WL_DEBUG_PRINT( s ) printf( s ) |
89 |
#endif |
|
90 |
|
|
91 |
#ifdef ROBOT |
|
92 |
#define WL_DEBUG_PRINT_INT( i ) usb_puti(i) |
|
93 |
#else |
|
139 |
#define WL_DEBUG_PRINT_CHAR( c ) printf("%c", c) |
|
94 | 140 |
#define WL_DEBUG_PRINT_INT( i ) printf("%i", i) |
95 | 141 |
#endif |
96 | 142 |
|
97 | 143 |
#else |
98 | 144 |
|
99 | 145 |
#define WL_DEBUG_PRINT( s ) |
146 |
#define WL_DEBUG_PRINT_CHAR( c ) |
|
100 | 147 |
#define WL_DEBUG_PRINT_INT( i ) |
101 | 148 |
|
102 | 149 |
#endif |
103 | 150 |
|
104 | 151 |
#endif |
105 |
|
branches/wireless/code/lib/include/libwireless/xbee.h | ||
---|---|---|
1 | 1 |
/** |
2 |
* Copyright (c) 2007 Colony Project
|
|
2 |
* Copyright (c) 2009 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 |
... | ... | |
32 | 32 |
* To use a robot, define ROBOT in wl_defs.h, and |
33 | 33 |
* to use a computer, don't define ROBOT. |
34 | 34 |
* |
35 |
* @author Brian Coltin, Colony Project, CMU Robotics Club
|
|
35 |
* @author Colony Project, CMU Robotics Club |
|
36 | 36 |
**/ |
37 | 37 |
|
38 | 38 |
#ifndef XBEE_H |
39 | 39 |
#define XBEE_H |
40 | 40 |
|
41 |
/** |
|
42 |
* The port to use the XBee from on the computer. |
|
43 |
* Also, a backup port if the other is used. |
|
44 |
**/ |
|
45 |
#ifndef ROBOT |
|
46 |
#define XBEE_PORT_DEFAULT "/dev/ttyUSB1" |
|
41 |
// need this for C99 int types |
|
42 |
#ifndef STDINT_H |
|
43 |
#define STDINT_H |
|
44 |
#include <stdint.h> |
|
47 | 45 |
#endif |
48 | 46 |
|
47 |
|
|
49 | 48 |
/** |
50 | 49 |
* @defgroup xbee XBee |
51 | 50 |
* @brief Interface with the XBee module |
... | ... | |
54 | 53 |
* |
55 | 54 |
* @{ |
56 | 55 |
**/ |
57 |
|
|
58 |
/*Definitions*/ |
|
56 |
|
|
57 |
/**@defgroup xbee_const xbee constants |
|
58 |
* @brief These are constants used for the xbee module. |
|
59 |
* @{ **/ |
|
60 |
|
|
61 |
#ifndef XBEE_BAUD |
|
62 |
/** @brief The XBee baud rate **/ |
|
63 |
#define XBEE_BAUD 115200 |
|
64 |
#endif |
|
65 |
|
|
66 |
/**@name xbee options |
|
67 |
* @{ **/ |
|
68 |
|
|
59 | 69 |
/**@brief Unset PAN, uses XBee default **/ |
60 | 70 |
#define XBEE_PAN_DEFAULT 0xFFFF |
61 | 71 |
/**@brief Unset channel, uses XBee default **/ |
... | ... | |
73 | 83 |
/**@brief A packet received from another XBee **/ |
74 | 84 |
#define XBEE_RX 0x81 |
75 | 85 |
|
86 |
/**@} |
|
87 |
* @name xbee frame types |
|
88 |
* @{ **/ |
|
89 |
|
|
90 |
// TODO: add comments for all of these definitions |
|
91 |
|
|
92 |
/*Frame Types*/ |
|
93 |
#define XBEE_FRAME_START 0x7E |
|
94 |
#define XBEE_FRAME_STATUS 0x8A |
|
95 |
#define XBEE_FRAME_AT_COMMAND 0x08 |
|
96 |
#define XBEE_FRAME_AT_COMMAND_RESPONSE 0x88 |
|
97 |
#define XBEE_FRAME_TX_REQUEST_64 0x00 |
|
98 |
#define XBEE_FRAME_TX_REQUEST_16 0x01 |
|
99 |
#define XBEE_FRAME_TX_STATUS XBEE_TX_STATUS |
|
100 |
#define XBEE_FRAME_RX_64 0x80 |
|
101 |
#define XBEE_FRAME_RX_16 XBEE_RX |
|
102 |
|
|
103 |
/** @} **/ |
|
104 |
|
|
105 |
/**@} **/ // end const group |
|
106 |
|
|
107 |
/** |
|
108 |
* @defgroup xbee_funcs Xbee Functions |
|
109 |
* @brief These are the public xbee functions. |
|
110 |
* @{ |
|
111 |
**/ |
|
112 |
|
|
76 | 113 |
/**@brief Initialize the XBee library **/ |
77 |
int xbee_lib_init(void);
|
|
114 |
int8_t xbee_init(void);
|
|
78 | 115 |
/**@brief Uninitialize the XBee library **/ |
79 |
void xbee_terminate(void); |
|
80 |
/**@brief Get a packet from the XBee **/ |
|
81 |
int xbee_get_packet(unsigned char* packet); |
|
116 |
int8_t xbee_terminate(void); |
|
117 |
/**@brief Send a byte to the xbee **/ |
|
118 |
int8_t xbee_putc(uint8_t c); |
|
119 |
/**@brief Read a byte form the xbee **/ |
|
120 |
int16_t xbee_getc(void); |
|
121 |
/**@brief Read a byte from the xbee (nonblocking) **/ |
|
122 |
int8_t xbee_getc_nb(uint8_t *c); |
|
123 |
/**@brief Send an array of bytes to the xbee **/ |
|
124 |
int8_t xbee_send(uint8_t* buf, uint16_t size); |
|
125 |
/**@brief Add a buffer to the checksum value **/ |
|
126 |
int8_t xbee_checksum_add(uint8_t *buf, uint8_t len, uint8_t* sum); |
|
127 |
/**@brief Send a frame header to the xbee **/ |
|
128 |
int8_t xbee_send_header(uint8_t type, uint16_t len); |
|
82 | 129 |
/**@brief Send a packet to the XBee **/ |
83 |
int xbee_send_packet(char* packet, int len, int dest, char options, char frame);
|
|
130 |
int8_t xbee_send_packet(uint8_t* packet, uint8_t len, uint16_t dest, uint8_t options, uint8_t frame);
|
|
84 | 131 |
/**@brief Set the PAN ID for the XBee **/ |
85 |
int xbee_set_pan_id(int id);
|
|
132 |
int8_t xbee_set_pan_id(uint16_t id);
|
|
86 | 133 |
/**@brief Get the XBee's PAN ID **/ |
87 |
unsigned int xbee_get_pan_id(void);
|
|
134 |
uint16_t xbee_get_pan_id(void);
|
|
88 | 135 |
/**@brief Set the channel the XBee is currently using **/ |
89 |
int xbee_set_channel(int channel);
|
|
136 |
int8_t xbee_set_channel(uint8_t channel);
|
|
90 | 137 |
/**@brief Get the channel the XBee is currently using **/ |
91 |
int xbee_get_channel(void); |
|
138 |
int8_t xbee_get_channel(void);
|
|
92 | 139 |
/**@brief Get the XBee's 16-bit address **/ |
93 |
unsigned int xbee_get_address(void); |
|
94 |
/**@brief Set the com port on a computer, undefined on the robot**/ |
|
95 |
void xbee_set_com_port(char* port); |
|
140 |
uint16_t xbee_get_address(void); |
|
96 | 141 |
/**@brief Reset XBee **/ |
97 |
int xbee_reset(void);
|
|
142 |
int8_t xbee_reset(void); // TODO: implement this function
|
|
98 | 143 |
|
99 |
/**@}**/ //end defgroup
|
|
144 |
/**@} **/ //end xbee_funcs group
|
|
100 | 145 |
|
146 |
/**@} **/ //end defgroup |
|
147 |
|
|
101 | 148 |
#endif |
branches/wireless/code/projects/unit_tests/test_tokenring.c | ||
---|---|---|
1 |
#include <dragonfly_lib.h> |
|
2 |
#include <wireless.h> |
|
3 |
#include <wl_token_ring.h> |
|
4 |
|
|
5 |
|
|
6 |
/** |
|
7 |
* Tests the token ring and BOM |
|
8 |
* - prints table of bom values for every robot in token ring |
|
9 |
* - takes several seconds to initialize token ring |
|
10 |
*/ |
|
11 |
|
|
12 |
#define MAX_ROBOTS 16 |
|
13 |
|
|
14 |
// set a list of integers to 0 |
|
15 |
void clearRobots(int* list) { |
|
16 |
for(int i=0;i<MAX_ROBOTS;i++) |
|
17 |
list[i] = 0; |
|
18 |
} |
|
19 |
|
|
20 |
int testtokenring(void) { |
|
21 |
usb_init(); |
|
22 |
usb_puts("usb turned on\r\n"); |
|
23 |
wl_init(); |
|
24 |
usb_puts("wireless turned on\r\n"); |
|
25 |
wl_token_ring_register(); |
|
26 |
wl_token_ring_join(); // join token ring |
|
27 |
usb_puts("token ring joined\r\n"); |
|
28 |
int robotList[sizeof(int)*MAX_ROBOTS]; |
|
29 |
int numRobots = 0; |
|
30 |
delay_ms(1000); |
|
31 |
|
|
32 |
// start testing wireless/token ring/BOM |
|
33 |
int i=0; |
|
34 |
while(1) { |
|
35 |
wl_do(); |
|
36 |
// only print table every 200 loops |
|
37 |
if (i%200==0) { |
|
38 |
// get token ring size values |
|
39 |
usb_puts("\r\nnumber of robots in token ring:"); |
|
40 |
usb_puti(wl_token_get_robots_in_ring()); |
|
41 |
usb_puts("\r\nnumber of robots in matrix:"); |
|
42 |
usb_puti(wl_token_get_num_robots()); |
|
43 |
|
|
44 |
// get list of robots |
|
45 |
numRobots = 0; |
|
46 |
clearRobots(robotList); |
|
47 |
wl_token_iterator_begin(); |
|
48 |
while(wl_token_iterator_has_next()) { |
|
49 |
int tmp = wl_token_iterator_next(); |
|
50 |
if (tmp < 0) |
|
51 |
break; |
|
52 |
robotList[tmp] = 1; |
|
53 |
numRobots++; |
|
54 |
} |
|
55 |
if (numRobots < 1) { |
|
56 |
usb_puts("\r\nNo BOM table available."); |
|
57 |
continue; // skip table printing |
|
58 |
} else { |
|
59 |
usb_puts("\r\nBOM table: (* indicates this robot)"); |
|
60 |
} |
|
61 |
|
|
62 |
// print table of bom readings between robots |
|
63 |
usb_puts("\r\ns \\ d"); |
|
64 |
// print header |
|
65 |
for(int j=0;j<MAX_ROBOTS;j++) |
|
66 |
if (robotList[j]) { |
|
67 |
usb_puts("\t|"); |
|
68 |
if (j == wl_get_xbee_id()) |
|
69 |
usb_puts("*"); // indicate that this is the current bot |
|
70 |
usb_puti(j); |
|
71 |
} |
|
72 |
usb_puts("\r\n"); |
|
73 |
// print body |
|
74 |
for(int l=0;l<MAX_ROBOTS;l++) |
|
75 |
if (robotList[l]) { |
|
76 |
if (l == wl_get_xbee_id()) |
|
77 |
usb_puts("*"); // indicate that this is the current bot |
|
78 |
usb_puti(l); // print label col |
|
79 |
for(int k=0;k<MAX_ROBOTS;k++) |
|
80 |
if (robotList[k]) { |
|
81 |
usb_puts("\t|"); |
|
82 |
if (k != l) { |
|
83 |
int bom = wl_token_get_sensor_reading(l,k); |
|
84 |
if (bom >= 0 && bom <= 15) |
|
85 |
usb_puti(bom); // print bom value |
|
86 |
} |
|
87 |
} |
|
88 |
usb_puts("\r\n"); |
|
89 |
} |
|
90 |
usb_puts("\r\n"); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
// end testing token ring and bom |
|
95 |
return 0; |
|
96 |
} |
|
97 |
|
|
98 |
|
|
99 | 0 |
branches/wireless/code/projects/unit_tests/xbee.c | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (c) 2007 Colony Project |
|
3 |
* |
|
4 |
* Permission is hereby granted, free of charge, to any person |
|
5 |
* obtaining a copy of this software and associated documentation |
|
6 |
* files (the "Software"), to deal in the Software without |
|
7 |
* restriction, including without limitation the rights to use, |
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
* copies of the Software, and to permit persons to whom the |
|
10 |
* Software is furnished to do so, subject to the following |
|
11 |
* conditions: |
|
12 |
* |
|
13 |
* The above copyright notice and this permission notice shall be |
|
14 |
* included in all copies or substantial portions of the Software. |
|
15 |
* |
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
* OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
**/ |
|
25 |
|
|
26 |
/** |
|
27 |
* @file xbee.c |
|
28 |
* @brief XBee Interface |
|
29 |
* |
|
30 |
* Implementation of low level communication with the XBee in API mode. |
|
31 |
* |
|
32 |
* @author Brian Coltin, Colony Project, CMU Robotics Club |
|
33 |
**/ |
|
34 |
|
|
35 |
#include <xbee.h> |
|
36 |
#include <wl_defs.h> |
|
37 |
|
|
38 |
#define ROBOT |
|
39 |
#define WL_DEBUG |
|
40 |
#define WL_DEBUG_PRINT( s ) usb_puts( s ) |
|
41 |
#define WL_DEBUG_PRINT_INT( i ) usb_puti(i) |
|
42 |
|
|
43 |
#ifndef ROBOT |
|
44 |
|
|
45 |
#include <fcntl.h> |
|
46 |
#include <unistd.h> |
|
47 |
#include <pthread.h> |
|
48 |
#include <errno.h> |
|
49 |
#include <termios.h> |
|
50 |
|
|
51 |
#else |
|
52 |
|
|
53 |
#include <serial.h> |
|
54 |
#include <avr/interrupt.h> |
|
55 |
|
|
56 |
#endif |
|
57 |
|
|
58 |
#include <stdio.h> |
|
59 |
#include <stdlib.h> |
|
60 |
#include <string.h> |
|
61 |
|
|
62 |
#define XBEE_FRAME_START 0x7E |
|
63 |
#define XBEE_GET_PACKET_TIMEOUT 1000 |
|
64 |
|
|
65 |
/*Frame Types*/ |
|
66 |
#define XBEE_FRAME_STATUS 0x8A |
|
67 |
#define XBEE_FRAME_AT_COMMAND 0x08 |
|
68 |
#define XBEE_FRAME_AT_COMMAND_RESPONSE 0x88 |
|
69 |
#define XBEE_FRAME_TX_REQUEST_64 0x00 |
|
70 |
#define XBEE_FRAME_TX_REQUEST_16 0x01 |
|
71 |
#define XBEE_FRAME_TX_STATUS XBEE_TX_STATUS |
|
72 |
#define XBEE_FRAME_RX_64 0x80 |
|
73 |
#define XBEE_FRAME_RX_16 XBEE_RX |
|
74 |
|
|
75 |
/*Internal Function Prototypes*/ |
|
76 |
|
|
77 |
/*I/O Functions*/ |
|
78 |
static int xbee_send(char* buf, int size); |
|
79 |
static int xbee_send_string(char* c); |
|
80 |
|
|
81 |
#ifndef ROBOT |
|
82 |
static int xbee_read(char* buf, int size); |
|
83 |
#endif |
|
84 |
|
|
85 |
/*Command Mode Functions |
|
86 |
* Called during initialization. |
|
87 |
*/ |
|
88 |
static int xbee_enter_command_mode(void); |
|
89 |
static int xbee_exit_command_mode(void); |
|
90 |
static int xbee_enter_api_mode(void); |
|
91 |
static int xbee_exit_api_mode(void); |
|
92 |
static int xbee_wait_for_string(char* s, int len); |
|
93 |
static int xbee_wait_for_ok(void); |
|
94 |
|
|
95 |
/*API Mode Functions*/ |
|
96 |
|
|
97 |
static int xbee_handle_packet(char* packet, int len); |
|
98 |
static void xbee_handle_at_command_response(char* command, char result, char* extra, int extraLen); |
|
99 |
static void xbee_handle_status(char status); |
|
100 |
static int xbee_verify_checksum(char* packet, int len); |
|
101 |
static char xbee_compute_checksum(char* packet, int len); |
|
102 |
static int xbee_send_frame(char* buf, int len); |
|
103 |
int xbee_send_read_at_command(char* command); |
|
104 |
static int xbee_send_modify_at_command(char* command, char* value); |
|
105 |
|
|
106 |
/*Global Variables*/ |
|
107 |
|
|
108 |
#ifndef ROBOT |
|
109 |
static char* xbee_com_port = XBEE_PORT_DEFAULT; |
|
110 |
static int xbee_stream; |
|
111 |
static pthread_t* xbee_listen_thread; |
|
112 |
#endif |
|
113 |
|
|
114 |
// TODO: is this a good size? |
|
115 |
#define XBEE_BUFFER_SIZE 128 |
|
116 |
#define PACKET_BUFFER_SIZE 108 |
|
117 |
// a buffer for data received from the XBee |
|
118 |
char arrival_buf[XBEE_BUFFER_SIZE]; |
|
119 |
// location of last unread byte in buffer |
|
120 |
volatile int buffer_last = 0; |
|
121 |
// first unread byte in buffer |
|
122 |
volatile int buffer_first = 0; |
|
123 |
|
|
124 |
|
|
125 |
//used to store packets as they are read |
|
126 |
static char xbee_buf[PACKET_BUFFER_SIZE]; |
|
127 |
static int currentBufPos = 0; |
|
128 |
|
|
129 |
//XBee status |
|
130 |
static unsigned int xbee_panID = XBEE_PAN_DEFAULT; |
|
131 |
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT; |
|
132 |
static int xbee_channel = XBEE_CHANNEL_DEFAULT; |
|
133 |
static int xbee_pending_channel = XBEE_CHANNEL_DEFAULT; |
|
134 |
static volatile unsigned int xbee_address = 0; |
|
135 |
|
|
136 |
/*Function Implementations*/ |
|
137 |
|
|
138 |
#ifdef ROBOT |
|
139 |
|
|
140 |
/** |
|
141 |
* Interrupt for the robot. Adds bytes received from the xbee |
|
142 |
* to the buffer. |
|
143 |
**/ |
|
144 |
#ifndef FIREFLY |
|
145 |
ISR(USART1_RX_vect) |
|
146 |
{ |
|
147 |
char c = UDR1; |
|
148 |
arrival_buf[buffer_last] = c; |
|
149 |
int t = buffer_last + 1; |
|
150 |
if (t == XBEE_BUFFER_SIZE) |
|
151 |
t = 0; |
|
152 |
if (t == buffer_first) |
|
153 |
{ |
|
154 |
WL_DEBUG_PRINT("\nOut of space in buffer.\n"); |
|
155 |
} |
|
156 |
buffer_last = t; |
|
157 |
} |
|
158 |
#else |
|
159 |
SIGNAL(SIG_USART0_RECV) |
|
160 |
{ |
|
161 |
char c = UDR0; |
|
162 |
arrival_buf[buffer_last] = c; |
|
163 |
int t = buffer_last + 1; |
|
164 |
if (t == XBEE_BUFFER_SIZE) |
|
165 |
t = 0; |
|
166 |
if (t == buffer_first) |
|
167 |
{ |
|
168 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
169 |
} |
|
170 |
buffer_last = t; |
|
171 |
} |
|
172 |
#endif |
|
173 |
|
|
174 |
#else |
|
175 |
|
|
176 |
// Computer code |
|
177 |
|
|
178 |
/** |
|
179 |
* Thread that listens to the xbee. |
|
180 |
**/ |
|
181 |
static void* listen_to_xbee(void* x) |
|
182 |
{ |
|
183 |
char c; |
|
184 |
while (1) |
|
185 |
{ |
|
186 |
if (xbee_read(&c, 1) != 0) { |
|
187 |
WL_DEBUG_PRINT("xbee_read failed.\n"); |
|
188 |
return NULL; |
|
189 |
} |
|
190 |
|
|
191 |
arrival_buf[buffer_last] = c; |
|
192 |
int t = buffer_last + 1; |
|
193 |
if (t == XBEE_BUFFER_SIZE) |
|
194 |
t = 0; |
|
195 |
if (t == buffer_first) |
|
196 |
{ |
|
197 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
198 |
} |
|
199 |
buffer_last = t; |
|
200 |
|
|
201 |
usleep(1000); |
|
202 |
} |
|
203 |
|
|
204 |
return NULL; |
|
205 |
} |
|
206 |
|
|
207 |
#endif |
|
208 |
|
|
209 |
/** |
|
210 |
* Initializes the XBee library so that other functions may be used. |
|
211 |
**/ |
|
212 |
int xbee_lib_init() |
|
213 |
{ |
|
214 |
WL_DEBUG_PRINT("in xbee_init\n"); |
|
215 |
#ifdef ROBOT |
|
216 |
|
|
217 |
//enable the receiving interrupt |
|
218 |
#ifdef FIREFLY |
|
219 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
|
220 |
#else |
|
221 |
#ifdef BAYBOARD |
|
222 |
UCSR1B |= _BV(RXCIE1); |
|
223 |
#else |
|
224 |
UCSR1B |= _BV(RXCIE); |
|
225 |
#endif |
|
226 |
#endif |
|
227 |
sei(); |
|
228 |
#else |
|
229 |
xbee_stream = open(xbee_com_port, O_RDWR); |
|
230 |
if (xbee_stream == -1/* || lockf(xbee_stream, F_TEST, 0) != 0*/) |
|
231 |
{ |
|
232 |
WL_DEBUG_PRINT("Failed to open connection to XBee on port "); |
|
233 |
WL_DEBUG_PRINT_INT(xbee_com_port); |
|
234 |
WL_DEBUG_PRINT(".\n"); |
|
235 |
return -1; |
|
236 |
} else { |
|
237 |
WL_DEBUG_PRINT("Successfully opened connection to XBee on port "); |
|
238 |
WL_DEBUG_PRINT_INT(xbee_com_port); |
|
239 |
WL_DEBUG_PRINT(".\n"); |
|
240 |
} |
|
241 |
|
|
242 |
// set baud rate, etc. correctly |
|
243 |
struct termios options; |
|
244 |
|
|
245 |
tcgetattr(xbee_stream, &options); |
|
246 |
cfsetispeed(&options, B9600); |
|
247 |
cfsetospeed(&options, B9600); |
|
248 |
options.c_iflag &= ~ICRNL; |
|
249 |
options.c_oflag &= ~OCRNL; |
|
250 |
options.c_cflag |= (CLOCAL | CREAD); |
|
251 |
options.c_cflag &= ~PARENB; |
|
252 |
options.c_cflag &= ~CSTOPB; |
|
253 |
options.c_cflag &= ~CSIZE; |
|
254 |
options.c_cflag |= CS8; |
|
255 |
options.c_lflag &= ~ICANON; |
|
256 |
options.c_cc[VMIN] = 1; |
|
257 |
options.c_cc[VTIME] = 50; |
|
258 |
|
|
259 |
if (tcsetattr(xbee_stream, TCSANOW, &options)) |
|
260 |
{ |
|
261 |
WL_DEBUG_PRINT("Error setting attributes.\n"); |
|
262 |
return -1; |
|
263 |
} |
|
264 |
|
|
265 |
xbee_listen_thread = (pthread_t*)malloc(sizeof(pthread_t)); |
|
266 |
if (xbee_listen_thread == NULL) |
|
267 |
{ |
|
268 |
WL_DEBUG_PRINT("Malloc failed.\n"); |
|
269 |
return -1; |
|
270 |
} |
|
271 |
|
|
272 |
int ret = pthread_create(xbee_listen_thread, NULL, listen_to_xbee, NULL); |
|
273 |
if (ret) |
|
274 |
{ |
|
275 |
WL_DEBUG_PRINT("Failed to create listener thread.\n"); |
|
276 |
return -1; |
|
277 |
} |
|
278 |
#endif |
|
279 |
|
|
280 |
WL_DEBUG_PRINT("Entering command mode.\n"); |
|
281 |
|
|
282 |
if (xbee_enter_command_mode() != 0) { |
|
283 |
return -1; |
|
284 |
} |
|
285 |
|
|
286 |
WL_DEBUG_PRINT("Entered command mode.\n"); |
|
287 |
|
|
288 |
if (xbee_enter_api_mode() != 0) { |
|
289 |
return -1; |
|
290 |
} |
|
291 |
|
|
292 |
WL_DEBUG_PRINT("Entered api mode.\n"); |
|
293 |
|
|
294 |
if (xbee_exit_command_mode() != 0) { |
|
295 |
return -1; |
|
296 |
} |
|
297 |
|
|
298 |
WL_DEBUG_PRINT("Left command mode.\n"); |
|
299 |
|
|
300 |
if (xbee_send_read_at_command("MY")) { |
|
301 |
return -1; |
|
302 |
} |
|
303 |
WL_DEBUG_PRINT("Getting ATMY address.\n"); |
|
304 |
|
|
305 |
#ifndef ROBOT |
|
306 |
int i; |
|
307 |
for (i = 0; xbee_address == 0 && i < XBEE_GET_PACKET_TIMEOUT; i++) { |
|
308 |
ret = xbee_get_packet(NULL); |
|
309 |
|
|
310 |
usleep(1000); |
|
311 |
|
|
312 |
/* if (ret == -1) { */ |
|
313 |
/* WL_DEBUG_PRINT("xbee_get_packet(NULL) failed.\n"); */ |
|
314 |
/* return -1; */ |
|
315 |
/* } */ |
|
316 |
} |
|
317 |
#else |
|
318 |
//wait to return until the address is set |
|
319 |
//TODO: this shouldn't wait indefinitely. There should be some sort of reasonable timeout |
|
320 |
// so if the address is never set right, an error can be returned instead of having the |
|
321 |
// robot hang forever |
|
322 |
while (xbee_address == 0) { |
|
323 |
xbee_get_packet(NULL); |
|
324 |
} |
|
325 |
#endif |
|
326 |
WL_DEBUG_PRINT("Got ATMY address.\n"); |
|
327 |
|
|
328 |
#ifndef ROBOT |
|
329 |
if (i == XBEE_GET_PACKET_TIMEOUT) { // We timed-out. |
|
330 |
|
|
331 |
WL_DEBUG_PRINT("xbee_get_packet timed out.\n"); |
|
332 |
return -1; |
|
333 |
} else { |
|
334 |
return 0; |
|
335 |
} |
|
336 |
#else |
|
337 |
return 0; |
|
338 |
#endif |
|
339 |
} |
|
340 |
|
|
341 |
/** |
|
342 |
* Call when finished using the XBee library. This releases |
|
343 |
* all sued resources. |
|
344 |
**/ |
|
345 |
void xbee_terminate() |
|
346 |
{ |
|
347 |
#ifndef ROBOT |
|
348 |
pthread_cancel(*xbee_listen_thread); |
|
349 |
pthread_join(*xbee_listen_thread, NULL); |
|
350 |
free(xbee_listen_thread); |
|
351 |
lockf(xbee_stream, F_ULOCK, 0); |
|
352 |
close(xbee_stream); |
|
353 |
#else |
|
354 |
xbee_exit_api_mode(); |
|
355 |
#endif |
|
356 |
} |
|
357 |
|
|
358 |
/** |
|
359 |
* Send a buffer buf of size bytes to the XBee. |
|
360 |
* |
|
361 |
* @param buf the buffer of data to send |
|
362 |
* @param size the number of bytes to send |
|
363 |
**/ |
|
364 |
static int xbee_send(char* buf, int size) |
|
365 |
{ |
|
366 |
#ifdef ROBOT |
|
367 |
int i; |
|
368 |
for (i = 0; i < size; i++) { |
|
369 |
xbee_putc(buf[i]); |
|
370 |
} |
|
371 |
|
|
372 |
return 0; |
|
373 |
|
|
374 |
#else |
|
375 |
|
|
376 |
int ret = write(xbee_stream, buf, size); |
|
377 |
//success |
|
378 |
if (ret == size) |
|
379 |
return 0; |
|
380 |
if (ret == -1) |
|
381 |
{ |
|
382 |
//interrupted by system signal, probably timer interrupt. |
|
383 |
//just try again |
|
384 |
if (errno == 4) |
|
385 |
{ |
|
386 |
return xbee_send(buf, size); |
|
387 |
} |
|
388 |
WL_DEBUG_PRINT("Failed to write to xbee\r\n"); |
|
389 |
return -1; |
|
390 |
} |
|
391 |
|
|
392 |
//write was interrupted after writing ret bytes |
|
393 |
return xbee_send(buf + ret, size - ret); |
|
394 |
#endif |
|
395 |
} |
|
396 |
|
|
397 |
/** |
|
398 |
* Sends a string to the XBee. |
|
399 |
* |
|
400 |
* @param c the string to send to the XBEE |
|
401 |
**/ |
|
402 |
static int xbee_send_string(char* c) |
|
403 |
{ |
|
404 |
return xbee_send(c, strlen(c)); |
|
405 |
} |
|
406 |
|
|
407 |
#ifndef ROBOT |
|
408 |
static int xbee_read(char* buf, int size) |
|
409 |
{ |
|
410 |
if (read(xbee_stream, buf, size) == -1) { |
|
411 |
WL_DEBUG_PRINT("Failed to read from xbee.\r\n"); |
|
412 |
return -1; |
|
413 |
} |
|
414 |
|
|
415 |
return 0; |
|
416 |
} |
|
417 |
#endif |
|
418 |
|
|
419 |
/** |
|
420 |
* Enter into command mode. |
|
421 |
**/ |
|
422 |
static int xbee_enter_command_mode() |
|
423 |
{ |
|
424 |
if (xbee_send_string("+++") != 0) { |
|
425 |
return -1; |
|
426 |
} |
|
427 |
|
|
428 |
if (xbee_wait_for_ok() != 0) { |
|
429 |
return -1; |
|
430 |
} |
|
431 |
return 0; |
|
432 |
} |
|
433 |
|
|
434 |
/** |
|
435 |
* Exit from command mode. |
|
436 |
**/ |
|
437 |
static int xbee_exit_command_mode() |
|
438 |
{ |
|
439 |
if (xbee_send_string("ATCN\r") != 0) { |
|
440 |
return -1; |
|
441 |
} |
|
442 |
|
|
443 |
xbee_wait_for_ok(); |
|
444 |
|
|
445 |
return 0; |
|
446 |
} |
|
447 |
|
|
448 |
/** |
|
449 |
* Enter API mode. |
|
450 |
**/ |
|
451 |
static int xbee_enter_api_mode() |
|
452 |
{ |
|
453 |
if (xbee_send_string("ATAP 1\r") != 0) { |
|
454 |
return -1; |
|
455 |
} |
|
456 |
xbee_wait_for_ok(); |
|
457 |
|
|
458 |
return 0; |
|
459 |
} |
|
460 |
|
|
461 |
/** |
|
462 |
* Exit API mode. |
|
463 |
**/ |
|
464 |
static int xbee_exit_api_mode() |
|
465 |
{ |
|
466 |
if (xbee_send_modify_at_command("AP","0") != 0) { |
|
467 |
return -1; |
|
468 |
} |
|
469 |
|
|
470 |
return 0; |
|
471 |
} |
|
472 |
|
|
473 |
/** |
|
474 |
* Wait until the string "OK\r" is received from the XBee. |
|
475 |
**/ |
|
476 |
static int xbee_wait_for_ok() |
|
477 |
{ |
|
478 |
return xbee_wait_for_string("OK\r", 3); |
|
479 |
} |
|
480 |
|
|
481 |
/** |
|
482 |
* Delay until the specified string is received from |
|
483 |
* the XBee. Discards all other XBee data. |
|
484 |
* |
|
485 |
* ********* Robot often hangs here **************** |
|
486 |
* |
|
487 |
* @param s the string to receive |
|
488 |
* @param len the length of the string |
|
489 |
**/ |
|
490 |
static int xbee_wait_for_string(char* s, int len) |
|
491 |
{ |
|
492 |
char* curr = s; |
|
493 |
while (curr - s < len) { |
|
494 |
WL_DEBUG_PRINT("waiting for string\r\n"); |
|
495 |
// check if buffer is empty |
|
496 |
if (buffer_last != buffer_first) { |
|
497 |
char c = arrival_buf[buffer_first++]; |
|
498 |
if (buffer_first == XBEE_BUFFER_SIZE) { |
|
499 |
buffer_first = 0; |
|
500 |
} |
|
501 |
|
|
502 |
if (c == *curr) { |
|
503 |
curr++; |
|
504 |
} else { |
|
505 |
#ifndef ROBOT |
|
506 |
//return -1; // Computer is less forgiving. |
|
507 |
curr = s; |
|
508 |
#else |
|
509 |
curr = s; |
|
510 |
#endif |
|
511 |
} |
|
512 |
} // else buffer is empty. |
|
513 |
|
|
514 |
#ifndef ROBOT |
|
515 |
usleep(100); |
|
516 |
#endif |
|
517 |
} |
|
518 |
|
|
519 |
return 0; |
|
520 |
} |
|
521 |
|
|
522 |
/** |
|
523 |
* Verifies that the packets checksum is correct. |
|
524 |
* (If the checksum is correct, the sum of the bytes |
|
525 |
* is 0xFF.) |
|
526 |
* |
|
527 |
* @param packet the packet received. This includes the first |
|
528 |
* three bytes, which are header information from the XBee. |
|
529 |
* |
|
530 |
* @param len The length of the packet received from the XBee |
|
531 |
* |
|
532 |
* @return 0 if the checksum is incorrect, nonzero |
|
533 |
* otherwise |
|
534 |
**/ |
|
535 |
int xbee_verify_checksum(char* packet, int len) |
|
536 |
{ |
|
537 |
unsigned char sum = 0; |
|
538 |
int i; |
|
539 |
for (i = 3; i < len; i++) |
|
540 |
sum += (unsigned char)packet[i]; |
|
541 |
return sum == 0xFF; |
|
542 |
} |
|
543 |
|
|
544 |
/** |
|
545 |
* Returns the checksum of the given packet. |
|
546 |
* |
|
547 |
* @param buf the data for the packet to send |
|
548 |
* @param len the length of the packet in bytes |
|
549 |
* |
|
550 |
* @return the checksum of the packet, which will |
|
551 |
* become the last byte sent in the packet |
|
552 |
**/ |
|
553 |
char xbee_compute_checksum(char* buf, int len) |
|
554 |
{ |
|
555 |
int i; |
|
556 |
unsigned char sum = 0; |
|
557 |
for (i = 0; i < len; i++) |
|
558 |
sum += (unsigned char)buf[i]; |
|
559 |
return 0xFF - sum; |
|
560 |
} |
|
561 |
|
|
562 |
/** |
|
563 |
* Adds header information and checksum to the given |
|
564 |
* packet and sends it. Header information includes |
|
565 |
* XBEE_FRAME_START and the packet length, as two bytes. |
|
566 |
* |
|
567 |
* @param buf the packet data |
|
568 |
* @param len the size in bytes of the packet data |
|
569 |
* |
|
570 |
**/ |
|
571 |
static int xbee_send_frame(char* buf, int len) |
|
572 |
{ |
|
573 |
char prefix[3]; |
|
574 |
prefix[0] = XBEE_FRAME_START; |
|
575 |
prefix[1] = (len & 0xFF00) >> 8; |
|
576 |
prefix[2] = len & 0xFF; |
|
577 |
char checksum = xbee_compute_checksum(buf, len); |
|
578 |
|
|
579 |
if (xbee_send(prefix, 3) != 0) { |
|
580 |
return -1; |
|
581 |
} |
|
582 |
|
|
583 |
if (xbee_send(buf, len) != 0) { |
|
584 |
return -1; |
|
585 |
} |
|
586 |
|
|
587 |
if (xbee_send(&checksum, 1) != 0) { |
|
588 |
return -1; |
|
589 |
} |
|
590 |
|
|
591 |
return 0; |
|
592 |
} |
|
593 |
|
|
594 |
/** |
|
595 |
* Sends an AT command to read a parameter. |
|
596 |
* |
|
597 |
* @param command the AT command to send. For exmaple, |
|
598 |
* use ID to read the PAN ID and MY to return the XBee ID. |
|
599 |
* See the XBee reference guide for a complete listing. |
|
600 |
**/ |
|
601 |
int xbee_send_read_at_command(char* command) |
|
602 |
{ |
|
603 |
return xbee_send_modify_at_command(command, NULL); |
|
604 |
} |
|
605 |
|
|
606 |
/** |
|
607 |
* Sends the given AT command. |
|
608 |
* |
|
609 |
* @param command the AT command to send (e.g., MY, ID) |
|
610 |
* @param value the value to pass as a parameter |
|
611 |
* (or NULL if there is no parameter) |
|
612 |
**/ |
|
613 |
static int xbee_send_modify_at_command(char* command, char* value) |
|
614 |
{ |
|
615 |
char buf[16]; |
|
616 |
int i; |
|
617 |
|
|
618 |
buf[0] = XBEE_FRAME_AT_COMMAND; |
|
619 |
buf[1] = 1; |
|
620 |
buf[2] = command[0]; |
|
621 |
buf[3] = command[1]; |
|
622 |
int valueLen = 0; |
|
623 |
if (value != NULL) |
|
624 |
{ |
|
625 |
valueLen = strlen(value); |
|
626 |
if (valueLen > 8) |
|
627 |
{ |
|
628 |
WL_DEBUG_PRINT("AT Command too large.\r\n"); |
|
629 |
return -1; |
|
630 |
} |
|
631 |
|
|
632 |
for (i = 0; i < valueLen; i++) { |
|
633 |
buf[4 + i] = value[i]; |
|
634 |
} |
|
635 |
} |
|
636 |
|
|
637 |
return xbee_send_frame(buf, 4 + valueLen); |
|
638 |
} |
|
639 |
|
|
640 |
/** |
|
641 |
* Send the specified packet. |
|
642 |
* |
|
643 |
* @param packet the packet data to send |
|
644 |
* @param len the number of bytes in the packet |
|
645 |
* |
|
646 |
* @param dest the ID of the XBee to send the packet to, |
|
647 |
* or XBEE_BROADCAST to send the message to all robots |
|
648 |
* in the PAN. |
|
649 |
* |
|
650 |
* @param options a combination of the flags |
|
651 |
* XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and |
|
652 |
* XBEE_OPTIONS_BROADCAST_ALL_PANS |
|
653 |
* |
|
654 |
* @param frame the frame number to associate this packet |
|
655 |
* with. This will be used to identify the response when |
|
656 |
* the XBee alerts us as to whether or not our message |
|
657 |
* was received. |
|
658 |
**/ |
|
659 |
int xbee_send_packet(char* packet, int len, int dest, char options, char frame) |
|
660 |
{ |
|
661 |
char buf[5]; |
|
662 |
char prefix[3]; |
|
663 |
int i; |
|
664 |
unsigned char checksum = 0; |
|
665 |
|
|
666 |
if (len > 100) |
|
667 |
{ |
|
668 |
WL_DEBUG_PRINT("Packet is too large.\r\n"); |
|
669 |
return -1; |
|
670 |
} |
|
671 |
|
|
672 |
//data for sending request |
|
673 |
buf[0] = XBEE_FRAME_TX_REQUEST_16; |
|
674 |
buf[1] = frame; |
|
675 |
buf[2] = (dest >> 8) & 0xFF; |
|
676 |
buf[3] = dest & 0xFF; |
|
677 |
buf[4] = options; |
|
678 |
|
|
679 |
//packet prefix, do this here so we don't need an extra buffer |
|
680 |
prefix[0] = XBEE_FRAME_START; |
|
681 |
prefix[1] = ((5 + len) & 0xFF00) >> 8; |
|
682 |
prefix[2] = (5 + len) & 0xFF; |
|
683 |
|
|
684 |
for (i = 0; i < 5; i++) |
|
685 |
checksum += (unsigned char)buf[i]; |
|
686 |
for (i = 0; i < len; i++) |
|
687 |
checksum += (unsigned char)packet[i]; |
|
688 |
checksum = 0xFF - checksum; |
|
689 |
|
|
690 |
if (xbee_send(prefix, 3) != 0) { |
|
691 |
return -1; |
|
692 |
} |
|
693 |
|
|
694 |
if (xbee_send(buf, 5) != 0) { |
|
695 |
return -1; |
|
696 |
} |
|
697 |
|
|
698 |
if (xbee_send(packet, len) != 0) { |
|
699 |
return -1; |
|
700 |
} |
|
701 |
|
|
702 |
if (xbee_send((char*)&checksum, 1) != 0) { |
|
703 |
return -1; |
|
704 |
} |
|
705 |
|
|
706 |
return 0; |
|
707 |
} |
|
708 |
|
|
709 |
/** |
|
710 |
* Reads a packet received from the XBee. This function |
|
711 |
* is non-blocking. The resulting packet is stored in dest. |
|
712 |
* Only returns transmission response packets and |
|
713 |
* received packets. The returned packet does not include |
|
714 |
* header information or the checksum. This method also |
|
715 |
* handles special packets dealt with by the XBee library, |
|
716 |
* and so should be called frequently while the XBee is in |
|
717 |
* use.<br><br> |
|
718 |
* |
|
719 |
* The first byte of the packet will be either |
|
720 |
* XBEE_TX_STATUS or XBEE_RX to indicated |
|
721 |
* a response to a sent message or a received message, |
|
722 |
* respectively.<br><br> |
|
723 |
* |
|
724 |
* For a status response packet:<br> |
|
725 |
* The first byte will be XBEE_TX_STATUS.<br> |
|
726 |
* The second byte will be the frame number.<br> |
|
727 |
* The third byte will be the result. 0 indicates success, |
|
728 |
* and nonzero indicates that an error ocurred in |
|
729 |
* transmitting the packet.<br><br> |
|
730 |
* |
|
731 |
* For a received packet:<br> |
|
732 |
* The first byte will be XBEE_RX.<br> |
|
733 |
* The second and third bytes will be the 16-bit |
|
734 |
* address of the packet's sender.<br> |
|
735 |
* The fourth byte is the signal strength.<br> |
|
736 |
* The fifth byte is 1 if the packet were sent to |
|
737 |
* a specific address, and 2 if it is a broadcast packet.<br><br> |
|
738 |
* |
|
739 |
* @param dest set to the packet data |
|
740 |
* @return the length of the packet, or -1 if no packet |
|
741 |
* is available |
|
742 |
**/ |
|
743 |
int xbee_get_packet(unsigned char* dest) |
|
744 |
{ |
|
745 |
int ret; |
|
746 |
//start reading a packet with XBEE_FRAME_START |
|
747 |
if (currentBufPos == 0) |
|
748 |
{ |
|
749 |
do |
|
750 |
{ |
|
751 |
if (buffer_first == XBEE_BUFFER_SIZE) |
|
752 |
buffer_first = 0; |
|
753 |
// check if buffer is empty |
|
754 |
if (buffer_first == buffer_last) { |
|
755 |
return -1; |
|
756 |
} |
Also available in: Unified diff