A library to control a CYS S8218 servo

Dependents:   Heiko

Files at this revision

API Documentation at this revision

Comitter:
Kerneels Bezuidenhout
Date:
Wed Sep 07 03:07:07 2016 +0200
Parent:
0:23a6381d15ff
Child:
2:695c74c6d483
Commit message:
Initial version

Changed in this revision

CYS8218Controller.cpp Show annotated file Show diff for this revision Revisions of this file
CYS8218Controller.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/CYS8218Controller.cpp	Wed Sep 07 00:15:46 2016 +0000
+++ b/CYS8218Controller.cpp	Wed Sep 07 03:07:07 2016 +0200
@@ -0,0 +1,42 @@
+#include "CYS8218Controller.hpp"
+
+CYS8218Controller::CYS8218Controller(PinName pwmPin, float initAngle) :
+  _servo(pwmPin)
+{
+  _angle = initAngle;
+  ZEROPW = 0.0015f; //1500us default position
+  PW_PER_DEG = 0.0005f/45.0f; //500us -> 45deg default
+  _servo.period_us(5000);
+  SetServo();
+}
+
+void CYS8218Controller::SetServo()
+{
+  float pw = ZEROPW+_angle*PW_PER_DEG;
+
+  pw = ( pw < MIN_PW ? MIN_PW : pw > MAX_PW ? MAX_PW : pw);
+
+  _servo.pulsewidth(pw);
+}
+
+void CYS8218Controller::SaveZero()
+{
+  ZEROPW = (ZEROPW+_angle*PW_PER_DEG);
+}
+
+void CYS8218Controller::Calibrate(float actualAngle)
+{
+  PW_PER_DEG = (_angle*PW_PER_DEG)/actualAngle;
+}
+
+void CYS8218Controller::Set(float angle)
+{
+  _angle = angle;
+  SetServo();
+}
+
+void CYS8218Controller::Move(float relAngle)
+{
+  _angle += relAngle;
+  SetServo();
+}
--- a/CYS8218Controller.hpp	Wed Sep 07 00:15:46 2016 +0000
+++ b/CYS8218Controller.hpp	Wed Sep 07 03:07:07 2016 +0200
@@ -0,0 +1,58 @@
+#ifndef CYS8218CONTROLLER_H
+#define CYS8218CONTROLLER_H
+
+//Includes
+#include "mbed.h"
+
+/**
+ *  A CYS S8218 servo controller library
+ *
+ * @author CA Bezuidenhout
+ */
+class CYS8218Controller
+{
+public:
+  /**
+   * @param pwmPin : PWM pin of servo / orange wire
+   * @param initAngle : The angle the servo goes to when object is created
+   */
+  CYS8218Controller(PinName pwmPin, float initAngle = 0.0f);
+
+  /**
+   * Saves the current position as the zero position.
+   */
+  void SaveZero();
+
+  /**
+   * Calibrate the servo
+   * @param actualAngle : The actual angle from zero, of the current position in degrees.
+   */
+  void Calibrate(float actualAngle);
+
+  /**
+   * Sets the angular position of the servo
+   * @param angle : Desired angle from the zero position in degrees
+   */
+  void Set(float angle);
+
+  /**
+   * Moves to an relative angular position from the current position
+   * @param relAngle : The relative angle to move in degrees
+   */
+  void Move(float relAngle);
+
+
+private:
+  PwmOut _servo;
+  float _angle;
+  float _ref;
+  float PW_PER_DEG;
+  float ZEROPW;
+
+  static const float MIN_PW = 0.0005;
+  static const float MAX_PW = 0.0025f;
+
+  void SetServo();
+};
+
+#endif