PM2_Lib

Dependencies:   LSM9DS1 RangeFinder FastPWM

Committer:
pmic
Date:
Wed Mar 23 09:30:21 2022 +0000
Revision:
21:45d903d986f6
Parent:
20:ea222b81350e
Child:
22:13db7047054c
Added ReEnable function to Servo class

Who changed what in which revision?

UserRevisionLine numberNew 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 16:69911e81dfd4 14 * Sets the desired angle.
pmic 16:69911e81dfd4 15 * @_Input a value between 0...1.
pmic 10:fe74e8909d3f 16 */
pmic 16:69911e81dfd4 17 void Servo::SetPosition(float _Input)
pmic 4:9c003c402033 18 {
pmic 20:ea222b81350e 19 if (servoEnabled) {
pmic 20:ea222b81350e 20 if (_Input < MIN_INPUT) _Input = MIN_INPUT;
pmic 20:ea222b81350e 21 if (_Input > MAX_INPUT) _Input = MAX_INPUT;
pmic 20:ea222b81350e 22 Position = static_cast<int>(_Input * static_cast<float>(Period));
pmic 20:ea222b81350e 23 }
pmic 4:9c003c402033 24 }
pmic 0:86129f1b4a93 25
pmic 4:9c003c402033 26 void Servo::StartPulse()
pmic 4:9c003c402033 27 {
pmic 4:9c003c402033 28 ServoPin = 1;
pmic 10:fe74e8909d3f 29 PulseStop.attach(callback(this, &Servo::EndPulse), std::chrono::microseconds{static_cast<long int>(Position)});
pmic 4:9c003c402033 30 }
pmic 0:86129f1b4a93 31
pmic 4:9c003c402033 32 void Servo::EndPulse()
pmic 4:9c003c402033 33 {
pmic 4:9c003c402033 34 ServoPin = 0;
pmic 4:9c003c402033 35 }
pmic 0:86129f1b4a93 36
pmic 10:fe74e8909d3f 37 /**
pmic 16:69911e81dfd4 38 * Enables the servo with start angle and period.
pmic 16:69911e81dfd4 39 * @_StartInput a value between 0...1.
pmic 16:69911e81dfd4 40 * @_Period period in mus.
pmic 10:fe74e8909d3f 41 */
pmic 16:69911e81dfd4 42 void Servo::Enable(float _StartInput, int _Period)
pmic 4:9c003c402033 43 {
pmic 20:ea222b81350e 44 servoEnabled = true;
pmic 18:21c0a669d6ef 45 if (_StartInput < MIN_INPUT) _StartInput = MIN_INPUT;
pmic 18:21c0a669d6ef 46 if (_StartInput > MAX_INPUT) _StartInput = MAX_INPUT;
pmic 16:69911e81dfd4 47 Period = _Period;
pmic 16:69911e81dfd4 48 Position = static_cast<int>(_StartInput * static_cast<float>(Period));
pmic 10:fe74e8909d3f 49 Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)});
pmic 4:9c003c402033 50 }
pmic 0:86129f1b4a93 51
pmic 10:fe74e8909d3f 52 /**
pmic 21:45d903d986f6 53 * Re enables the servo with last set angle and period.
pmic 21:45d903d986f6 54 */
pmic 21:45d903d986f6 55 void Servo::ReEnable()
pmic 21:45d903d986f6 56 {
pmic 21:45d903d986f6 57 servoEnabled = true;
pmic 21:45d903d986f6 58 Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)});
pmic 21:45d903d986f6 59 }
pmic 21:45d903d986f6 60
pmic 21:45d903d986f6 61 /**
pmic 10:fe74e8909d3f 62 * Disables the servo.
pmic 10:fe74e8909d3f 63 */
pmic 4:9c003c402033 64 void Servo::Disable()
pmic 4:9c003c402033 65 {
pmic 20:ea222b81350e 66 servoEnabled = false;
pmic 4:9c003c402033 67 Pulse.detach();
pmic 20:ea222b81350e 68 }
pmic 20:ea222b81350e 69
pmic 20:ea222b81350e 70 /**
pmic 20:ea222b81350e 71 * Returns true if Servo is enabled.
pmic 20:ea222b81350e 72 * @return isEnable.
pmic 20:ea222b81350e 73 */
pmic 20:ea222b81350e 74 bool Servo::isEnabled()
pmic 20:ea222b81350e 75 {
pmic 20:ea222b81350e 76 return servoEnabled;
pmic 4:9c003c402033 77 }