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@7:014d36c33b73, 2018-06-05 (annotated)
- Committer:
- haarkon
- Date:
- Tue Jun 05 12:22:17 2018 +0000
- Revision:
- 7:014d36c33b73
- Parent:
- 6:cf65fc8b0de1
Tested library using soft PWM
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 | 6:cf65fc8b0de1 | 3 | RC_Servo::RC_Servo(PinName PWM, int _extended) : _RCpwm(PWM) |
haarkon | 0:b8dcca3dc509 | 4 | { |
haarkon | 0:b8dcca3dc509 | 5 | if (_extended) { |
haarkon | 6:cf65fc8b0de1 | 6 | _RCpMin = 400; |
haarkon | 6:cf65fc8b0de1 | 7 | _RCpMax = 2400; |
haarkon | 0:b8dcca3dc509 | 8 | } else { |
haarkon | 6:cf65fc8b0de1 | 9 | _RCpMin = 1000; |
haarkon | 6:cf65fc8b0de1 | 10 | _RCpMax = 2000; |
haarkon | 0:b8dcca3dc509 | 11 | } |
haarkon | 0:b8dcca3dc509 | 12 | } |
haarkon | 0:b8dcca3dc509 | 13 | |
haarkon | 6:cf65fc8b0de1 | 14 | int RC_Servo::setLimits (int Tmin, int Tmax) |
haarkon | 6:cf65fc8b0de1 | 15 | { |
haarkon | 6:cf65fc8b0de1 | 16 | if ((Tmin > 400) && (Tmin < Tmax)) _RCpMin = Tmin; |
haarkon | 1:8482eba4d652 | 17 | else return Tmin; |
haarkon | 6:cf65fc8b0de1 | 18 | if ((Tmax < 2400) && (Tmin < Tmax)) _RCpMax = Tmax; |
haarkon | 1:8482eba4d652 | 19 | else return Tmax; |
haarkon | 1:8482eba4d652 | 20 | return 0; |
haarkon | 1:8482eba4d652 | 21 | } |
haarkon | 1:8482eba4d652 | 22 | |
haarkon | 7:014d36c33b73 | 23 | void RC_Servo::write (double position) |
haarkon | 0:b8dcca3dc509 | 24 | { |
haarkon | 0:b8dcca3dc509 | 25 | if ((position >= 0) && (position <= 1)) |
haarkon | 7:014d36c33b73 | 26 | _RCpwm.pulsewidth_us (_RCpMin + (long)((_RCpMax - _RCpMin) * position)); |
haarkon | 0:b8dcca3dc509 | 27 | } |
haarkon | 0:b8dcca3dc509 | 28 | |
haarkon | 7:014d36c33b73 | 29 | RC_Servo& RC_Servo::operator= (double position) |
haarkon | 0:b8dcca3dc509 | 30 | { |
haarkon | 0:b8dcca3dc509 | 31 | write (position); |
haarkon | 1:8482eba4d652 | 32 | return *this; |
haarkon | 0:b8dcca3dc509 | 33 | } |