Index: colonet_dragonfly.c
===================================================================
--- colonet_dragonfly.c	(revision 660)
+++ colonet_dragonfly.c	(revision 661)
@@ -24,15 +24,25 @@
   void (*handler)(void);
 } UserHandler;
 
+typedef struct {
+  int up_x;
+  int up_y;
+  int low_x;
+  int low_y;
+} VirtualWall;
+
 /* Globals (internal) */
 static UserHandler user_handlers[USER_DEFINED_MSG_TOTAL];
 
-static unsigned robot_x, robot_y;
-static unsigned last_x, last_y;
+static volatile unsigned robot_x, robot_y;
+static volatile unsigned last_x, last_y;
 static volatile int updated_robot_pos_ready;
 static volatile int new_movement_command_received;
 static volatile int robot_lost; //set to 1 if robot's position is not known by the server, else 0.
+static volatile VirtualWall virtual_wall;
 
+static int virtual_wall_available;
+
 #define ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST (1)
 
 /* Internal function prototypes */
@@ -40,6 +50,7 @@
 static void colonet_handle_receive(char type, int source, unsigned char* packet, int length);
 static void move_to_position_routine(unsigned x, unsigned y);
 static int dist_squared(int x1, int y1, int x2, int y2);
+static void move_routine(void);
 
 static PacketGroupHandler colonet_pgh;
 
@@ -67,6 +78,12 @@
   new_movement_command_received = 0;
   robot_lost = 1;
 
+  virtual_wall_available = 0;
+  virtual_wall.up_x = 0;
+  virtual_wall.up_y = 0;
+  virtual_wall.low_x = 0;
+  virtual_wall.low_y = 0;
+
   return 0;
 }
 
@@ -131,7 +148,7 @@
   //  usb_puts(buf);
 
   return 250;
-  
+
   /*
   long e_mag = sqrt_100_approx(e_x*e_x + e_y*e_y);
   long v_mag = sqrt_100_approx(v_x*v_x + v_y*v_y);
@@ -141,10 +158,32 @@
   long v_norm_y = (1000 * v_y) / v_mag;
   long e_dot_v = e_norm_x*v_norm_x + e_norm_y*v_norm_y;
   */
-  
 
+
 }
 
+static void move_routine() {
+  do {    
+    int count = 0;
+    while (!updated_robot_pos_ready) {
+      wl_do();
+      if (count++ == 5000) { // in case the server missed it...
+        request_abs_position();
+        count = 0;
+      }
+    }
+
+    wl_do();
+
+    if (robot_lost) {
+      motors_off();
+    }
+
+    updated_robot_pos_ready = 0;
+    request_abs_position();
+  } while (!new_movement_command_received);
+}
+
 static void move_to_position_routine(unsigned target_x, unsigned target_y) {
   new_movement_command_received = 0;
 
@@ -194,7 +233,7 @@
 
       //sprintf(buf, "v: (x:%d,y:%d)\n", v_x, v_y);
       //usb_puts(buf);
-    
+
       if (abs(v_x) < 2 && abs(v_x) < 2) {
         // if we didn't move since the last check, just go forward.
         motor1_set(1, 180);
@@ -299,10 +338,6 @@
       break;
 
       //Analog
-    case CALL_ANALOG8:
-      break;
-    case CALL_ANALOG10:
-      break;
     case WHEEL:
       break;
 
@@ -350,6 +385,16 @@
       motors_off();
       break;
 
+    case SERVER_REPORT_VIRTUAL_WALL_UPPER:
+      virtual_wall.up_x = int_args[0];
+      virtual_wall.up_y = int_args[1];
+      break;
+
+    case SERVER_REPORT_VIRTUAL_WALL_LOWER:
+      virtual_wall.low_x = int_args[0];
+      virtual_wall.low_y = int_args[1];
+      break;
+
     case SERVER_REPORT_POSITION_TO_ROBOT:
       robot_lost = 0;
 
@@ -360,6 +405,12 @@
 
       updated_robot_pos_ready = 1;
 
+      if (robot_x < virtual_wall.up_x || robot_x > virtual_wall.low_x || robot_y < virtual_wall.up_x
+          || robot_y > virtual_wall.up_y) {
+        robot_lost = 1;
+        motors_off();
+      }
+
       //    sprintf(buf, "pos is: (x:%d, y:%d)\n", robot_x, robot_y);
       //usb_puts(buf);
       break;
@@ -412,6 +463,7 @@
       usb_puts("calling motors_init\n");
       motors_init();
       break;
+
     case MOTOR1_SET:
       new_movement_command_received = 1;
 
@@ -419,6 +471,7 @@
       usb_puts(buf);
       motor1_set(args[0], args[1]);
       break;
+
     case MOTOR2_SET:
       new_movement_command_received = 1;
 
@@ -426,6 +479,7 @@
       usb_puts(buf);
       motor2_set(args[0], args[1]);
       break;
+
     case MOTORS_OFF:
       new_movement_command_received = 1;
 
@@ -432,14 +486,19 @@
       usb_puts("calling motors_off\n");
       motors_off();
       break;
+
     case MOVE:
       new_movement_command_received = 1;
-      /* format for move: left direction, left velocity, right direction, right velocity */	  
+      /* format for move: left direction, left velocity, right direction, right velocity */
       sprintf(buf, "calling move\n");
       usb_puts(buf);
-	  motor1_set(args[0], args[1]);
-	  motor2_set(args[2], args[3]);  
+      motor1_set(args[0], args[1]);
+      motor2_set(args[2], args[3]);
+
+      /* Loop and update position for virtual wall. */
+      move_routine();
       break;
+
     case PRINTF:
       usb_puts((char*)pkt->data);
       break;
