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
Diff: RC_Servo.cpp
- Revision:
- 0:b8dcca3dc509
- Child:
- 1:8482eba4d652
diff -r 000000000000 -r b8dcca3dc509 RC_Servo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RC_Servo.cpp Mon May 21 15:55:54 2018 +0000 @@ -0,0 +1,27 @@ +#include "RC_Servo.h" + +RC_Servo::RC_Servo(PinName PWM, int _extended) : _pwm(PWM) +{ + _pwm.period(0.02); + if (_extended) { + _pMin = 400; + _pMax = 2400; + } else { + _pMin = 1000; + _pMax = 2000; + } +} + +void RC_Servo::write (float position) +{ + if ((position >= 0) && (position <= 1)) + _pwm.pulsewidth_us (_pMin + (int)(position * (_pMax - _pMin))); +} + +/** + * A short hand for write + */ +RC_Servo& RC_Servo::operator= (float position) +{ + write (position); +}