Developer World
Spresense Arduino Library v3.2.0-77d75a4
Servo.h
1/*
2 * Servo.h - Servo Header file for the Spresense SDK
3 * Copyright 2018 Sony Semiconductor Solutions Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef Servo_h
21#define Servo_h
22
29// #ifdef __cplusplus
30
31#include <stdbool.h>
32#include <stdint.h>
33
34// Pin number of unattached pins
35#define SERVO_NOT_ATTACHED (0xFF)
36
37// Default min/max pulse widths (in microseconds) and angles (in degrees).
38#define SERVO_MIN_ANGLE (0)
39#define SERVO_MAX_ANGLE (180)
40
41#define SERVO_MIN_PULSE_WIDTH (544) // the shortest pulse sent to a servo
42#define SERVO_MAX_PULSE_WIDTH (2400) // the longest pulse sent to a servo
43
44#define SERVO_DEFAULT_PULSE_WIDTH (1500) // default pulse width when servo is attached
45#define SERVO_REFRESH_INTERVAL (2500) // minumim time to refresh servos in microseconds
46#define SERVO_REFRESH_FREQUENCY (1000000L / SERVO_REFRESH_INTERVAL)
47
49class Servo
50{
51public:
58
65
100 bool attach(uint8_t pin,
101 uint16_t min_pulse_width = SERVO_MIN_PULSE_WIDTH,
102 uint16_t max_pulse_width = SERVO_MAX_PULSE_WIDTH,
103 uint16_t min_angle = SERVO_MIN_ANGLE,
104 uint16_t max_angle = SERVO_MAX_ANGLE);
105
113 bool detach();
114
124 void write(uint16_t angle);
125
136 void writeMicroseconds(uint16_t pulse_width);
137
144 uint16_t read() const;
145
151 bool attached() const { return this->pin_ != SERVO_NOT_ATTACHED; }
152
159 uint8_t attachedPin() const { return this->pin_; }
160
161private:
162 uint8_t pin_;
163 uint16_t min_pw_;
164 uint16_t max_pw_;
165 uint16_t min_angle_;
166 uint16_t max_angle_;
167 uint16_t angle_;
168};
169
170// #endif // __cplusplus
171
174#endif //Servo_h
Definition: Servo.h:50
bool attached() const
Check if this instance is attached to a servo.
Definition: Servo.h:151
bool attach(uint8_t pin, uint16_t min_pulse_width=SERVO_MIN_PULSE_WIDTH, uint16_t max_pulse_width=SERVO_MAX_PULSE_WIDTH, uint16_t min_angle=SERVO_MIN_ANGLE, uint16_t max_angle=SERVO_MAX_ANGLE)
Associate this instance with a servomotor whose input is connected to pin.
Servo()
Construct a new Servo instance.
uint16_t read() const
uint8_t attachedPin() const
Get the pin this instance is attached to.
Definition: Servo.h:159
void write(uint16_t angle)
Set the servomotor target angle.
~Servo()
Destructor a Servo instance.
void writeMicroseconds(uint16_t pulse_width)
Set the pulse width, in microseconds.
bool detach()
Stop driving the servo pulse train.