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
RC_Servo.cpp@0:b8dcca3dc509, 2018-05-21 (annotated)
- Committer:
- haarkon
- Date:
- Mon May 21 15:55:54 2018 +0000
- Revision:
- 0:b8dcca3dc509
- Child:
- 1:8482eba4d652
Untested first release of RC Servomotor library;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:b8dcca3dc509 | 15 | void RC_Servo::write (float position) |
haarkon | 0:b8dcca3dc509 | 16 | { |
haarkon | 0:b8dcca3dc509 | 17 | if ((position >= 0) && (position <= 1)) |
haarkon | 0:b8dcca3dc509 | 18 | _pwm.pulsewidth_us (_pMin + (int)(position * (_pMax - _pMin))); |
haarkon | 0:b8dcca3dc509 | 19 | } |
haarkon | 0:b8dcca3dc509 | 20 | |
haarkon | 0:b8dcca3dc509 | 21 | /** |
haarkon | 0:b8dcca3dc509 | 22 | * A short hand for write |
haarkon | 0:b8dcca3dc509 | 23 | */ |
haarkon | 0:b8dcca3dc509 | 24 | RC_Servo& RC_Servo::operator= (float position) |
haarkon | 0:b8dcca3dc509 | 25 | { |
haarkon | 0:b8dcca3dc509 | 26 | write (position); |
haarkon | 0:b8dcca3dc509 | 27 | } |