root / trunk / code / projects / colonet / lib / colonet_dragonfly / colonet_dragonfly.c @ 180
History | View | Annotate | Download (6.94 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 |
usb_puts("Packet received.\n");
|
66 |
char buf[40]; |
67 |
sprintf(buf, "length=%d\n", length);
|
68 |
|
69 |
int i;
|
70 |
for (i = 0; i < length; i++) { |
71 |
sprintf(buf, "%d: %d ", i, packet[i]);
|
72 |
usb_puts(buf); |
73 |
} |
74 |
usb_puts("\n");
|
75 |
|
76 |
//printf("received message from %d of length %d\n", source, length);
|
77 |
|
78 |
///assert(length == sizeof(ColonetRobotServerPacket));
|
79 |
packet_string_to_struct(&pkt, packet); |
80 |
|
81 |
char buf2[40]; |
82 |
sprintf(buf2, "client_id:%d, msg_code:%d\n", pkt.client_id, pkt.msg_code);
|
83 |
usb_puts(buf2); |
84 |
|
85 |
args = pkt.data; |
86 |
|
87 |
int_args[0] = two_bytes_to_int(args[0], args[1]); |
88 |
int_args[1] = two_bytes_to_int(args[2], args[3]); |
89 |
int_args[2] = two_bytes_to_int(args[4], args[5]); |
90 |
|
91 |
if (type == COLONET_REQUEST) {
|
92 |
usb_puts("type is colonet request\n");
|
93 |
|
94 |
/* TODO - send back data! */
|
95 |
|
96 |
switch (pkt.msg_code) {
|
97 |
//Sharp
|
98 |
case READ_DISTANCE:
|
99 |
break;
|
100 |
case LINEARIZE_DISTANCE:
|
101 |
break;
|
102 |
case LOG_DISTANCE:
|
103 |
break;
|
104 |
|
105 |
|
106 |
//Analog
|
107 |
case CALL_ANALOG8:
|
108 |
break;
|
109 |
case CALL_ANALOG10:
|
110 |
break;
|
111 |
case WHEEL:
|
112 |
break;
|
113 |
case BATTERY:
|
114 |
break;
|
115 |
|
116 |
//BOM
|
117 |
case GETMAXBOM:
|
118 |
break;
|
119 |
|
120 |
//Dio
|
121 |
case DIGITAL_INPUT:
|
122 |
break;
|
123 |
case BUTTON1_READ:
|
124 |
break;
|
125 |
case BUTTON2_READ:
|
126 |
break;
|
127 |
|
128 |
//Bumper
|
129 |
case DETECT_BUMP:
|
130 |
break;
|
131 |
} |
132 |
} else if (type == COLONET_COMMAND) { |
133 |
usb_puts("type is colonet command...\n");
|
134 |
|
135 |
/* TODO uncomment this stuff */
|
136 |
/* if (pkt.msg_code >= USER_DEFINED_MSG_ID_START && */
|
137 |
/* pkt.msg_code <= USER_DEFINED_MSG_ID_END) { */
|
138 |
/* if (user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler) { */
|
139 |
/* /\* Call the user's handler function if it the function's address */
|
140 |
/* * is non-zero (has been set) *\/ */
|
141 |
/* user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler(); */
|
142 |
/* } */
|
143 |
/* } */
|
144 |
|
145 |
switch (pkt.msg_code) {
|
146 |
/* default: */
|
147 |
/* printf("%s: Error - message code %d not implemented\n", __FUNCTION__, */
|
148 |
/* pkt.msg_code); */
|
149 |
/* break; */
|
150 |
|
151 |
//Buzzer
|
152 |
case BUZZER_INIT:
|
153 |
buzzer_init(); |
154 |
break;
|
155 |
case BUZZER_SET_VAL:
|
156 |
buzzer_set_val(args[0]);
|
157 |
break;
|
158 |
case BUZZER_SET_FREQ:
|
159 |
buzzer_set_freq(args[0]);
|
160 |
break;
|
161 |
case BUZZER_CHIRP:
|
162 |
buzzer_chirp(int_args[0], int_args[1]); |
163 |
break;
|
164 |
case BUZZER_OFF:
|
165 |
buzzer_off(); |
166 |
break;
|
167 |
|
168 |
case ORB_INIT:
|
169 |
orb_init(); |
170 |
break;
|
171 |
//Orb
|
172 |
case ORB_SET:
|
173 |
orb_set(args[0], args[1], args[2]); |
174 |
break;
|
175 |
case ORB_SET_COLOR:
|
176 |
orb_set_color(int_args[0]);
|
177 |
break;
|
178 |
case ORB_DISABLE:
|
179 |
orb_disable(); |
180 |
break;
|
181 |
case ORB_ENABLE:
|
182 |
orb_enable(); |
183 |
break;
|
184 |
case LED_INIT:
|
185 |
break;
|
186 |
case LED_USER:
|
187 |
break;
|
188 |
case ORB_SEND:
|
189 |
//orb_send();
|
190 |
break;
|
191 |
//Motors
|
192 |
case MOTORS_INIT:
|
193 |
usb_puts("calling motors_init\n");
|
194 |
motors_init(); |
195 |
break;
|
196 |
case MOTOR1_SET:
|
197 |
sprintf(buf, "calling motor1_set [%i] [%i]\n", args[0], args[1]); |
198 |
usb_puts(buf); |
199 |
motor1_set(args[0], args[1]); |
200 |
break;
|
201 |
case MOTOR2_SET:
|
202 |
sprintf(buf, "calling motor2_set [%i] [%i]\n", args[0], args[1]); |
203 |
usb_puts(buf); |
204 |
motor2_set(args[0], args[1]); |
205 |
break;
|
206 |
case MOTORS_OFF:
|
207 |
usb_puts("calling motors_off\n");
|
208 |
motors_off(); |
209 |
break;
|
210 |
case MOVE:
|
211 |
buf[40];
|
212 |
sprintf(buf, "calling move with: %d, %d\n", args[0], args[1]); |
213 |
usb_puts(buf); |
214 |
move(args[0], args[1]); |
215 |
break;
|
216 |
case XBEE_INIT:
|
217 |
xbee_init(); |
218 |
break;
|
219 |
|
220 |
case XBEE_PUTC:
|
221 |
xbee_putc((char)args[0]); |
222 |
break;
|
223 |
|
224 |
case USB_INIT:
|
225 |
usb_init(); |
226 |
break;
|
227 |
|
228 |
case USB_PUTC:
|
229 |
usb_putc((char)args[0]); |
230 |
break;
|
231 |
|
232 |
case PRINTF:
|
233 |
printf("%s", pkt.data);
|
234 |
break;
|
235 |
case KILL_ROBOT:
|
236 |
motors_off(); |
237 |
buzzer_off(); |
238 |
exit(0); //kill the robot |
239 |
break;
|
240 |
//Time
|
241 |
case DELAY_MS:
|
242 |
delay_ms(int_args[0]);
|
243 |
break;
|
244 |
|
245 |
//Analog
|
246 |
case ANALOG_INIT:
|
247 |
analog_init(); |
248 |
break;
|
249 |
|
250 |
//BOM
|
251 |
case BOM_ON:
|
252 |
bom_on(); |
253 |
break;
|
254 |
case BOM_OFF:
|
255 |
bom_off(); |
256 |
break;
|
257 |
|
258 |
//Dio
|
259 |
case DIGITAL_OUTPUT:
|
260 |
digital_output(int_args[0], int_args[1]); |
261 |
break;
|
262 |
} |
263 |
} else {
|
264 |
char buf[80]; |
265 |
sprintf(buf, "%s: Error: Invalid colonet message type", __FUNCTION__);
|
266 |
usb_puts(buf); |
267 |
} |
268 |
} |
269 |
|
270 |
/* colonet_add_message
|
271 |
* Adds a user-defined message
|
272 |
*/
|
273 |
int colonet_add_message(unsigned char msgId, void (*handler)(void)) { |
274 |
if(msgId < USER_DEFINED_MSG_ID_START || msgId > USER_DEFINED_MSG_ID_END){
|
275 |
return -1; |
276 |
} |
277 |
|
278 |
/* Register this function in the array */
|
279 |
user_handlers[msgId-USER_DEFINED_MSG_ID_START].msgId = msgId; |
280 |
user_handlers[msgId-USER_DEFINED_MSG_ID_START].handler = handler; |
281 |
|
282 |
return 0; |
283 |
} |
284 |
|
285 |
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt, |
286 |
char* pkt_buf) {
|
287 |
memcpy(dest_pkt, pkt_buf, sizeof(ColonetRobotServerPacket));
|
288 |
} |
289 |
|
290 |
/* two_bytes_to_int(char a, char b)
|
291 |
* Returns int of form [high][low]
|
292 |
*/
|
293 |
static unsigned int two_bytes_to_int(char high, char low) { |
294 |
return (((unsigned int)high)<<8) + (unsigned int)low; |
295 |
} |