Skyward boardcore
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Servo.h
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#ifndef COMPILE_FOR_HOST
24#include <drivers/timer/PWM.h>
25#endif
26
27#include "ServoData.h"
28
29#pragma once
30
31namespace Boardcore
32{
33
58class Servo
59{
60public:
61#ifndef COMPILE_FOR_HOST
78 explicit Servo(TIM_TypeDef* const timer, TimerUtils::Channel pwmChannel,
79 unsigned int minPulse = 1000, unsigned int maxPulse = 2000,
80 unsigned int frequency = 50);
81#else
82 explicit Servo(unsigned int minPulse = 1000, unsigned int maxPulse = 2000,
83 unsigned int frequency = 50);
84#endif
85
89 void enable();
90
94 void disable();
95
101 void setPosition(float position, bool limited = true);
102
103 void setPosition90Deg(float degrees);
104
105 void setPosition120Deg(float degrees);
106
107 void setPosition180Deg(float degrees);
108
109 void setPosition360Deg(float degrees);
110
119 float getPosition();
120
121 float getPosition90Deg();
122
123 float getPosition180Deg();
124
125 float getPosition360Deg();
126
131
132private:
133 // This class is not copyable!
134 Servo& operator=(const Servo&) = delete;
135 Servo(const Servo& s) = delete;
136
137#ifndef COMPILE_FOR_HOST
138 PWM pwm;
139 TimerUtils::Channel pwmChannel;
140#else
141 float dutyCycle;
142#endif
143
144 float minPulse;
145 float maxPulse;
146 float frequency;
147};
148
149} // namespace Boardcore
Driver for easy access to the PWM capabilities of the general purpose timers.
Definition PWM.h:52
Driver to operate a PWM controlled servo motor.
Definition Servo.h:59
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
This file includes all the types the logdecoder script will decode.