PM2_Lib

Dependencies:   LSM9DS1 RangeFinder FastPWM

Committer:
pmic
Date:
Tue Mar 22 08:35:07 2022 +0000
Revision:
19:518ed284d98b
Parent:
18:21c0a669d6ef
Child:
20:ea222b81350e
Adjusted min and max value in different classes.

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 4:9c003c402033 6 Servo::Servo(PinName Pin) : ServoPin(Pin) {}
pmic 0:86129f1b4a93 7
pmic 10:fe74e8909d3f 8 /**
pmic 16:69911e81dfd4 9 * Sets the desired angle.
pmic 16:69911e81dfd4 10 * @_Input a value between 0...1.
pmic 10:fe74e8909d3f 11 */
pmic 16:69911e81dfd4 12 void Servo::SetPosition(float _Input)
pmic 4:9c003c402033 13 {
pmic 18:21c0a669d6ef 14 if (_Input < MIN_INPUT) _Input = MIN_INPUT;
pmic 18:21c0a669d6ef 15 if (_Input > MAX_INPUT) _Input = MAX_INPUT;
pmic 16:69911e81dfd4 16 Position = static_cast<int>(_Input * static_cast<float>(Period));
pmic 4:9c003c402033 17 }
pmic 0:86129f1b4a93 18
pmic 4:9c003c402033 19 void Servo::StartPulse()
pmic 4:9c003c402033 20 {
pmic 4:9c003c402033 21 ServoPin = 1;
pmic 10:fe74e8909d3f 22 PulseStop.attach(callback(this, &Servo::EndPulse), std::chrono::microseconds{static_cast<long int>(Position)});
pmic 4:9c003c402033 23 }
pmic 0:86129f1b4a93 24
pmic 4:9c003c402033 25 void Servo::EndPulse()
pmic 4:9c003c402033 26 {
pmic 4:9c003c402033 27 ServoPin = 0;
pmic 4:9c003c402033 28 }
pmic 0:86129f1b4a93 29
pmic 10:fe74e8909d3f 30 /**
pmic 16:69911e81dfd4 31 * Enables the servo with start angle and period.
pmic 16:69911e81dfd4 32 * @_StartInput a value between 0...1.
pmic 16:69911e81dfd4 33 * @_Period period in mus.
pmic 10:fe74e8909d3f 34 */
pmic 16:69911e81dfd4 35 void Servo::Enable(float _StartInput, int _Period)
pmic 4:9c003c402033 36 {
pmic 18:21c0a669d6ef 37 if (_StartInput < MIN_INPUT) _StartInput = MIN_INPUT;
pmic 18:21c0a669d6ef 38 if (_StartInput > MAX_INPUT) _StartInput = MAX_INPUT;
pmic 16:69911e81dfd4 39 Period = _Period;
pmic 16:69911e81dfd4 40 Position = static_cast<int>(_StartInput * static_cast<float>(Period));
pmic 10:fe74e8909d3f 41 Pulse.attach(callback(this, &Servo::StartPulse), std::chrono::microseconds{static_cast<long int>(Period)});
pmic 4:9c003c402033 42 }
pmic 0:86129f1b4a93 43
pmic 10:fe74e8909d3f 44 /**
pmic 10:fe74e8909d3f 45 * Disables the servo.
pmic 10:fe74e8909d3f 46 */
pmic 4:9c003c402033 47 void Servo::Disable()
pmic 4:9c003c402033 48 {
pmic 4:9c003c402033 49 Pulse.detach();
pmic 4:9c003c402033 50 }