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
Revision 7:014d36c33b73, committed 2018-06-05
- Comitter:
- haarkon
- Date:
- Tue Jun 05 12:22:17 2018 +0000
- Parent:
- 6:cf65fc8b0de1
- Commit message:
- Tested library using soft PWM
Changed in this revision
diff -r cf65fc8b0de1 -r 014d36c33b73 RC_Servo.cpp --- a/RC_Servo.cpp Tue Jun 05 07:25:09 2018 +0000 +++ b/RC_Servo.cpp Tue Jun 05 12:22:17 2018 +0000 @@ -2,7 +2,6 @@ RC_Servo::RC_Servo(PinName PWM, int _extended) : _RCpwm(PWM) { - _tickRC.attach(callback(this,&RC_Servo::generatePwm),0.02); if (_extended) { _RCpMin = 400; _RCpMax = 2400; @@ -12,24 +11,6 @@ } } -void RC_Servo::generatePwm (void) -{ - - static int output = 0; - static long pulseWidthTime = 0; - - if (pulseWidthTime == 0) pulseWidthTime = _RCpMin; - _tickRC.detach(); - output = !output; - _RCpwm = output; - - if (output == 1) _tickRC.attach (callback(this,&RC_Servo::generatePwm), pulseWidthTime); - else { - _tickRC.attach (callback(this,&RC_Servo::generatePwm), 20000-pulseWidthTime); - pulseWidthTime = RCdelay; - } -} - int RC_Servo::setLimits (int Tmin, int Tmax) { if ((Tmin > 400) && (Tmin < Tmax)) _RCpMin = Tmin; @@ -39,13 +20,13 @@ return 0; } -void RC_Servo::write (float position) +void RC_Servo::write (double position) { if ((position >= 0) && (position <= 1)) - RCdelay = _RCpMin + (long)((_RCpMax - _RCpMin) * position); + _RCpwm.pulsewidth_us (_RCpMin + (long)((_RCpMax - _RCpMin) * position)); } -RC_Servo& RC_Servo::operator= (float position) +RC_Servo& RC_Servo::operator= (double position) { write (position); return *this;
diff -r cf65fc8b0de1 -r 014d36c33b73 RC_Servo.h --- a/RC_Servo.h Tue Jun 05 07:25:09 2018 +0000 +++ b/RC_Servo.h Tue Jun 05 12:22:17 2018 +0000 @@ -39,6 +39,7 @@ * Includes : Mbed Library */ #include "mbed.h" +#include "SoftPWM.h" /** * RC_Servo @@ -51,11 +52,6 @@ public : /** - * Global Variable that define pulsewidth in microsecond - */ - long RCdelay; - - /** * Constructor of a RC Servo (analog). * * @param PWM (PinName) : is the Mbed pin used to generate the PWM signal @@ -77,21 +73,18 @@ * * @param position : float number between 0 and 1 (0 = Min, 1 = Max) */ - void write (float position); + void write (double position); /** * A short hand for write */ - RC_Servo& operator= (float position); + RC_Servo& operator= (double position); private : int _RCpMin, _RCpMax; - Ticker _tickRC; - - void generatePwm (void); + SoftPWM _RCpwm; protected : - DigitalOut _RCpwm ; }; #endif //GP2A_H \ No newline at end of file
diff -r cf65fc8b0de1 -r 014d36c33b73 SoftPWM.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoftPWM.lib Tue Jun 05 12:22:17 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/FRC-Hackathon/code/SoftPWM/#9aba3dc9cd97