Made changes to individually control servo speeds

Dependents:   VariableSpeedServo

Fork of Servo by Jasper Denkers

Revision:
2:466f005a4dd5
Parent:
0:30b972d2dcec
Child:
3:774dd54867f2
--- a/Servo.cpp	Sun Oct 17 13:34:19 2010 +0000
+++ b/Servo.cpp	Sun Oct 11 20:50:48 2015 +0000
@@ -2,13 +2,46 @@
 #include "mbed.h"
 
     Servo::Servo(PinName Pin) : ServoPin(Pin) {}
+    
+int Servo:: SpeedConvert (int _speed) {
+    
+    if (_speed > 50)
+            _speed = 50;
+    else if (_speed < 1)
+            _speed =1;
+    speed = 10000/_speed;       
+    return speed;
+    
+    }   
 
-    void Servo::SetPosition(int Pos) {
-        Position = Pos;
+ void Servo::SetPosition(int Pos) {           //go to position 
+        NewPosition = Pos;
     }
+    
+void Servo::Update()
+{
+
+
+    if(NewPosition != CurrentPosition) {
+        
+        if(NewPosition > CurrentPosition) {
+            CurrentPosition++;
+            Position =  CurrentPosition;
+            
+        }
+         else  {
+            CurrentPosition--;
+            Position = CurrentPosition;
+        }
+    }
+    Position = CurrentPosition;
+
+}
+         
 
     void Servo::StartPulse() {
         ServoPin = 1;
+        
         PulseStop.attach_us(this, &Servo::EndPulse, Position);
     }
 
@@ -16,11 +49,15 @@
         ServoPin = 0;
     }
 
-    void Servo::Enable(int StartPos, int Period) {
+    void Servo::Enable(int StartPos, int RefreshRate, int _speed) {
+        SpeedConvert(_speed);
+        Period = (1000000/RefreshRate);                            //converts from HZ to period in  us 
         Position = StartPos;
-        Pulse.attach_us(this, &Servo::StartPulse, Period);
+        CurrentPosition = StartPos;                              //sets CurrentPosition to  StartPos value 
+        Pulse.attach_us(this, &Servo::StartPulse, Period);       //starts servo period Ticker r
+        Speed.attach_us(this, &Servo::Update, speed);            //starts servo Speed Ticker  speed of servo update pulse width steps in  us,    slow =100000 100ms per step
     }
 
-    void Servo::Disable() {
+    void Servo::Disable() {         //turns off Ticker Pulse 
         Pulse.detach();
     }
\ No newline at end of file