A servo controller library

Files at this revision

API Documentation at this revision

Comitter:
Kerneels Bezuidenhout
Date:
Wed Sep 07 02:03:33 2016 +0200
Parent:
1:1b8a9e5b178d
Commit message:
Update

Changed in this revision

ServoController.cpp Show annotated file Show diff for this revision Revisions of this file
ServoController.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/ServoController.cpp	Tue Sep 06 22:10:01 2016 +0200
+++ b/ServoController.cpp	Wed Sep 07 02:03:33 2016 +0200
@@ -3,18 +3,22 @@
 ServoController::ServoController(PinName pwmPin, float ref) :
   _servo(pwmPin)
 {
+    DEG2PW = (500.0f/45.0f)/(1000000.0f);
+    ZEROPW = 500.0f/(1000000.0f);
     _ref = ref;
     _angle = _ref;
+    _servo.period_us(5000);
+    SerServo();
 }
 
 void ServoController::SetServo()
 {
   _angle = ( _angle < 0.0f ? 0.0f : _angle > 180.0f ? 180.0f : _angle );
 
-  _servo.pulsewidth(1+DEG2PW*_angle);
+  _servo.pulsewidth(ZEROPW+DEG2PW*_angle);
 }
 
-void ServoController::GetAngle()
+float ServoController::GetAngle()
 {
   return _angle;
 }
--- a/ServoController.hpp	Tue Sep 06 22:10:01 2016 +0200
+++ b/ServoController.hpp	Wed Sep 07 02:03:33 2016 +0200
@@ -24,14 +24,16 @@
    */
   float GetAngle();
 
+  void SetAbsolute(float angle);
+
 private:
-  PWMOut _servo;
+  PwmOut _servo;
   float _angle;
   float _ref;
-  static const float DEG2PW = (2.0f-1.0f)/180.0f;
+  float DEG2PW;
+  float ZEROPW;
 
   void SetServo();
-  void SetAbsolute(float angle);
 };
 
 #endif