Skyward boardcore
Loading...
Searching...
No Matches
Servo.cpp
Go to the documentation of this file.
1/* Copyright (c) 2019 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "Servo.h"
24
26
27#include "miosix.h"
28
29namespace Boardcore
30{
31
32#ifndef COMPILE_FOR_HOST
33
34Servo::Servo(TIM_TypeDef* const timer, TimerUtils::Channel pwmChannel,
35 unsigned int minPulse, unsigned int maxPulse,
36 unsigned int frequency)
37 : pwm(timer, frequency), pwmChannel(pwmChannel), minPulse(minPulse),
38 maxPulse(maxPulse), frequency(frequency)
39{
40 setPosition(0);
41}
42
43void Servo::enable() { pwm.enableChannel(pwmChannel); }
44
45void Servo::disable() { pwm.disableChannel(pwmChannel); }
46
47#else
48
49Servo::Servo(unsigned int minPulse, unsigned int maxPulse,
50 unsigned int frequency)
51 : minPulse(minPulse), maxPulse(maxPulse), frequency(frequency)
52{
53 setPosition(0);
54}
55
56void Servo::enable() {}
57
58void Servo::disable() {}
59
60#endif
61
62void Servo::setPosition(float position, bool limited)
63{
64 if (limited)
65 {
66 if (position < 0)
67 position = 0;
68 else if (position > 1)
69 position = 1;
70 }
71
72 float pulse = minPulse + (maxPulse - minPulse) * position;
73
74 float dutyCycle = pulse * frequency / 1000000.0f;
75
76#ifndef COMPILE_FOR_HOST
77 pwm.setDutyCycle(pwmChannel, dutyCycle);
78#else
79 this->dutyCycle = dutyCycle;
80#endif
81}
82
83void Servo::setPosition90Deg(float degrees) { setPosition(degrees / 90); }
84
85void Servo::setPosition120Deg(float degrees) { setPosition(degrees / 120); }
86
87void Servo::setPosition180Deg(float degrees) { setPosition(degrees / 180); }
88
89void Servo::setPosition360Deg(float degrees) { setPosition(degrees / 360); }
90
92{
93#ifndef COMPILE_FOR_HOST
94 float dutyCycle = pwm.getDutyCycle(pwmChannel);
95#else
96 float dutyCycle = this->dutyCycle;
97#endif
98
99 float pulse = dutyCycle * 1000000.0f / frequency;
100
101 return (pulse - minPulse) / (maxPulse - minPulse);
102}
103
104float Servo::getPosition90Deg() { return getPosition() * 90; }
105
106float Servo::getPosition180Deg() { return getPosition() * 180; }
107
108float Servo::getPosition360Deg() { return getPosition() * 360; }
109
111{
113
114#ifndef COMPILE_FOR_HOST
115 pwm.getTimer().getTimerNumber(), static_cast<uint8_t>(pwmChannel),
116#else
117 0, 0,
118#endif
119 getPosition()};
120}
121
122} // namespace Boardcore
void disableChannel(TimerUtils::Channel channel)
Definition PWM.cpp:68
void setDutyCycle(TimerUtils::Channel channel, float dutyCycle)
Sets the duty cycle for the specified channel.
Definition PWM.cpp:78
float getDutyCycle(TimerUtils::Channel channel)
Return the channel's duty cycle in the range [0,1].
Definition PWM.cpp:89
void enableChannel(TimerUtils::Channel channel, Polarity polarity=Polarity::NORMAL)
Definition PWM.cpp:56
GP16bitTimer & getTimer()
Returns the timer used to generate the pwm signal.
Definition PWM.cpp:95
ServoData getState()
Returns the current position and the current timestamp.
Definition Servo.cpp:110
float getPosition90Deg()
Definition Servo.cpp:104
void setPosition360Deg(float degrees)
Definition Servo.cpp:89
void setPosition90Deg(float degrees)
Definition Servo.cpp:83
void enable()
Starts producing the PWM signal.
Definition Servo.cpp:43
void setPosition180Deg(float degrees)
Definition Servo.cpp:87
float getPosition180Deg()
Definition Servo.cpp:106
void disable()
Stops producing the PWM signal.
Definition Servo.cpp:45
Servo(TIM_TypeDef *const timer, TimerUtils::Channel pwmChannel, unsigned int minPulse=1000, unsigned int maxPulse=2000, unsigned int frequency=50)
Prepare the timer and sets the PWM output to the minimum.
Definition Servo.cpp:34
void setPosition(float position, bool limited=true)
Set the position of the servomotor.
Definition Servo.cpp:62
float getPosition()
Returns the current position of the servomotor.
Definition Servo.cpp:91
void setPosition120Deg(float degrees)
Definition Servo.cpp:85
float getPosition360Deg()
Definition Servo.cpp:108
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.