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:
Mon May 21 16:07:20 2018 +0000
Revision:
1:8482eba4d652
Parent:
0:b8dcca3dc509
Child:
6:cf65fc8b0de1
Added func to set Min and Max limits and solve cosmetic issues

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haarkon 0:b8dcca3dc509 1 #include "RC_Servo.h"
haarkon 0:b8dcca3dc509 2
haarkon 0:b8dcca3dc509 3 RC_Servo::RC_Servo(PinName PWM, int _extended) : _pwm(PWM)
haarkon 0:b8dcca3dc509 4 {
haarkon 0:b8dcca3dc509 5 _pwm.period(0.02);
haarkon 0:b8dcca3dc509 6 if (_extended) {
haarkon 0:b8dcca3dc509 7 _pMin = 400;
haarkon 0:b8dcca3dc509 8 _pMax = 2400;
haarkon 0:b8dcca3dc509 9 } else {
haarkon 0:b8dcca3dc509 10 _pMin = 1000;
haarkon 0:b8dcca3dc509 11 _pMax = 2000;
haarkon 0:b8dcca3dc509 12 }
haarkon 0:b8dcca3dc509 13 }
haarkon 0:b8dcca3dc509 14
haarkon 1:8482eba4d652 15 int RC_Servo::setLimits (int Tmin, int Tmax) {
haarkon 1:8482eba4d652 16 if ((Tmin > 400) && (Tmin < Tmax)) _pMin = Tmin;
haarkon 1:8482eba4d652 17 else return Tmin;
haarkon 1:8482eba4d652 18 if ((Tmax < 2400) && (Tmin < Tmax)) _pMax = Tmax;
haarkon 1:8482eba4d652 19 else return Tmax;
haarkon 1:8482eba4d652 20 return 0;
haarkon 1:8482eba4d652 21 }
haarkon 1:8482eba4d652 22
haarkon 0:b8dcca3dc509 23 void RC_Servo::write (float position)
haarkon 0:b8dcca3dc509 24 {
haarkon 0:b8dcca3dc509 25 if ((position >= 0) && (position <= 1))
haarkon 0:b8dcca3dc509 26 _pwm.pulsewidth_us (_pMin + (int)(position * (_pMax - _pMin)));
haarkon 0:b8dcca3dc509 27 }
haarkon 0:b8dcca3dc509 28
haarkon 0:b8dcca3dc509 29 RC_Servo& RC_Servo::operator= (float position)
haarkon 0:b8dcca3dc509 30 {
haarkon 0:b8dcca3dc509 31 write (position);
haarkon 1:8482eba4d652 32 return *this;
haarkon 0:b8dcca3dc509 33 }