Smartcar Shield
Loading...
Searching...
No Matches
ServoMotor.cpp
Go to the documentation of this file.
1#include "ServoMotor.hpp"
2#include "../../../utilities/Utilities.hpp"
3
4using namespace smartcarlib::utils;
6
7ServoMotor::ServoMotor(uint8_t controlPin,
8 int minPulseLength,
9 int idlePulseLength,
10 int maxPulseLength)
11 : kControlPin{ controlPin }
12 , kMinPulseLength{ minPulseLength }
13 , kIdlePulseLength{ idlePulseLength }
14 , kMaxPulseLength{ maxPulseLength }
15{
16}
17
18void ServoMotor::setSpeed(int speed)
19{
20 if (!mAttached)
21 {
22 Servo::attach(kControlPin, kMinPulseLength, kMaxPulseLength);
23 mAttached = true;
24 }
25
27 // The pulses duration might not be symmetrically distributed around the
28 // idle speed so we need to split the mapping between the positive and the
29 // negative values
30 if (speed < kIdleMotorSpeed)
31 {
32 Servo::writeMicroseconds(
33 getMap(speed, kMinMotorSpeed, kIdleMotorSpeed, kMinPulseLength, kIdlePulseLength));
34 }
35 else
36 {
37 Servo::writeMicroseconds(
38 getMap(speed, kIdleMotorSpeed, kMaxMotorSpeed, kIdlePulseLength, kMaxPulseLength));
39 }
40}
void setSpeed(int speed) override
Sets the motor speed and direction as the percentage of the maximum possible speed,...
ServoMotor(uint8_t controlPin, int minPulseLength=smartcarlib::constants::servomotor::kDefaultMinPulseLength, int idlePulseLength=smartcarlib::constants::servomotor::kDefaultIdlePulseLength, int maxPulseLength=smartcarlib::constants::servomotor::kDefaultMaxPulseLength)
Constructs a servo motor.
Definition ServoMotor.cpp:7
constexpr AnyNumber getMap(AnyNumber valueToMap, AnyNumber fromLow, AnyNumber fromHigh, AnyNumber toLow, AnyNumber toHigh)
Maps a value from a range to another.
constexpr AnyNumber getConstrain(AnyNumber number, AnyNumber min, AnyNumber max)
Limit the number between a range.
Definition Utilities.hpp:46