root / trunk / code / projects / colonet / lib / colonet_dragonfly / colonet_dragonfly.c @ 174
History | View | Annotate | Download (6.07 KB)
1 |
/** @file colonet.c
|
---|---|
2 |
* @brief Colonet library for DRAGONFLY colony robots
|
3 |
*
|
4 |
* @author Eugene Marinelli
|
5 |
* @date 10/10/07
|
6 |
*
|
7 |
* @bug Handler registration not tested
|
8 |
* @bug Request reponding not implemented - only accepts commands.
|
9 |
*/
|
10 |
|
11 |
#include <assert.h> |
12 |
#include <dragonfly_lib.h> |
13 |
#include <string.h> |
14 |
#include <wireless.h> |
15 |
|
16 |
#include "colonet_dragonfly.h" |
17 |
|
18 |
typedef struct { |
19 |
unsigned char msgId; //is this necessary? |
20 |
void (*handler)(void); |
21 |
} UserHandler; |
22 |
|
23 |
/* Globals (internal) */
|
24 |
static UserHandler user_handlers[USER_DEFINED_MSG_TOTAL];
|
25 |
|
26 |
/* Internal function prototypes */
|
27 |
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt, |
28 |
char* pkt_buf);
|
29 |
static unsigned int two_bytes_to_int(char high, char low); |
30 |
static void colonet_handle_receive(char type, int source, |
31 |
unsigned char* packet, int length); |
32 |
|
33 |
static PacketGroupHandler colonet_pgh;
|
34 |
|
35 |
/* Public functions */
|
36 |
int colonet_init() {
|
37 |
colonet_pgh.groupCode = COLONET_PACKET_GROUP_ID; |
38 |
colonet_pgh.timeout_handler = NULL;
|
39 |
colonet_pgh.handle_response = NULL;
|
40 |
colonet_pgh.handle_receive = colonet_handle_receive; |
41 |
colonet_pgh.unregister = NULL;
|
42 |
|
43 |
// TODO this should return an error if wl_init has not been called yet.
|
44 |
wl_register_packet_group(&colonet_pgh); |
45 |
|
46 |
return 0; |
47 |
} |
48 |
|
49 |
/* Private functions */
|
50 |
|
51 |
/** @brief Handles colonet packets. Should be called by parse_buffer
|
52 |
* when it is determined that a colonet message has been received.
|
53 |
*
|
54 |
* @param robot_id The robot id
|
55 |
* @param pkt_buf The packet buffer (e.g. wl_buf)
|
56 |
*
|
57 |
* @return -1 on error (invalid msgId), 0 on success
|
58 |
*/
|
59 |
void colonet_handle_receive(char type, int source, unsigned char* packet, |
60 |
int length) {
|
61 |
ColonetRobotServerPacket pkt; |
62 |
unsigned char* args; //up to 7 char args |
63 |
unsigned int int_args[3]; //up to 3 int (2-byte) args |
64 |
|
65 |
orb_set_color(BLUE); |
66 |
usb_puts("Packet received.\n");
|
67 |
//printf("received message from %d of length %d\n", source, length);
|
68 |
|
69 |
///assert(length == sizeof(ColonetRobotServerPacket));
|
70 |
packet_string_to_struct(&pkt, packet); |
71 |
|
72 |
args = pkt.data; |
73 |
|
74 |
int_args[0] = two_bytes_to_int(args[0], args[1]); |
75 |
int_args[1] = two_bytes_to_int(args[2], args[3]); |
76 |
int_args[2] = two_bytes_to_int(args[4], args[5]); |
77 |
|
78 |
if (type == COLONET_REQUEST) {
|
79 |
/* TODO - send back data! */
|
80 |
|
81 |
switch (pkt.msg_code) {
|
82 |
//Sharp
|
83 |
case READ_DISTANCE:
|
84 |
break;
|
85 |
case LINEARIZE_DISTANCE:
|
86 |
break;
|
87 |
case LOG_DISTANCE:
|
88 |
break;
|
89 |
|
90 |
|
91 |
//Analog
|
92 |
case CALL_ANALOG8:
|
93 |
break;
|
94 |
case CALL_ANALOG10:
|
95 |
break;
|
96 |
case WHEEL:
|
97 |
break;
|
98 |
case BATTERY:
|
99 |
break;
|
100 |
|
101 |
//BOM
|
102 |
case GETMAXBOM:
|
103 |
break;
|
104 |
|
105 |
//Dio
|
106 |
case DIGITAL_INPUT:
|
107 |
break;
|
108 |
case BUTTON1_READ:
|
109 |
break;
|
110 |
case BUTTON2_READ:
|
111 |
break;
|
112 |
|
113 |
//Bumper
|
114 |
case DETECT_BUMP:
|
115 |
break;
|
116 |
} |
117 |
} else if (type == COLONET_COMMAND) { |
118 |
if(pkt.msg_code >= USER_DEFINED_MSG_ID_START &&
|
119 |
pkt.msg_code <= USER_DEFINED_MSG_ID_END){ |
120 |
if (user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler) {
|
121 |
/* Call the user's handler function if it the function's address
|
122 |
* is non-zero (has been set) */
|
123 |
user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler(); |
124 |
} |
125 |
} |
126 |
|
127 |
switch(pkt.msg_code){
|
128 |
default:
|
129 |
printf("%s: Error - message code %d not implemented\n", __FUNCTION__,
|
130 |
pkt.msg_code); |
131 |
break;
|
132 |
|
133 |
//Buzzer
|
134 |
case BUZZER_INIT:
|
135 |
buzzer_init(); |
136 |
break;
|
137 |
case BUZZER_SET_VAL:
|
138 |
buzzer_set_val(args[0]);
|
139 |
break;
|
140 |
case BUZZER_SET_FREQ:
|
141 |
buzzer_set_freq(args[0]);
|
142 |
break;
|
143 |
case BUZZER_CHIRP:
|
144 |
buzzer_chirp(int_args[0], int_args[1]); |
145 |
break;
|
146 |
case BUZZER_OFF:
|
147 |
buzzer_off(); |
148 |
break;
|
149 |
|
150 |
case ORB_INIT:
|
151 |
orb_init(); |
152 |
break;
|
153 |
//Orb
|
154 |
case ORB_SET:
|
155 |
orb_set(args[0], args[1], args[2]); |
156 |
break;
|
157 |
case ORB_SET_COLOR:
|
158 |
orb_set_color(int_args[0]);
|
159 |
break;
|
160 |
case ORB_DISABLE:
|
161 |
orb_disable(); |
162 |
break;
|
163 |
case ORB_ENABLE:
|
164 |
orb_enable(); |
165 |
break;
|
166 |
case LED_INIT:
|
167 |
break;
|
168 |
case LED_USER:
|
169 |
break;
|
170 |
case ORB_SEND:
|
171 |
//orb_send();
|
172 |
break;
|
173 |
//Motors
|
174 |
case MOTORS_INIT:
|
175 |
motors_init(); |
176 |
break;
|
177 |
case MOTOR1_SET:
|
178 |
motor1_set(int_args[0], int_args[1]); |
179 |
break;
|
180 |
case MOTOR2_SET:
|
181 |
motor2_set(int_args[0], int_args[1]); |
182 |
break;
|
183 |
case MOTORS_OFF:
|
184 |
motors_off(); |
185 |
break;
|
186 |
|
187 |
/*
|
188 |
case MOVE:
|
189 |
|
190 |
break;
|
191 |
*/
|
192 |
|
193 |
case XBEE_INIT:
|
194 |
xbee_init(); |
195 |
break;
|
196 |
|
197 |
case XBEE_PUTC:
|
198 |
xbee_putc((char)args[0]); |
199 |
break;
|
200 |
|
201 |
case USB_INIT:
|
202 |
usb_init(); |
203 |
break;
|
204 |
|
205 |
case USB_PUTC:
|
206 |
usb_putc((char)args[0]); |
207 |
break;
|
208 |
|
209 |
case PRINTF:
|
210 |
printf("%s", pkt.data);
|
211 |
break;
|
212 |
case KILL_ROBOT:
|
213 |
motors_off(); |
214 |
buzzer_off(); |
215 |
exit(0); //kill the robot |
216 |
break;
|
217 |
//Time
|
218 |
case DELAY_MS:
|
219 |
delay_ms(int_args[0]);
|
220 |
break;
|
221 |
|
222 |
//Analog
|
223 |
case ANALOG_INIT:
|
224 |
analog_init(); |
225 |
break;
|
226 |
|
227 |
//BOM
|
228 |
case BOM_ON:
|
229 |
bom_on(); |
230 |
break;
|
231 |
case BOM_OFF:
|
232 |
bom_off(); |
233 |
break;
|
234 |
|
235 |
//Dio
|
236 |
case DIGITAL_OUTPUT:
|
237 |
digital_output(int_args[0], int_args[1]); |
238 |
break;
|
239 |
} |
240 |
} else {
|
241 |
printf("%s: Error: Invalid colonet message type", __FUNCTION__);
|
242 |
} |
243 |
} |
244 |
|
245 |
/* colonet_add_message
|
246 |
* Adds a user-defined message
|
247 |
*/
|
248 |
int colonet_add_message(unsigned char msgId, void (*handler)(void)) { |
249 |
if(msgId < USER_DEFINED_MSG_ID_START || msgId > USER_DEFINED_MSG_ID_END){
|
250 |
return -1; |
251 |
} |
252 |
|
253 |
/* Register this function in the array */
|
254 |
user_handlers[msgId-USER_DEFINED_MSG_ID_START].msgId = msgId; |
255 |
user_handlers[msgId-USER_DEFINED_MSG_ID_START].handler = handler; |
256 |
|
257 |
return 0; |
258 |
} |
259 |
|
260 |
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt, |
261 |
char* pkt_buf) {
|
262 |
memcpy(dest_pkt, pkt_buf, sizeof(ColonetRobotServerPacket));
|
263 |
} |
264 |
|
265 |
/* two_bytes_to_int(char a, char b)
|
266 |
* Returns int of form [high][low]
|
267 |
*/
|
268 |
static unsigned int two_bytes_to_int(char high, char low) { |
269 |
return (((unsigned int)high)<<8) + (unsigned int)low; |
270 |
} |