Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / robot / colonet_dragonfly / colonet_dragonfly.c @ 620

History | View | Annotate | Download (12.4 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 <battery.h>
13
#include <colonet_defs.h>
14
#include <colonet_dragonfly.h>
15
#include <dragonfly_lib.h>
16
#include <math.h>
17
#include <string.h>
18
#include <wireless.h>
19

    
20
#define TARGET_POSITION_STOP_DISTANCE_THRESHOLD (40.0)
21

    
22
typedef struct {
23
  unsigned char msgId; //is this necessary?
24
  void (*handler)(void);
25
} UserHandler;
26

    
27
/* Globals (internal) */
28
static UserHandler user_handlers[USER_DEFINED_MSG_TOTAL];
29

    
30
static unsigned robot_x, robot_y;
31
static volatile int updated_robot_pos_ready;
32
#define ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST (1)
33

    
34
/* Internal function prototypes */
35
static unsigned two_bytes_to_int(unsigned char high, unsigned char low);
36
static void colonet_handle_receive(char type, int source, unsigned char* packet, int length);
37
static void move_to_position_routine(unsigned x, unsigned y);
38

    
39
static PacketGroupHandler colonet_pgh;
40

    
41
/* two_bytes_to_int(char a, char b)
42
 * Returns int of form [high][low]
43
 */
44
static unsigned int two_bytes_to_int(unsigned char high, unsigned char low) {
45
  return (high<<8) | low;
46
}
47

    
48
/* Public functions */
49
int colonet_init() {
50
  colonet_pgh.groupCode = COLONET_PACKET_GROUP_ID;
51
  colonet_pgh.timeout_handler = NULL;
52
  colonet_pgh.handle_response = NULL;
53
  colonet_pgh.handle_receive = colonet_handle_receive;
54
  colonet_pgh.unregister = NULL;
55

    
56
  // TODO this should return an error if wl_init has not been called yet.
57
  wl_register_packet_group(&colonet_pgh);
58

    
59
  robot_x = 0;
60
  robot_y = 0;
61
  updated_robot_pos_ready = 0;
62

    
63
  return 0;
64
}
65

    
66
/* colonet_add_message
67
 * Adds a user-defined message
68
 */
69
int colonet_add_message(unsigned char msgId, void (*handler)(void)) {
70
  if(msgId < USER_DEFINED_MSG_ID_START  /* || msgId > USER_DEFINED_MSG_ID_END */){
71
    return -1;
72
  }
73

    
74
  /* Register this function in the array */
75
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].msgId = msgId;
76
  user_handlers[msgId-USER_DEFINED_MSG_ID_START].handler = handler;
77

    
78
  return 0;
79
}
80

    
81
void get_absolute_position(int* x, int* y) {
82
  *x = robot_x;
83
  *y = robot_y;
84
}
85

    
86
void request_abs_position() {
87
  //usb_puts("requesting_abs_position\n");
88

    
89
  ColonetRobotServerPacket pkt;
90
  pkt.client_id = -1;
91
  pkt.msg_code = ROBOT_REQUEST_POSITION_FROM_SERVER;
92
  wl_send_global_packet(colonet_pgh.groupCode, 0, (char*)&pkt, sizeof(pkt), 0);
93
}
94

    
95
static float sqrt_approx(float x) {
96
  float x2 = x*x;
97
  float x3 = x*x2;
98

    
99
  return 0.00014*x3 - 0.0078*x2 + 0.29*x + 0.84;
100
}
101

    
102
static float dist(float x1, float y1, float x2, float y2) {
103
  return sqrt_approx((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
104
}
105

    
106
/* cubic approximation of arctan. */
107
/*static void arctan(float t) {
108
  float t3 = t*t*t;
109
  return -0.039 * t3 + 0.74 * t + 0.00011;
110
  }*/
111

    
112
/** Differential is the intended left motor speed - right motor speed. */
113
static void set_motors_with_differential(int differential) {
114
  int ml, mr;
115

    
116
  if (differential >= 0) {
117
    /* Going left or straight. */
118
    ml = 250;
119
    mr = ml - differential;
120
  } else {
121
    /* Turning right. */
122
    mr = 250;
123
    ml = mr + differential;
124
  }
125

    
126
  int motor1_dir = mr < 0 ? 0 : 1;
127
  int motor2_dir = ml < 0 ? 0 : 1;
128

    
129
  motor1_set(motor1_dir, mr);
130
  motor2_set(motor2_dir, ml);
131
}
132

    
133
static void move_to_position_routine(unsigned target_x, unsigned target_y) {
134
  usb_puts("move to absolute position routine!\n");
135
  
136
  updated_robot_pos_ready = 0;
137
  request_abs_position(); // While we're doing this computation, server can be reporting next position.
138

    
139
  int count = 0;
140
  while (!updated_robot_pos_ready) {
141
    wl_do();
142
    if (count++ == 5000) { // in case the server missed it...
143
      request_abs_position();
144
      count = 0;
145
    }
146
  }
147

    
148
//  usb_puts("got past first loop.\n");
149

    
150
  unsigned last_x = robot_x, last_y = robot_y;
151

    
152
  char buf[40];
153
  //sprintf(buf, "cur dist is %f\n", dist((float)robot_x, (float)robot_y, (float)target_x, (float)target_y));
154
  //usb_puts(buf);
155
  //sprintf(buf, "radius squared is %f\n", TARGET_POSITION_STOP_DISTANCE_THRESHOLD);
156
  //usb_puts(buf);
157

    
158
//  usb_puts("entering while loop.\n");
159

    
160
  while (dist(robot_x, robot_y, target_x, target_y) > TARGET_POSITION_STOP_DISTANCE_THRESHOLD) {
161
    
162
//    usb_puts("inside while loop.\n");
163
    
164
    updated_robot_pos_ready = 0;
165
    request_abs_position(); // While we're doing this computation, server can be reporting next position.
166

    
167
//    usb_puts("after request_abs_position.\n");
168

    
169
    int cur_robot_x = robot_x;
170
    int cur_robot_y = robot_y;
171

    
172
    //usb_puts("after cur_robot_x/y = robot_x/y.\n");
173

    
174

    
175
    int e_x = target_x - cur_robot_x;
176
    int e_y = target_y - cur_robot_y;
177

    
178
    int v_x = cur_robot_x - last_x;
179
    int v_y = cur_robot_y - last_y;
180

    
181
    
182

    
183
    sprintf(buf, "vx:%d vy:%d rx:%d ry:%d ex:%d ey:%d\n", v_x, v_y, e_x, e_y, e_x, e_y);
184
    usb_puts(buf);
185
    
186
    int e_mag = e_x*e_x + e_y*e_y;
187

    
188
    int motor_differential = e_mag >> 7;// / 128;
189

    
190
/*
191
    sprintf(buf, "Current position: %d %d\n", cur_robot_x, cur_robot_y);
192
    usb_puts(buf);
193

194
    sprintf(buf, "Target position: %d %d\n", target_x, target_y);
195
    usb_puts(buf);
196

197
    float r_x = (float)target_x - cur_robot_x;
198
    float r_y = (float)target_y - cur_robot_y;
199
    float r_mag = sqrt_approx(r_x * r_x + r_y * r_y);
200
    r_x /= r_mag;
201
    r_y /= r_mag;
202

203
    float v_x = (float)cur_robot_x - last_x;
204
    float v_y = robot_y - last_y;
205
    float v_mag = sqrt_approx(v_x*v_x + v_y*v_y);
206
    v_x /= v_mag;
207
    v_y /= v_mag;
208

209
    float e_x = r_x - v_x;
210
    float e_y = r_y - v_y;
211
    float e_mag = sqrt_approx(e_x * e_x + e_y * e_y);
212

213
    sprintf(buf, "Error: <%f,%f>; mag: %f\n", e_x, e_y, e_mag);
214
    usb_puts(buf);
215

216
    // Motor differential proportional to magnitude of directional error.
217
  //  int motor_differential = (int)(e_mag * ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST);
218
*/
219
  
220

    
221
    int p_x = v_y;
222
    int p_y = -v_x;
223
    
224
    int e_trans_x = p_x * e_x + p_y * e_y;
225

    
226
/*
227
    // Determine left or right by transforming error vector to robot axes
228
    // Perpendicular
229
    float p_x = v_y;
230
    float p_y = -v_x;
231

232
    // Inverse matrix
233
    float coeff = 1.0 / (p_x * v_y - v_x * p_y);
234
    float mat[2][2];
235
    mat[0][0] = v_y * coeff;
236
    mat[0][1] = -v_x * coeff;
237
    //mat[1][0] = -p_y * coeff;     //Not needed for our purposes.
238
    //mat[1][1] = p_x * coeff;
239

240
    float e_trans_x = mat[0][0] * e_x + mat[0][1] * e_y;
241
    //float e_trans_y = mat[1][0] * e_x + mat[1][1] * e_y;  //Not needed for our purposes.
242
*/
243

    
244

    
245
    if (e_trans_x < 0) {
246
      motor_differential = -motor_differential;
247
    }
248

    
249
    sprintf(buf, "motor_diff: %d\n", motor_differential);
250
    usb_puts(buf);
251

    
252
    last_x = cur_robot_x;
253
    last_y = cur_robot_y;
254

    
255
    set_motors_with_differential(motor_differential);
256

    
257
    count = 0;
258
    while (!updated_robot_pos_ready) { // ghetto condition variable
259
      wl_do();
260
      if (count++ == 5000) { // in case the server missed it...
261
        request_abs_position();
262
        count = 0;
263
      }
264
    }
265
  }
266

    
267
  usb_puts("reached destination!\n");
268
}
269

    
270
/* Private functions */
271

    
272
/** @brief Handles colonet packets.  Should be called by parse_buffer
273
 * when it is determined that a colonet message has been received.
274
 *
275
 * @param robot_id The robot id
276
 * @param pkt_buf The packet buffer (e.g. wl_buf)
277
 *
278
 * @return -1 on error (invalid msgId), 0 on success
279
 */
280
static void colonet_handle_receive(char type, int wl_source, unsigned char* packet, int length) {
281
  ColonetRobotServerPacket* pkt= (ColonetRobotServerPacket*)packet;
282
  unsigned char* args; //up to 7 char args
283
  unsigned int_args[3]; //up to 3 int (2-byte) args
284

    
285
  char buf[40];
286

    
287
  length = length;
288
  /*
289
  usb_puts("Packet received.\n");
290

291
  sprintf(buf, "length=%d\n", length);
292

293
  int i;
294
  for (i = 0; i < length; i++) {
295
    sprintf(buf, "%d: %d ", i, packet[i]);
296
    usb_puts(buf); 
297
 }
298
  usb_puts("\n");
299
  */
300

    
301
  //printf("received message from %d of length %d\n", source, length);
302

    
303
  ///assert(length == sizeof(ColonetRobotServerPacket));
304

    
305
  /*
306
  sprintf(buf, "client_id:%d, msg_code:%d\n", pkt.client_id, pkt.msg_code);
307
  usb_puts(buf);*/
308

    
309
  args = pkt->data;
310

    
311
  int_args[0] = two_bytes_to_int(args[0], args[1]);
312
  int_args[1] = two_bytes_to_int(args[2], args[3]);
313
  int_args[2] = two_bytes_to_int(args[4], args[5]);
314

    
315
  if (type == COLONET_REQUEST) {
316
    sprintf(buf, "received colonet request: pkt msgcode is %d\n", pkt->msg_code);
317
    usb_puts(buf);
318

    
319
    switch (pkt->msg_code) {
320
      //Sharp
321
    case READ_DISTANCE:
322
      break;
323
    case LINEARIZE_DISTANCE:
324
      break;
325
    case LOG_DISTANCE:
326
      break;
327

    
328
      //Analog
329
    case CALL_ANALOG8:
330
      break;
331
    case CALL_ANALOG10:
332
      break;
333
    case WHEEL:
334
      break;
335

    
336
    case BATTERY:
337
      sprintf((char*)pkt->data, "%d", (int)battery8());
338

    
339
      // TODO: THIS IS NOT WORKING!  SERVER DOES NOT GET PACKET!
340
      wl_send_robot_to_robot_global_packet(colonet_pgh.groupCode, 0, (char*)&pkt, sizeof(pkt), wl_source, 0);
341

    
342
      sprintf(buf, "it's a battery request\nsent battery value: %s\n", pkt->data);
343
      usb_puts(buf);
344
      break;
345

    
346
      //BOM
347
    case GETMAXBOM:
348
      break;
349

    
350
      //Dio
351
    case DIGITAL_INPUT:
352
      break;
353
    case BUTTON1_READ:
354
      break;
355
    case BUTTON2_READ:
356
      break;
357

    
358
      //Bumper
359
    case DETECT_BUMP:
360
      break;
361
    }
362
  } else if (type == COLONET_COMMAND) {
363
    sprintf(buf, "colonet command ... pkt->msg_code=%d\n", pkt->msg_code);
364
    usb_puts(buf);
365

    
366
/* TODO uncomment this stuff */
367
/*     if (pkt.msg_code >= USER_DEFINED_MSG_ID_START && */
368
/*         pkt.msg_code <= USER_DEFINED_MSG_ID_END) { */
369
/*       if (user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler) { */
370
/*         /\* Call the user's handler function if it the function's address */
371
/*          * is non-zero (has been set) *\/ */
372
/*         user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler(); */
373
/*       } */
374
/*     } */
375

    
376
    switch (pkt->msg_code) {
377
/*     default: */
378
/*       printf("%s: Error - message code %d not implemented\n", __FUNCTION__, */
379
/*              pkt.msg_code); */
380
/*       break; */
381

    
382
    case SERVER_REPORT_POSITION_TO_ROBOT:
383
      robot_x = (unsigned)int_args[0];
384
      robot_y = (unsigned)int_args[1];
385

    
386
      updated_robot_pos_ready = 1;
387

    
388
      sprintf(buf, "pos is: %u %u\n", robot_x, robot_y);
389
      usb_puts(buf);
390
      break;
391

    
392
    case MOVE_TO_ABSOLUTE_POSITION:
393
      usb_puts("move to abs position2!\n");
394
      move_to_position_routine((unsigned)int_args[0], (unsigned)int_args[1]);
395
      break;
396

    
397
      //Buzzer
398
    case BUZZER_INIT:
399
      buzzer_init();
400
      break;
401
    case BUZZER_SET_VAL:
402
      buzzer_set_val(args[0]);
403
      break;
404
    case BUZZER_SET_FREQ:
405
      buzzer_set_freq(args[0]);
406
      break;
407
    case BUZZER_CHIRP:
408
      buzzer_chirp(int_args[0], int_args[1]);
409
      break;
410
    case BUZZER_OFF:
411
      buzzer_off();
412
      break;
413
    case ORB_INIT:
414
      orb_init();
415
      break;
416
    //Orb
417
    case ORB_SET:
418
      orb_set(args[0], args[1], args[2]);
419
      break;
420
    case ORB_SET_COLOR:
421
      orb_set_color(int_args[0]);
422
      break;
423
    case ORB_DISABLE:
424
      orb_disable();
425
      break;
426
    case ORB_ENABLE:
427
      orb_enable();
428
      break;
429
    case LED_INIT:
430
      break;
431
    case LED_USER:
432
      break;
433
      //Motors
434
    case MOTORS_INIT:
435
      usb_puts("calling motors_init\n");
436
      motors_init();
437
      break;
438
    case MOTOR1_SET:
439
      sprintf(buf, "calling motor1_set [%i] [%i]\n", args[0], args[1]);
440
      usb_puts(buf);
441
      motor1_set(args[0], args[1]);
442
      break;
443
    case MOTOR2_SET:
444
      sprintf(buf, "calling motor2_set [%i] [%i]\n", args[0], args[1]);
445
      usb_puts(buf);
446
      motor2_set(args[0], args[1]);
447
      break;
448
    case MOTORS_OFF:
449
      usb_puts("calling motors_off\n");
450
      motors_off();
451
      break;
452
    case MOVE:
453
      sprintf(buf, "calling move with: %d, %d\n", args[0], args[1]);
454
      usb_puts(buf);
455
      move(args[0], args[1]);
456
      break;
457

    
458
    case USB_INIT:
459
      usb_init();
460
      break;
461

    
462
    case USB_PUTC:
463
      usb_putc((char)args[0]);
464
      break;
465

    
466
    case PRINTF:
467
      printf("%s", pkt->data);
468
      break;
469
    case KILL_ROBOT:
470
      motors_off();
471
      buzzer_off();
472
      exit(0); //kill the robot
473
      break;
474
    //Time
475
    case DELAY_MS:
476
      delay_ms(int_args[0]);
477
      break;
478

    
479
      //Analog
480
    case ANALOG_INIT:
481
      //TODO: how do we really init the analog? this doesn't work:
482
      //analog_init();
483
      break;
484

    
485
      //BOM
486
    case BOM_ON:
487
      bom_on();
488
      break;
489
    case BOM_OFF:
490
      bom_off();
491
      break;
492

    
493
    //Dio
494
    case DIGITAL_OUTPUT:
495
      digital_output(int_args[0], int_args[1]);
496
      break;
497
    }
498
  } else {
499
    sprintf(buf, "%s: Error: Invalid colonet message type", __FUNCTION__);
500
    usb_puts(buf);
501
  }
502
}