Code Monkey home page Code Monkey logo

esp32servo's Introduction

Servo Library for ESP32

This library attempts to faithfully replicate the semantics of the Arduino Servo library (see http://www.arduino.cc/en/Reference/Servo) for the ESP32, with two (optional) additions. The two new functions expose the ability of the ESP32 PWM timers to vary timer width.

License

Copyright (c) 2017 John K. Bennett. All right reserved.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Library Description:

Servo - Class for manipulating servo motors connected to ESP32 pins.
int attach(pin )  - Attaches the given GPIO pin to the next free channel
    (channels that have previously been detached are used first),
    returns channel number or 0 if failure. All pin numbers are allowed,
    but only pins 2,4,12-19,21-23,25-27,32-33 are recommended.
int attach(pin, min, max  ) - Attaches to a pin setting min and max
    values in microseconds; enforced minimum min is 500, enforced max
    is 2500. Other semantics are the same as attach().
void write () - Sets the servo angle in degrees; a value below 500 is
    treated as a value in degrees (0 to 180). These limit are enforced,
    i.e., values are constrained as follows:
        Value                                   Becomes
        -----                                   -------
        < 0                                        0
        0 - 180                                 value (treated as degrees)
        181 - 499                                 180
        500 - (min-1)                             min
        min-max (from attach or default)   value (treated as microseconds)
        (max+1) - 2500                            max
void writeMicroseconds() - Sets the servo pulse width in microseconds.
    min and max are enforced (see above).
int read() - Gets the last written servo pulse width as an angle between 0 and 180.
int readMicroseconds()   - Gets the last written servo pulse width in microseconds.
bool attached() - Returns true if this servo instance is attached to a pin.
void detach() - Stops an the attached servo, frees the attached pin, and frees
    its channel for reuse.
  • New ESP32-specific functions ** setTimerWidth(value) - Sets the PWM timer width (must be 16-20) (ESP32 ONLY); as a side effect, the pulse width is recomputed. int readTimerWidth() - Gets the PWM timer width (ESP32 ONLY)

Useful Defaults:

default min pulse width for attach(): 1000us default max pulse width for attach(): 2000us default timer width 16 (if timer width is not set) default pulse width 1500us (servos are initialized with this value) MINIMUM pulse with: 500us MAXIMUM pulse with: 2500us MAXIMUM number of servos: 16 (this is the number of PWM channels in the ESP32)

esp32servo's People

Contributors

jkb-git avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

esp32servo's Issues

Start the 1st servo on the 9th pwm channel

Hi great work! I´m using 8 ledc functions for other purpose, how can i make your library to start on 9th pwm channel so i don´t have conlfict with my ledc functions.

thx

Buggy?

Hi, thank you for this library ;-) I see the following issues:

  1. function setTimerWidth (line 230):

The following block seems to have no effect (the variable "this->ticks" will never be changed) ... why is it there? What if widthDifference is negative ... do you really want to make such a bit shift?

if (widthDifference > 0)
{
    this->ticks << widthDifference;
}
else
{
    this->ticks >> widthDifference;
}
  1. Function "int Servo::attach(int pin, int min, int max)" ... there is a return value in case the first condition is ok ("if ((this->servoChannel <= MAX_SERVOS) && (this->servoChannel > 0))").

Bye,
Filip

Not compatible with esp32 3.0.0

I tried compiling it with latest version, it starts complaining quite a few thing such as

Servo.h:68:54: error: 'SOC_LEDC_TIMER_BIT_WIDE_NUM' was not declared in this scope; did you mean 'SOC_LEDC_TIMER_BIT_WIDTH'?
...

if I switch back to esp32 2.0.17, it works fine.

Library not compatible with more than 2 servos

Problem

  • Device: ESP32-S3-Wroom-1

When we try to command more than two servos, only the two servo motors function as expected. The rest does not move at all. All servos individually run if only one servo is used in the code.
The example is:

servo0.attach(pin0);   
servo1.attach(pin1);
servo2.attach(pin2);   // This servo does not turn with .write()
servo3.attach(pin3);   // This servo does not turn with .write()

If we switch the order, then two motors (servo2 and servo3) run instead

servo2.attach(pin2);   
servo3.attach(pin3);
servo0.attach(pin0);   // This servo does not turn with .write()
servo1.attach(pin1);   // This servo does not turn with .write()

Potential Reasons

This issue has already been reported in the community.
https://forum.arduino.cc/t/esp32-s3-not-working-with-2-servos/1157409/9

Current Workaround

ESP32-S2 AnalogWrite Library works with more than two servos. We have tested with 4 servos.
https://forum.arduino.cc/t/esp32-s3-not-working-with-2-servos/1157409/14

Timer Incompatibility with Camera Functions?

Hello,

Doing some research and it is looking like there is a potential problem when using servo.attach.

I'm noticing, that there channels maybe interfering with one another causing an error to be thrown

"[E][camera.c:495] i2s_run(): Timeout waiting for VSYNC"

I would prefer not to have to rewrite code to use PWM & ledcAnalogWrite as you have a wonderful library.

Thoughts on how to rectify this?

Kind Regards,
TK

rename .read() to .getLast()

The name of the method that returns saved ticks implies that it's retrieving information directly from the servo itself (suggesting the user could manually move the servo and use it like a potentiometer). However, it actually just returns the last value that was passed to .write().

Attaching the servo at a given angle

Does the ESP32Servo library support detaching and following attaching the servo motor again at exact desired angle without unwanted jumps of the motor? How to achieve this behavior?

Getting an error resolving around tone?

I am not including my code only because I'm simply just trying to run the example sweep function, but everytime I try to compile my code after installing the library (same error every version 12.0-13.0), I get this error

/Users/joshuamendiola/Documents/Arduino/libraries/ESP32Servo/src/ESP32Tone.h:11:41: error: macro "tone" requires 3 arguments, but only 2 given void tone(int pin,unsigned int frequency);

it seems there is an issue with the tone library being used in the ESP32Servo file, but I am unable to pinpoint the problem after a few hours of debugging, what is going on here?

read() does not return last write() accurately

There is a loss of precision when reading back the angle that was last written to the servo. This prevents doing something like this: servo.write(servo.read()+1); to increment a servo by 1 degree from it's current position.

Possible cause is the value being mangled by the (integer) map() macros in the read() & write() functions.

Example test code:

for (int16_t i = 0; i <= 30; i++) {
x_axis.write(i);
Serial.printf("Wrote: %u Read: %u\n", i, x_axis.read());
}

Output:
Wrote: 0 Read: 0
Wrote: 1 Read: 0
Wrote: 2 Read: 1
Wrote: 3 Read: 1
Wrote: 4 Read: 3
Wrote: 5 Read: 3
Wrote: 6 Read: 5
Wrote: 7 Read: 5
Wrote: 8 Read: 7
Wrote: 9 Read: 7
Wrote: 10 Read: 9
Wrote: 11 Read: 10
Wrote: 12 Read: 10
Wrote: 13 Read: 12
Wrote: 14 Read: 12
Wrote: 15 Read: 14
Wrote: 16 Read: 14
Wrote: 17 Read: 16
Wrote: 18 Read: 16
Wrote: 19 Read: 18
Wrote: 20 Read: 18
Wrote: 21 Read: 20
Wrote: 22 Read: 20
Wrote: 23 Read: 22
Wrote: 24 Read: 22
Wrote: 25 Read: 24
Wrote: 26 Read: 24
Wrote: 27 Read: 26
Wrote: 28 Read: 26
Wrote: 29 Read: 28
Wrote: 30 Read: 29

Clearly this is not very useful

Examples include wrong header file

Thanks for making this! Some of the example source files mistakenly include "ESP32_Servo.h" instead of "ESP32Servo.h". Threw me for a loop and would be a wonderful quick fix :)

Best,
-John

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.