PM2_Lib
Dependencies: LSM9DS1 RangeFinder FastPWM
Servo.cpp@22:13db7047054c, 2022-03-23 (annotated)
- Committer:
- pmic
- Date:
- Wed Mar 23 09:36:59 2022 +0000
- Revision:
- 22:13db7047054c
- Parent:
- 21:45d903d986f6
- Child:
- 23:1926540ebbec
Added SetPeriod function in Servo class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 0:86129f1b4a93 | 1 | #include "Servo.h" |
pmic | 4:9c003c402033 | 2 | |
pmic | 18:21c0a669d6ef | 3 | const float Servo::MIN_INPUT = 0.01f; |
pmic | 18:21c0a669d6ef | 4 | const float Servo::MAX_INPUT = 0.99f; |
pmic | 18:21c0a669d6ef | 5 | |
pmic | 20:ea222b81350e | 6 | Servo::Servo(PinName Pin) : ServoPin(Pin) |
pmic | 20:ea222b81350e | 7 | { |
pmic | 20:ea222b81350e | 8 | servoEnabled = false; |
pmic | 20:ea222b81350e | 9 | Position = 0; |
pmic | 20:ea222b81350e | 10 | Period = 0; |
pmic | 20:ea222b81350e | 11 | } |
pmic | 0:86129f1b4a93 | 12 | |
pmic | 10:fe74e8909d3f | 13 | /** |
pmic | 22:13db7047054c | 14 | * Sets the pwm period. |
pmic | 22:13db7047054c | 15 | * @_Period period in mus. |
pmic | 22:13db7047054c | 16 | */ |
pmic | 22:13db7047054c | 17 | void Servo::SetPeriod(float _Period) |
pmic | 22:13db7047054c | 18 | { |
pmic | 22:13db7047054c | 19 | Period = _Period; |
pmic | 22:13db7047054c | 20 | } |
pmic | 22:13db7047054c | 21 | |
pmic | 22:13db7047054c | 22 | /** |
pmic | 16:69911e81dfd4 | 23 | * Sets the desired angle. |
pmic | 16:69911e81dfd4 | 24 | * @_Input a value between 0...1. |
pmic | 10:fe74e8909d3f | 25 | */ |
pmic | 16:69911e81dfd4 | 26 | void Servo::SetPosition(float _Input) |
pmic | 4:9c003c402033 | 27 | { |
pmic | 20:ea222b81350e | 28 | if (servoEnabled) { |
pmic | 20:ea222b81350e | 29 | if (_Input < MIN_INPUT) _Input = MIN_INPUT; |
pmic | 20:ea222b81350e | 30 | if (_Input > MAX_INPUT) _Input = MAX_INPUT; |
pmic | 20:ea222b81350e | 31 | Position = static_cast<int>(_Input * static_cast<float>(Period)); |
pmic | 20:ea222b81350e | 32 | } |
pmic | 4:9c003c402033 | 33 | } |
pmic | 0:86129f1b4a93 | 34 | |
pmic | 4:9c003c402033 | 35 | void Servo::StartPulse() |
pmic | 4:9c003c402033 | 36 | { |
pmic | 4:9c003c402033 | 37 | ServoPin = 1; |
pmic | 10:fe74e8909d3f | 38 | PulseStop.attach(callback(this, &Servo::EndPulse), std::chrono::microseconds{static_cast<long int>(Position)}); |
pmic | 4:9c003c402033 | 39 | } |
pmic | 0:86129f1b4a93 | 40 | |
pmic | 4:9c003c402033 | 41 | void Servo::EndPulse() |
pmic | 4:9c003c402033 | 42 | { |
pmic | 4:9c003c402033 | 43 | ServoPin = 0; |
pmic | 4:9c003c402033 | 44 | } |
pmic | 0:86129f1b4a93 | 45 | |
pmic | 10:fe74e8909d3f | 46 | /** |
pmic | 16:69911e81dfd4 | 47 | * Enables the servo with start angle and period. |
pmic | 16:69911e81dfd4 | 48 | * @_StartInput a value between 0...1. |
pmic | 16:69911e81dfd4 | 49 | * @_Period period in mus. |
pmic | 10:fe74e8909d3f | 50 | */ |
pmic | 16:69911e81dfd4 | 51 | void Servo::Enable(float _StartInput, int _Period) |
pmic | 4:9c003c402033 | 52 | { |
pmic | 20:ea222b81350e | 53 | servoEnabled = true; |
pmic | 18:21c0a669d6ef | 54 | if (_StartInput < MIN_INPUT) _StartInput = MIN_INPUT; |
pmic | 18:21c0a669d6ef | 55 | if (_StartInput > MAX_INPUT) _StartInput = MAX_INPUT; |
pmic | 16:69911e81dfd4 | 56 | Period = _Period; |
pmic | 16:69911e81dfd4 | 57 | Position = static_cast<int>(_StartInput * static_cast<float>(Period)); |
pmic | 10:fe74e8909d3f | 58 | Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)}); |
pmic | 4:9c003c402033 | 59 | } |
pmic | 0:86129f1b4a93 | 60 | |
pmic | 10:fe74e8909d3f | 61 | /** |
pmic | 21:45d903d986f6 | 62 | * Re enables the servo with last set angle and period. |
pmic | 21:45d903d986f6 | 63 | */ |
pmic | 21:45d903d986f6 | 64 | void Servo::ReEnable() |
pmic | 21:45d903d986f6 | 65 | { |
pmic | 21:45d903d986f6 | 66 | servoEnabled = true; |
pmic | 21:45d903d986f6 | 67 | Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)}); |
pmic | 21:45d903d986f6 | 68 | } |
pmic | 21:45d903d986f6 | 69 | |
pmic | 21:45d903d986f6 | 70 | /** |
pmic | 10:fe74e8909d3f | 71 | * Disables the servo. |
pmic | 10:fe74e8909d3f | 72 | */ |
pmic | 4:9c003c402033 | 73 | void Servo::Disable() |
pmic | 4:9c003c402033 | 74 | { |
pmic | 20:ea222b81350e | 75 | servoEnabled = false; |
pmic | 4:9c003c402033 | 76 | Pulse.detach(); |
pmic | 20:ea222b81350e | 77 | } |
pmic | 20:ea222b81350e | 78 | |
pmic | 20:ea222b81350e | 79 | /** |
pmic | 20:ea222b81350e | 80 | * Returns true if Servo is enabled. |
pmic | 20:ea222b81350e | 81 | * @return isEnable. |
pmic | 20:ea222b81350e | 82 | */ |
pmic | 20:ea222b81350e | 83 | bool Servo::isEnabled() |
pmic | 20:ea222b81350e | 84 | { |
pmic | 20:ea222b81350e | 85 | return servoEnabled; |
pmic | 4:9c003c402033 | 86 | } |