ESP32Servo
ESP32Servo.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2017 John K. Bennett. All right reserved.
3 
4  ESP32_Servo.h - Servo library for ESP32 - Version 1
5 
6  Original Servo.h written by Michael Margolis in 2009
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /*
24  A servo is activated by creating an instance of the Servo class, and passing
25  the desired GPIO pin to the attach() method.
26  The servos are pulsed in the background using the value most recently
27  written using the write() method.
28 
29  The class methods are:
30 
31  Servo - Class for manipulating servo motors connected to ESP32 pins.
32  int attach(pin ) - Attaches the given GPIO pin to the next free channel
33  (channels that have previously been detached are used first),
34  returns channel number or 0 if failure. All pin numbers are allowed,
35  but only pins 2,4,12-19,21-23,25-27,32-33 are recommended.
36  int attach(pin, min, max ) - Attaches to a pin setting min and max
37  values in microseconds; enforced minimum min is 500, enforced max
38  is 2500. Other semantics same as attach().
39  void write () - Sets the servo angle in degrees; a value below 500 is
40  treated as a value in degrees (0 to 180). These limit are enforced,
41  i.e., values are treated as follows:
42  Value Becomes
43  ----- -------
44  < 0 0
45  0 - 180 value (treated as degrees)
46  181 - 499 180
47  500 - (min-1) min
48  min-max (from attach or default) value (treated as microseconds)
49  (max+1) - 2500 max
50 
51  void writeMicroseconds() - Sets the servo pulse width in microseconds.
52  min and max are enforced (see above).
53  int read() - Gets the last written servo pulse width as an angle between 0 and 180.
54  int readMicroseconds() - Gets the last written servo pulse width in microseconds.
55  bool attached() - Returns true if this servo instance is attached.
56  void detach() - Stops an the attached servo, frees its attached pin, and frees
57  its channel for reuse).
58 
59  *** ESP32-specific functions **
60  setTimerWidth(value) - Sets the PWM timer width (must be 16-20) (ESP32 ONLY);
61  as a side effect, the pulse width is recomputed.
62  int readTimerWidth() - Gets the PWM timer width (ESP32 ONLY)
63  */
64 
65 #ifndef ESP32_Servo_h
66 #define ESP32_Servo_h
67 #include "analogWrite.h"
68 #include "ESP32PWM.h"
69 #include "ESP32Tone.h"
70 //Enforce only using PWM pins on the ESP32
71 #define ENFORCE_PINS
72 // Default Arduino Servo.h
73 #define DEFAULT_uS_LOW 544
74 #define DEFAULT_uS_HIGH 2400
75 
76 // Values for TowerPro MG995 large servos (and many other hobbyist servos)
77 //#define DEFAULT_uS_LOW 1000 // 1000us
78 //#define DEFAULT_uS_HIGH 2000 // 2000us
79 
80 // Values for TowerPro SG90 small servos
81 //#define DEFAULT_uS_LOW 400
82 //#define DEFAULT_uS_HIGH 2400
83 
84 #define DEFAULT_TIMER_WIDTH 16
85 #define DEFAULT_TIMER_WIDTH_TICKS 65536
86 
87 #define ESP32_Servo_VERSION 1 // software version of this library
88 
89 #define MIN_PULSE_WIDTH 500 // the shortest pulse sent to a servo
90 #define MAX_PULSE_WIDTH 2500 // the longest pulse sent to a servo
91 #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
92 #define DEFAULT_PULSE_WIDTH_TICKS 4825
93 //#define REFRESH_CPS 50
94 #define REFRESH_USEC 20000
95 
96 #define MAX_SERVOS 16 // no. of PWM channels in ESP32
97 
98 /*
99  * This group/channel/timmer mapping is for information only;
100  * the details are handled by lower-level code
101  *
102  * LEDC Chan to Group/Channel/Timer Mapping
103  ** ledc: 0 => Group: 0, Channel: 0, Timer: 0
104  ** ledc: 1 => Group: 0, Channel: 1, Timer: 0
105  ** ledc: 2 => Group: 0, Channel: 2, Timer: 1
106  ** ledc: 3 => Group: 0, Channel: 3, Timer: 1
107  ** ledc: 4 => Group: 0, Channel: 4, Timer: 2
108  ** ledc: 5 => Group: 0, Channel: 5, Timer: 2
109  ** ledc: 6 => Group: 0, Channel: 6, Timer: 3
110  ** ledc: 7 => Group: 0, Channel: 7, Timer: 3
111  ** ledc: 8 => Group: 1, Channel: 0, Timer: 0
112  ** ledc: 9 => Group: 1, Channel: 1, Timer: 0
113  ** ledc: 10 => Group: 1, Channel: 2, Timer: 1
114  ** ledc: 11 => Group: 1, Channel: 3, Timer: 1
115  ** ledc: 12 => Group: 1, Channel: 4, Timer: 2
116  ** ledc: 13 => Group: 1, Channel: 5, Timer: 2
117  ** ledc: 14 => Group: 1, Channel: 6, Timer: 3
118  ** ledc: 15 => Group: 1, Channel: 7, Timer: 3
119  */
120 
121 class Servo {
122 
123 public:
124  Servo();
125  // Arduino Servo Library calls
126  int attach(int pin); // attach the given pin to the next free channel, returns channel number or 0 if failure
127  int attach(int pin, int min, int max); // as above but also sets min and max values for writes.
128  void detach();
129  void write(int value); // if value is < MIN_PULSE_WIDTH its treated as an angle, otherwise as pulse width in microseconds
130  void writeMicroseconds(int value); // Write pulse width in microseconds
131  int read(); // returns current pulse width as an angle between 0 and 180 degrees
132  int readMicroseconds(); // returns current pulse width in microseconds for this servo
133  bool attached(); // return true if this servo is attached, otherwise false
134 
135  // ESP32 only functions
136  void setTimerWidth(int value); // set the PWM timer width (ESP32 ONLY)
137  int readTimerWidth(); // get the PWM timer width (ESP32 ONLY)
138  void setPeriodHertz(int hertz){
139  REFRESH_CPS=hertz;
140  setTimerWidth(this->timer_width);
141  }
142 private:
143  int usToTicks(int usec);
144  int ticksToUs(int ticks);
145 // static int ServoCount; // the total number of attached servos
146 // static int ChannelUsed[]; // used to track whether a channel is in service
147 // int servoChannel = 0; // channel number for this servo
148 
149  int min = DEFAULT_uS_LOW; // minimum pulse width for this servo
150  int max = DEFAULT_uS_HIGH; // maximum pulse width for this servo
151  int pinNumber = 0; // GPIO pin assigned to this channel
152  int timer_width = DEFAULT_TIMER_WIDTH; // ESP32 allows variable width PWM timers
153  int ticks = DEFAULT_PULSE_WIDTH_TICKS; // current pulse width on this channel
154  int timer_width_ticks = DEFAULT_TIMER_WIDTH_TICKS; // no. of ticks at rollover; varies with width
155  ESP32PWM * getPwm(); // get the PWM object
157  int REFRESH_CPS = 50;
158 
159 };
160 #endif
#define DEFAULT_TIMER_WIDTH
Definition: ESP32Servo.h:84
#define DEFAULT_PULSE_WIDTH_TICKS
Definition: ESP32Servo.h:92
void write(int value)
Definition: ESP32Servo.cpp:131
int max
Definition: ESP32Servo.h:150
void setPeriodHertz(int hertz)
Definition: ESP32Servo.h:138
int ticks
Definition: ESP32Servo.h:153
ESP32PWM pwm
Definition: ESP32Servo.h:156
bool attached()
Definition: ESP32Servo.cpp:183
int read()
Definition: ESP32Servo.cpp:163
int readTimerWidth()
Definition: ESP32Servo.cpp:221
#define DEFAULT_TIMER_WIDTH_TICKS
Definition: ESP32Servo.h:85
int REFRESH_CPS
Definition: ESP32Servo.h:157
#define DEFAULT_uS_LOW
Definition: ESP32Servo.h:73
int pinNumber
Definition: ESP32Servo.h:151
int timer_width_ticks
Definition: ESP32Servo.h:154
ESP32PWM * getPwm()
Definition: ESP32Servo.cpp:68
void detach()
Definition: ESP32Servo.cpp:120
int timer_width
Definition: ESP32Servo.h:152
int attach(int pin)
Definition: ESP32Servo.cpp:73
void writeMicroseconds(int value)
Definition: ESP32Servo.cpp:146
void setTimerWidth(int value)
Definition: ESP32Servo.cpp:188
int min
Definition: ESP32Servo.h:149
int pin
Definition: ToneExample.ino:7
#define DEFAULT_uS_HIGH
Definition: ESP32Servo.h:74
int ticksToUs(int ticks)
Definition: ESP32Servo.cpp:231
int readMicroseconds()
Definition: ESP32Servo.cpp:168
int usToTicks(int usec)
Definition: ESP32Servo.cpp:226