Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / lib / colonet_dragonfly / colonet_dragonfly.c @ 329

History | View | Annotate | Download (7.16 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
static 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
    // Battery
169
    case BATTERY:
170
      usb_puts("Got battery request.");
171
      wl_send_robot_to_robot_global_packet(colonet_pgh.groupCode, BATTERY,
172
        "34", 2, 0xA, 0);
173
      usb_puts("Sent.\n");
174
      break;
175

    
176
    case ORB_INIT:
177
      orb_init();
178
      break;
179
      //Orb
180
    case ORB_SET:
181
      orb_set(args[0], args[1], args[2]);
182
      break;
183
    case ORB_SET_COLOR:
184
      orb_set_color(int_args[0]);
185
      break;
186
    case ORB_DISABLE:
187
      orb_disable();
188
      break;
189
    case ORB_ENABLE:
190
      orb_enable();
191
      break;
192
    case LED_INIT:
193
      break;
194
    case LED_USER:
195
      break;
196
    case ORB_SEND:
197
      //orb_send();
198
      break;
199
      //Motors
200
    case MOTORS_INIT:
201
      usb_puts("calling motors_init\n");
202
      motors_init();
203
      break;
204
    case MOTOR1_SET:
205
             sprintf(buf, "calling motor1_set [%i] [%i]\n", args[0], args[1]);
206
      usb_puts(buf);
207
      motor1_set(args[0], args[1]);
208
      break;
209
    case MOTOR2_SET:
210
      sprintf(buf, "calling motor2_set [%i] [%i]\n", args[0], args[1]);
211
      usb_puts(buf);
212
      motor2_set(args[0], args[1]);
213
      break;
214
    case MOTORS_OFF:
215
      usb_puts("calling motors_off\n");
216
      motors_off();
217
      break;
218
    case MOVE:
219
      buf[40];
220
      sprintf(buf, "calling move with: %d, %d\n", args[0], args[1]);
221
      usb_puts(buf);
222
      move(args[0], args[1]);
223
      break;
224
    case XBEE_INIT:
225
      xbee_init();
226
      break;
227

    
228
    case XBEE_PUTC:
229
      xbee_putc((char)args[0]);
230
      break;
231

    
232
    case USB_INIT:
233
      usb_init();
234
      break;
235

    
236
    case USB_PUTC:
237
      usb_putc((char)args[0]);
238
      break;
239

    
240
    case PRINTF:
241
      printf("%s", pkt.data);
242
      break;
243
    case KILL_ROBOT:
244
      motors_off();
245
      buzzer_off();
246
      exit(0); //kill the robot
247
      break;
248
      //Time
249
    case DELAY_MS:
250
      delay_ms(int_args[0]);
251
      break;
252

    
253
      //Analog
254
    case ANALOG_INIT:
255
      analog_init();
256
      break;
257

    
258
      //BOM
259
    case BOM_ON:
260
      bom_on();
261
      break;
262
    case BOM_OFF:
263
      bom_off();
264
      break;
265

    
266
      //Dio
267
    case DIGITAL_OUTPUT:
268
      digital_output(int_args[0], int_args[1]);
269
      break;
270
    }
271
  } else {
272
    char buf[80];
273
    sprintf(buf, "%s: Error: Invalid colonet message type", __FUNCTION__);
274
    usb_puts(buf);
275
  }
276
}
277

    
278
/* colonet_add_message
279
 * Adds a user-defined message
280
 */
281
int colonet_add_message(unsigned char msgId, void (*handler)(void)) {
282
  if(msgId < USER_DEFINED_MSG_ID_START || msgId > USER_DEFINED_MSG_ID_END){
283
    return -1;
284
  }
285

    
286
  /* Register this function in the array */
287
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].msgId = msgId;
288
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].handler = handler;
289

    
290
  return 0;
291
}
292

    
293
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt,
294
                                    char* pkt_buf) {
295
  memcpy(dest_pkt, pkt_buf, sizeof(ColonetRobotServerPacket));
296
}
297

    
298
/* two_bytes_to_int(char a, char b)
299
 * Returns int of form [high][low]
300
 */
301
static unsigned int two_bytes_to_int(char high, char low) {
302
  return (((unsigned int)high)<<8) + (unsigned int)low;
303
}