PM2_Lib

Dependencies:   LSM9DS1 RangeFinder FastPWM

Committer:
pmic
Date:
Tue Mar 22 09:43:13 2022 +0000
Revision:
20:ea222b81350e
Parent:
19:518ed284d98b
Child:
21:45d903d986f6
Create isEnabled function in 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 10:fe74e8909d3f 53 * Disables the servo.
pmic 10:fe74e8909d3f 54 */
pmic 4:9c003c402033 55 void Servo::Disable()
pmic 4:9c003c402033 56 {
pmic 20:ea222b81350e 57 servoEnabled = false;
pmic 4:9c003c402033 58 Pulse.detach();
pmic 20:ea222b81350e 59 }
pmic 20:ea222b81350e 60
pmic 20:ea222b81350e 61 /**
pmic 20:ea222b81350e 62 * Returns true if Servo is enabled.
pmic 20:ea222b81350e 63 * @return isEnable.
pmic 20:ea222b81350e 64 */
pmic 20:ea222b81350e 65 bool Servo::isEnabled()
pmic 20:ea222b81350e 66 {
pmic 20:ea222b81350e 67 return servoEnabled;
pmic 4:9c003c402033 68 }