Pulse Width Modulation RC Servomotor Library compatible with classic and extended models. Models limit are customizable

Dependents:   FRC_2018 0hackton_08_06_18 0hackton_08_06_18_publish Kenya_2019 ... more

Committer:
haarkon
Date:
Tue Jun 05 07:25:09 2018 +0000
Revision:
6:cf65fc8b0de1
Parent:
1:8482eba4d652
Child:
7:014d36c33b73
Soft RC Servo (without PWMOut)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haarkon 0:b8dcca3dc509 1 #include "RC_Servo.h"
haarkon 0:b8dcca3dc509 2
haarkon 6:cf65fc8b0de1 3 RC_Servo::RC_Servo(PinName PWM, int _extended) : _RCpwm(PWM)
haarkon 0:b8dcca3dc509 4 {
haarkon 6:cf65fc8b0de1 5 _tickRC.attach(callback(this,&RC_Servo::generatePwm),0.02);
haarkon 0:b8dcca3dc509 6 if (_extended) {
haarkon 6:cf65fc8b0de1 7 _RCpMin = 400;
haarkon 6:cf65fc8b0de1 8 _RCpMax = 2400;
haarkon 0:b8dcca3dc509 9 } else {
haarkon 6:cf65fc8b0de1 10 _RCpMin = 1000;
haarkon 6:cf65fc8b0de1 11 _RCpMax = 2000;
haarkon 0:b8dcca3dc509 12 }
haarkon 0:b8dcca3dc509 13 }
haarkon 0:b8dcca3dc509 14
haarkon 6:cf65fc8b0de1 15 void RC_Servo::generatePwm (void)
haarkon 6:cf65fc8b0de1 16 {
haarkon 6:cf65fc8b0de1 17
haarkon 6:cf65fc8b0de1 18 static int output = 0;
haarkon 6:cf65fc8b0de1 19 static long pulseWidthTime = 0;
haarkon 6:cf65fc8b0de1 20
haarkon 6:cf65fc8b0de1 21 if (pulseWidthTime == 0) pulseWidthTime = _RCpMin;
haarkon 6:cf65fc8b0de1 22 _tickRC.detach();
haarkon 6:cf65fc8b0de1 23 output = !output;
haarkon 6:cf65fc8b0de1 24 _RCpwm = output;
haarkon 6:cf65fc8b0de1 25
haarkon 6:cf65fc8b0de1 26 if (output == 1) _tickRC.attach (callback(this,&RC_Servo::generatePwm), pulseWidthTime);
haarkon 6:cf65fc8b0de1 27 else {
haarkon 6:cf65fc8b0de1 28 _tickRC.attach (callback(this,&RC_Servo::generatePwm), 20000-pulseWidthTime);
haarkon 6:cf65fc8b0de1 29 pulseWidthTime = RCdelay;
haarkon 6:cf65fc8b0de1 30 }
haarkon 6:cf65fc8b0de1 31 }
haarkon 6:cf65fc8b0de1 32
haarkon 6:cf65fc8b0de1 33 int RC_Servo::setLimits (int Tmin, int Tmax)
haarkon 6:cf65fc8b0de1 34 {
haarkon 6:cf65fc8b0de1 35 if ((Tmin > 400) && (Tmin < Tmax)) _RCpMin = Tmin;
haarkon 1:8482eba4d652 36 else return Tmin;
haarkon 6:cf65fc8b0de1 37 if ((Tmax < 2400) && (Tmin < Tmax)) _RCpMax = Tmax;
haarkon 1:8482eba4d652 38 else return Tmax;
haarkon 1:8482eba4d652 39 return 0;
haarkon 1:8482eba4d652 40 }
haarkon 1:8482eba4d652 41
haarkon 0:b8dcca3dc509 42 void RC_Servo::write (float position)
haarkon 0:b8dcca3dc509 43 {
haarkon 0:b8dcca3dc509 44 if ((position >= 0) && (position <= 1))
haarkon 6:cf65fc8b0de1 45 RCdelay = _RCpMin + (long)((_RCpMax - _RCpMin) * position);
haarkon 0:b8dcca3dc509 46 }
haarkon 0:b8dcca3dc509 47
haarkon 0:b8dcca3dc509 48 RC_Servo& RC_Servo::operator= (float position)
haarkon 0:b8dcca3dc509 49 {
haarkon 0:b8dcca3dc509 50 write (position);
haarkon 1:8482eba4d652 51 return *this;
haarkon 0:b8dcca3dc509 52 }