ESP32Servo.h 6.7 KB

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