FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
servo.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef SERVO_H
22#define SERVO_H
23
25#include <fastarduino/pwm.h>
27
28namespace devices
29{
33 namespace servo
34 {
35 }
36}
37
38namespace devices::servo
39{
54 template<typename TIMER_, board::PWMPin PWMPIN_> class Servo
55 {
56 public:
58 using TIMER = TIMER_;
60 static constexpr const board::PWMPin PWMPIN = PWMPIN_;
62 static constexpr const board::DigitalPin PIN = board_traits::PWMPin_trait<PWMPIN>::ACTUAL_PIN;
64 using TYPE = typename TIMER::TYPE;
65
66 private:
67 using CALC = typename TIMER::CALCULATOR;
68 using TPRESCALER = typename CALC::PRESCALER;
69 static constexpr const TPRESCALER PRESCALER = TIMER::PRESCALER;
70
71 public:
73 Servo(const Servo&) = delete;
74 Servo& operator=(const Servo&) = delete;
76
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_)}
97 {
98 }
99
106 {
107 out_.set_duty(0);
108 }
109
124 {
125 out_.set_duty(utils::constrain(value, COUNTER_MINIMUM_, COUNTER_MAXIMUM_));
126 }
127
138 void set_pulse(uint16_t pulse_us)
139 {
140 // Constrain pulse to min/max and convert pulse to timer counter value
141 out_.set_duty(calculate_counter(pulse_us));
142 }
143
153 void rotate(int8_t angle)
154 {
155 angle = utils::constrain(angle, MIN, MAX);
156 TYPE count =
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_));
159 out_.set_duty(count);
160 }
161
174 constexpr TYPE calculate_counter(uint16_t pulse_us) const
175 {
176 return counter(utils::constrain(pulse_us, US_MINIMUM_, US_MAXIMUM_));
177 }
178
179 private:
180 static constexpr TYPE counter(uint16_t pulse_us)
181 {
182 return CALC::PulseTimer_value(PRESCALER, pulse_us);
183 }
184
185 static const int8_t MAX = +90;
186 static const int8_t MIN = -90;
187
189
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_;
196 };
197}
198
199#endif /* SERVO_H */
Construct a new handler for a PWM output pin.
Definition: pwm.h:54
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...
Definition: pwm.h:135
This template class supports one servomotor connected to a PWM pin.
Definition: servo.h:55
void set_pulse(uint16_t pulse_us)
Set the pulse width in microsecond, hence the servo angle.
Definition: servo.h:138
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...
Definition: servo.h:174
void set_counter(TYPE value) INLINE
Set the Timer counter that will change the pulse width, hence the servo angle.
Definition: servo.h:123
TIMER_ TIMER
The type of timer used to handle the connected servomotor.
Definition: servo.h:58
typename TIMER::TYPE TYPE
The type of counter for TIMER_.
Definition: servo.h:64
static constexpr const board::PWMPin PWMPIN
The PWM pin for this PWMOutput.
Definition: servo.h:60
void rotate(int8_t angle)
Rotate the servomotor at the given angle position.
Definition: servo.h:153
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...
Definition: servo.h:92
void detach() INLINE
Detach the servomotor from this handler.
Definition: servo.h:105
static constexpr const board::DigitalPin PIN
The pin to which the servomotor is connected.
Definition: servo.h:62
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
PWMPin
Defines all digital output pins of target MCU, capable of PWM output.
Definition: empty.h:84
DigitalPin
Defines all available digital input/output pins of the target MCU.
Definition: empty.h:56
Defines the API for servomotor support.
Definition: servo.h:34
Defines all API for all external devices supported by FastArduino.
Defines all API to manipulate AVR Timers.
Definition: pulse_timer.h:116
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].
Definition: utilities.h:89
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.
Definition: utilities.h:62
PulseTimer API.
PWM API.
General utilities API that have broad application in programs.