FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
devices::servo::Servo< TIMER_, PWMPIN_ > Class Template Reference

This template class supports one servomotor connected to a PWM pin. More...

#include <fastarduino/devices/servo.h>

Public Types

using TIMER = TIMER_
 The type of timer used to handle the connected servomotor. More...
 
using TYPE = typename TIMER::TYPE
 The type of counter for TIMER_. More...
 

Public Member Functions

 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 generation), and the additional parameters for pulse width. More...
 
void detach () INLINE
 Detach the servomotor from this handler. More...
 
void set_counter (TYPE value) INLINE
 Set the Timer counter that will change the pulse width, hence the servo angle. More...
 
void set_pulse (uint16_t pulse_us)
 Set the pulse width in microsecond, hence the servo angle. More...
 
void rotate (int8_t angle)
 Rotate the servomotor at the given angle position. More...
 
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. More...
 

Static Public Attributes

static constexpr const board::PWMPin PWMPIN = PWMPIN_
 The PWM pin for this PWMOutput. More...
 
static constexpr const board::DigitalPin PIN = board_traits::PWMPin_trait<PWMPIN>::ACTUAL_PIN
 The pin to which the servomotor is connected. More...
 

Detailed Description

template<typename TIMER_, board::PWMPin PWMPIN_>
class devices::servo::Servo< TIMER_, PWMPIN_ >

This template class supports one servomotor connected to a PWM pin.

Servomotors are driven by the width of pulses generated at a specific frequency (specific to each model, but typically around 50Hz, i.e. one pulse every 20ms), a specific width matches a specific rotation angle of the servo.

Template Parameters
TIMER_the type of timer used to handle the connected servomotor; this must be a timer::PulseTimer type.
PWMPIN_the board::PWMPin to which the servomotor is connected; this must be a PWM pin, connected to TIMER_.
See also
timer::PulseTimer

Definition at line 54 of file servo.h.

Member Typedef Documentation

◆ TIMER

template<typename TIMER_ , board::PWMPin PWMPIN_>
using devices::servo::Servo< TIMER_, PWMPIN_ >::TIMER = TIMER_

The type of timer used to handle the connected servomotor.

Definition at line 58 of file servo.h.

◆ TYPE

template<typename TIMER_ , board::PWMPin PWMPIN_>
using devices::servo::Servo< TIMER_, PWMPIN_ >::TYPE = typename TIMER::TYPE

The type of counter for TIMER_.

Definition at line 64 of file servo.h.

Constructor & Destructor Documentation

◆ Servo()

template<typename TIMER_ , board::PWMPin PWMPIN_>
devices::servo::Servo< TIMER_, PWMPIN_ >::Servo ( TIMER timer,
uint16_t  us_minimum,
uint16_t  us_maximum,
uint16_t  us_neutral = 0 
)
inline

Create a new servo handler, based on the provided timer (which will provide the frequency for pulse generation), and the additional parameters for pulse width.

Parameters
timerthe timer::PulseTimer that will handle pulse generation for this servomotor
us_minimumthe minimal pulse width in microseconds; this matches the minimal angle of the servo.
us_maximumthe maximal pulse width in microseconds: this matches the maximal angle of the servo.
us_neutralthe pulse width, in microseconds, that matches the 0 angle; when not provided (or 0), it will be calculated as the average of us_minimum and us_maximum.

Definition at line 92 of file servo.h.

Member Function Documentation

◆ detach()

template<typename TIMER_ , board::PWMPin PWMPIN_>
void devices::servo::Servo< TIMER_, PWMPIN_ >::detach ( )
inline

Detach the servomotor from this handler.

Concretely, this means that no pulse will be generated at all, i.e. the servo will not be able to hold its position anymore.

Definition at line 105 of file servo.h.

◆ set_counter()

template<typename TIMER_ , board::PWMPin PWMPIN_>
void devices::servo::Servo< TIMER_, PWMPIN_ >::set_counter ( TYPE  value)
inline

Set the Timer counter that will change the pulse width, hence the servo angle.

This method is the most optimized way to change the servo angle. However, it requires preliminary calculation of counter values from the desired angles. Calculation can be performed by calculate_counter().

Parameters
valuethe new counter value to use for fixing pulse width
See also
calculate_counter()
set_pulse()
rotate()

Definition at line 123 of file servo.h.

◆ set_pulse()

template<typename TIMER_ , board::PWMPin PWMPIN_>
void devices::servo::Servo< TIMER_, PWMPIN_ >::set_pulse ( uint16_t  pulse_us)
inline

Set the pulse width in microsecond, hence the servo angle.

This method is less optimized way than set_counter() to change the servo angle.

Parameters
pulse_usthe new pulse width in microseconds
See also
set_counter()
rotate()

Definition at line 138 of file servo.h.

◆ rotate()

template<typename TIMER_ , board::PWMPin PWMPIN_>
void devices::servo::Servo< TIMER_, PWMPIN_ >::rotate ( int8_t  angle)
inline

Rotate the servomotor at the given angle position.

This method is less optimized than set_counter() and more efficient than set_pulse(), but it provides a more friendly API as it uses real physical parameters.

Parameters
anglethe new angle, in degrees, to rotate the servo to; it must be between -90 and +90.

Definition at line 153 of file servo.h.

◆ calculate_counter()

template<typename TIMER_ , board::PWMPin PWMPIN_>
constexpr TYPE devices::servo::Servo< TIMER_, PWMPIN_ >::calculate_counter ( uint16_t  pulse_us) const
inlineconstexpr

Calculate the counter value to use with set_counter() in order to generate a pulse of the given width.

This method is constexpr hence it can be evaluated at compile-time if provided with a constant value.

Parameters
pulse_usthe pulse width, in microseconds, for which to compute the counter value
Returns
the counter value to use for a pulse_us width
See also
set_counter()

Definition at line 174 of file servo.h.

Member Data Documentation

◆ PWMPIN

template<typename TIMER_ , board::PWMPin PWMPIN_>
constexpr const board::PWMPin devices::servo::Servo< TIMER_, PWMPIN_ >::PWMPIN = PWMPIN_
staticconstexpr

The PWM pin for this PWMOutput.

Definition at line 60 of file servo.h.

◆ PIN

template<typename TIMER_ , board::PWMPin PWMPIN_>
constexpr const board::DigitalPin devices::servo::Servo< TIMER_, PWMPIN_ >::PIN = board_traits::PWMPin_trait<PWMPIN>::ACTUAL_PIN
staticconstexpr

The pin to which the servomotor is connected.

Definition at line 62 of file servo.h.


The documentation for this class was generated from the following file: