54 template<
typename TIMER_, board::PWMPin PWMPIN_>
class Servo
64 using TYPE =
typename TIMER::TYPE;
67 using CALC =
typename TIMER::CALCULATOR;
68 using TPRESCALER =
typename CALC::PRESCALER;
69 static constexpr const TPRESCALER PRESCALER = TIMER::PRESCALER;
92 Servo(
TIMER&
timer, uint16_t us_minimum, uint16_t us_maximum, uint16_t us_neutral = 0)
93 : out_{
timer}, US_MINIMUM_{us_minimum}, US_MAXIMUM_{us_maximum},
94 US_NEUTRAL_{us_neutral ? us_neutral : ((us_maximum + us_minimum) / 2)},
95 COUNTER_MINIMUM_{counter(US_MINIMUM_)},
96 COUNTER_MAXIMUM_{counter(US_MAXIMUM_)}, COUNTER_NEUTRAL_{counter(US_NEUTRAL_)}
157 (angle >= 0 ?
utils::map(int32_t(angle), 0L, int32_t(MAX), COUNTER_NEUTRAL_, COUNTER_MAXIMUM_) :
158 utils::map(int32_t(angle), int32_t(MIN), 0L, COUNTER_MINIMUM_, COUNTER_NEUTRAL_));
180 static constexpr TYPE counter(uint16_t pulse_us)
182 return CALC::PulseTimer_value(PRESCALER, pulse_us);
185 static const int8_t MAX = +90;
186 static const int8_t MIN = -90;
190 const uint16_t US_MINIMUM_;
191 const uint16_t US_MAXIMUM_;
192 const uint16_t US_NEUTRAL_;
193 const TYPE COUNTER_MINIMUM_;
194 const TYPE COUNTER_MAXIMUM_;
195 const TYPE COUNTER_NEUTRAL_;
Construct a new handler for a PWM output pin.
void set_duty(TYPE duty)
Set the duty cycle for this PWM pin, from 0 (0% duty cycle) to MAX (100%), any value above MAX will b...
This template class supports one servomotor connected to a PWM pin.
void set_pulse(uint16_t pulse_us)
Set the pulse width in microsecond, hence the servo angle.
constexpr TYPE calculate_counter(uint16_t pulse_us) const
Calculate the counter value to use with set_counter() in order to generate a pulse of the given width...
void set_counter(TYPE value) INLINE
Set the Timer counter that will change the pulse width, hence the servo angle.
TIMER_ TIMER
The type of timer used to handle the connected servomotor.
typename TIMER::TYPE TYPE
The type of counter for TIMER_.
static constexpr const board::PWMPin PWMPIN
The PWM pin for this PWMOutput.
void rotate(int8_t angle)
Rotate the servomotor at the given angle position.
Servo(TIMER &timer, uint16_t us_minimum, uint16_t us_maximum, uint16_t us_neutral=0)
Create a new servo handler, based on the provided timer (which will provide the frequency for pulse g...
void detach() INLINE
Detach the servomotor from this handler.
static constexpr const board::DigitalPin PIN
The pin to which the servomotor is connected.
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
PWMPin
Defines all digital output pins of target MCU, capable of PWM output.
DigitalPin
Defines all available digital input/output pins of the target MCU.
Defines the API for servomotor support.
Defines all API for all external devices supported by FastArduino.
Defines all API to manipulate AVR Timers.
constexpr TO map(TI value, TI input_min, TI input_max, TO output_min, TO output_max)
Linearly transform value from range [input_min ; input_max] to range [output_min ; output_max].
constexpr T constrain(T value, T min, T max)
Constrain value to be greater than or equal to min and lower than or equal to max.
General utilities API that have broad application in programs.