absolute movement
authorMjolnir <thomas.pietrzak@inria.fr>
Mon, 7 Sep 2015 12:50:55 +0000 (14:50 +0200)
committerMjolnir <thomas.pietrzak@inria.fr>
Mon, 7 Sep 2015 12:50:55 +0000 (14:50 +0200)
ArduinoScreen/ArduinoScreen.ino
ArduinoScreen/motorcontrol.ino

index 8f48b022312ee4bbb450519539bdb84a10cf7189..4791a65c100bcd6dd9e2caadea7a8b08cbe0d05b 100644 (file)
@@ -1,4 +1,3 @@
-
 //Motors
 const int limitleftpin  = 3;
 const int limitrightpin = 2;
@@ -6,18 +5,21 @@ const int motorpin1     = 5;
 const int motorpin2     = 4;
 const int motorsenable  = 6;
 
-unsigned int motorspeed =  77; // mm/s
+//DC motors
+const unsigned int rackwidth = 644; //mm
+unsigned int motorspeed      =  77; // mm/s
 
 //Servo motor
-const int servopin     = 7;
+const int servopin      = 7;
 
 //buffer stuff
 char command = 0;
-int value = 0;
+unsigned int value = 0;
 #define POSITIVE 0
 #define NEGATIVE 1
 int sign = POSITIVE;
 int a;
+unsigned int pos;
 
 void eval(int command, int value)
 {
@@ -27,14 +29,20 @@ void eval(int command, int value)
       Serial.println(F("LivingScreen"));
       //Serial.flush();
       break;
+    //relative translation
     case 'T':
-      Serial.print("Translate ");
-      Serial.println(value);
+      //Serial.print("Translate ");
+      //Serial.println(value);
       translate(value);
       break;
+    //absolute movement
+    case 'M':
+      if (value != pos)
+        translate(value - pos);
+      break;
     case 'R':
-      Serial.print("Rotate ");
-      Serial.println(value);
+      //Serial.print("Rotate ");
+      //Serial.println(value);
       rotate(value);
       break;
     case 'A':
@@ -43,13 +51,21 @@ void eval(int command, int value)
       Serial.println(a);
       break;
     case 'C':
-      Serial.println("Calibration");
+      //Serial.println("Calibration");
       calibrate();
       break;
     case 'S':
       Serial.print("Motor speed: ");
       Serial.println(motorspeed);
       break;
+    case 'P':
+      Serial.print("Position: ");
+      Serial.println(pos);
+      break;
+    case 'W':
+      Serial.print("Width: ");
+      Serial.println(rackwidth);
+      break;
     default:
       Serial.print("Unknown command ");
       Serial.write(command);
index b108a54082982c529308d64097df9e6497833b57..12ee1a97b55e84c9587f18cf887e27821a4dad4c 100644 (file)
@@ -1,8 +1,5 @@
 #include <Servo.h>
 
-//DC motors
-const unsigned int rackwidth = 644; //mm
-
 //Servo
 Servo servo;
 
@@ -61,6 +58,8 @@ void calibrate()
   digitalWrite(motorpin2, LOW);
   
   digitalWrite(motorsenable, LOW);
+  
+  pos = rackwidth / 2;
 }
 
 
@@ -96,8 +95,14 @@ void translate(int distance)
     {
       if (!digitalRead(limitrightpin))
       {
-        Serial.println("Left limit reached");
-        break;
+        //Serial.println("Left limit reached");
+        pos = rackwidth;
+        //motor stop
+        digitalWrite(motorpin1, LOW);
+        digitalWrite(motorpin2, LOW);
+        
+        digitalWrite(motorsenable, LOW);
+        return;
       }
       delayMicroseconds(10);
     }
@@ -106,13 +111,24 @@ void translate(int distance)
     {
       if (!digitalRead(limitleftpin))
       {
-        Serial.println("Right limit reached");
-        break;
+        //Serial.println("Right limit reached");
+        pos = 0;
+        //motor stop
+        digitalWrite(motorpin1, LOW);
+        digitalWrite(motorpin2, LOW);
+        
+        digitalWrite(motorsenable, LOW);
+        return;
       }
       delayMicroseconds(10);
     }
   }
-    
+  
+  if (pos + distance < 0)
+    pos = 0;
+  else
+    pos += distance;
+  
   //motor stop
   digitalWrite(motorpin1, LOW);
   digitalWrite(motorpin2, LOW);