Project

General

Profile

Revision 661

Added by Jason knichel about 16 years ago

virtual wall reported to robots

View differences:

colonet_dragonfly.c
24 24
  void (*handler)(void);
25 25
} UserHandler;
26 26

  
27
typedef struct {
28
  int up_x;
29
  int up_y;
30
  int low_x;
31
  int low_y;
32
} VirtualWall;
33

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

  
30
static unsigned robot_x, robot_y;
31
static unsigned last_x, last_y;
37
static volatile unsigned robot_x, robot_y;
38
static volatile unsigned last_x, last_y;
32 39
static volatile int updated_robot_pos_ready;
33 40
static volatile int new_movement_command_received;
34 41
static volatile int robot_lost; //set to 1 if robot's position is not known by the server, else 0.
42
static volatile VirtualWall virtual_wall;
35 43

  
44
static int virtual_wall_available;
45

  
36 46
#define ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST (1)
37 47

  
38 48
/* Internal function prototypes */
......
40 50
static void colonet_handle_receive(char type, int source, unsigned char* packet, int length);
41 51
static void move_to_position_routine(unsigned x, unsigned y);
42 52
static int dist_squared(int x1, int y1, int x2, int y2);
53
static void move_routine(void);
43 54

  
44 55
static PacketGroupHandler colonet_pgh;
45 56

  
......
67 78
  new_movement_command_received = 0;
68 79
  robot_lost = 1;
69 80

  
81
  virtual_wall_available = 0;
82
  virtual_wall.up_x = 0;
83
  virtual_wall.up_y = 0;
84
  virtual_wall.low_x = 0;
85
  virtual_wall.low_y = 0;
86

  
70 87
  return 0;
71 88
}
72 89

  
......
131 148
  //  usb_puts(buf);
132 149

  
133 150
  return 250;
134
  
151

  
135 152
  /*
136 153
  long e_mag = sqrt_100_approx(e_x*e_x + e_y*e_y);
137 154
  long v_mag = sqrt_100_approx(v_x*v_x + v_y*v_y);
......
141 158
  long v_norm_y = (1000 * v_y) / v_mag;
142 159
  long e_dot_v = e_norm_x*v_norm_x + e_norm_y*v_norm_y;
143 160
  */
144
  
145 161

  
162

  
146 163
}
147 164

  
165
static void move_routine() {
166
  do {    
167
    int count = 0;
168
    while (!updated_robot_pos_ready) {
169
      wl_do();
170
      if (count++ == 5000) { // in case the server missed it...
171
        request_abs_position();
172
        count = 0;
173
      }
174
    }
175

  
176
    wl_do();
177

  
178
    if (robot_lost) {
179
      motors_off();
180
    }
181

  
182
    updated_robot_pos_ready = 0;
183
    request_abs_position();
184
  } while (!new_movement_command_received);
185
}
186

  
148 187
static void move_to_position_routine(unsigned target_x, unsigned target_y) {
149 188
  new_movement_command_received = 0;
150 189

  
......
194 233

  
195 234
      //sprintf(buf, "v: (x:%d,y:%d)\n", v_x, v_y);
196 235
      //usb_puts(buf);
197
    
236

  
198 237
      if (abs(v_x) < 2 && abs(v_x) < 2) {
199 238
        // if we didn't move since the last check, just go forward.
200 239
        motor1_set(1, 180);
......
299 338
      break;
300 339

  
301 340
      //Analog
302
    case CALL_ANALOG8:
303
      break;
304
    case CALL_ANALOG10:
305
      break;
306 341
    case WHEEL:
307 342
      break;
308 343

  
......
350 385
      motors_off();
351 386
      break;
352 387

  
388
    case SERVER_REPORT_VIRTUAL_WALL_UPPER:
389
      virtual_wall.up_x = int_args[0];
390
      virtual_wall.up_y = int_args[1];
391
      break;
392

  
393
    case SERVER_REPORT_VIRTUAL_WALL_LOWER:
394
      virtual_wall.low_x = int_args[0];
395
      virtual_wall.low_y = int_args[1];
396
      break;
397

  
353 398
    case SERVER_REPORT_POSITION_TO_ROBOT:
354 399
      robot_lost = 0;
355 400

  
......
360 405

  
361 406
      updated_robot_pos_ready = 1;
362 407

  
408
      if (robot_x < virtual_wall.up_x || robot_x > virtual_wall.low_x || robot_y < virtual_wall.up_x
409
          || robot_y > virtual_wall.up_y) {
410
        robot_lost = 1;
411
        motors_off();
412
      }
413

  
363 414
      //    sprintf(buf, "pos is: (x:%d, y:%d)\n", robot_x, robot_y);
364 415
      //usb_puts(buf);
365 416
      break;
......
412 463
      usb_puts("calling motors_init\n");
413 464
      motors_init();
414 465
      break;
466

  
415 467
    case MOTOR1_SET:
416 468
      new_movement_command_received = 1;
417 469

  
......
419 471
      usb_puts(buf);
420 472
      motor1_set(args[0], args[1]);
421 473
      break;
474

  
422 475
    case MOTOR2_SET:
423 476
      new_movement_command_received = 1;
424 477

  
......
426 479
      usb_puts(buf);
427 480
      motor2_set(args[0], args[1]);
428 481
      break;
482

  
429 483
    case MOTORS_OFF:
430 484
      new_movement_command_received = 1;
431 485

  
432 486
      usb_puts("calling motors_off\n");
433 487
      motors_off();
434 488
      break;
489

  
435 490
    case MOVE:
436 491
      new_movement_command_received = 1;
437
      /* format for move: left direction, left velocity, right direction, right velocity */	  
492
      /* format for move: left direction, left velocity, right direction, right velocity */
438 493
      sprintf(buf, "calling move\n");
439 494
      usb_puts(buf);
440
	  motor1_set(args[0], args[1]);
441
	  motor2_set(args[2], args[3]);  
495
      motor1_set(args[0], args[1]);
496
      motor2_set(args[2], args[3]);
497

  
498
      /* Loop and update position for virtual wall. */
499
      move_routine();
442 500
      break;
501

  
443 502
    case PRINTF:
444 503
      usb_puts((char*)pkt->data);
445 504
      break;

Also available in: Unified diff