Code Monkey home page Code Monkey logo

servoesp32's Introduction

ServoESP32 Build Status

Generate RC servo signal on a selected pins with ESP32 device and Arduino framework.

Base on servo library for stm32f4 (d2a4a47).

Interface

The interface is similar to Arduino/Servo: https://www.arduino.cc/en/Reference/Servo

But the function attach() is different:

bool attach(
    int pin,
    int channel = CHANNEL_NOT_ATTACHED,
    int minAngle = DEFAULT_MIN_ANGLE,
    int maxAngle = DEFAULT_MAX_ANGLE,
    int minPulseWidthUs = DEFAULT_MIN_PULSE_WIDTH_US,
    int maxPulseWidthUs = DEFAULT_MAX_PULSE_WIDTH_US,
    int frequency = DEFAULT_FREQUENCY
);

More information in source code documentation.

Example: 04-SimpleServoAngles

There are also a ServoFloat and ServoDouble variant available. Use one of these when working in radians.

Example: : 05-SimpleServoRadians

IMPORTANT INFO

According testings, the frequency for ESP32 S2/S3/C3 has to be set at least to 200 Hz. Here is an example, how to set just frequency:

Servo servo1;
const int servoPin = 4;
const int frequency = 200; // Hz

servo1.attach(
    servoPin, 
    Servo::CHANNEL_NOT_ATTACHED, 
    Servo::DEFAULT_MIN_ANGLE, 
    Servo::DEFAULT_MAX_ANGLE, 
    Servo::DEFAULT_MIN_PULSE_WIDTH_US, 
    Servo::DEFAULT_MAX_PULSE_WIDTH_US, 
    frequency
);

For more information look at the PR25

PlatformIO

This library is also available at the PlatformIO as ServoESP32.

Arduino IDE

This library is available in Arduino IDE Library Manager as ServoESP32.

Known issues

Problem with build in Arduino IDE 1.8.10

There was an issue with building this library in Arduino IDE 1.8.10. But this issue should be fixed in Arduino IDE 1.8.11.

servoesp32's People

Contributors

cednik avatar clevehex avatar h2zero avatar jarekparal avatar mikaeltulldahl avatar tigertv avatar tikitikitiki 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

servoesp32's Issues

Incompatible with Preference.h (Flash writing)

On a Firebeetle ESP32 board, Servo.h can make it impossible to write to Onboard Flash. Reproduce like this:

  1. Attach more than 8 servos (I used 9) (in software; hardware doesn't matter)
  2. Sometimes required, sometimes not: Output PWM on 9th attached servo using any pin. It is not important what you do with the first 8 servos or if you use them at all.
  3. Try to use Preferences.h to write to on-board Flash: Cannot write, and read only returns the default value. No error messages.
  4. Detach Servos
  5. Try to use Preferences.h again: Works fine

The error occurs irrespective of what pins are used for the Servo. I suspect a problem with timer allocation or something like that, but I am no expert in ESP32 timers or the two libraries involved.

Servo::detach() can change servo position

it is useful to detach servo after movement in cases like:

  • limit current consumption (servo mechanic could keep position without electronic control for small forces)
  • limit servo vibrations (usually present in cheap servos) - sometimes we can observe vibrations after reaching designed position

In current implementation (if I understood correctly) detach() is not controlling pwm state and can disable pwm during high state and move servo to different position.

It would be great to support some safe detach, which will check if pwm is not in active state and won't change servo position.
Proposal for interface:

detach(bool isSafe=false); //false default to keep backward compatibility 
or
safeDetach();

This library uses depreciated functions from ESP32 Core 2.0.x

Hello, I found that when I try to use this library with ESP32 Core 3.0, I get errors saying certain functions were not declared in scope (namely ledcSetup, ledcAttachPin, and ledcDetachPin). I found out these functions were removed in Core 3.0 and the same test sketch I use works perfectly with Core 2.0.17.

If you are having trouble try going to the board manager and changing your esp32 installation to 2.0.17

Category

Change category to "Device control"?

Could not build library with provided examples

Hi, I am trying to use this library on ESP32, but when I try to build the example I get more errors:

for example:
In file included from src/main.cpp:2: .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:68:81: error: call to non-'constexpr' function 'const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]' static const int TIMER_RESOLUTION = std::min(16, SOC_LEDC_TIMER_BIT_WIDE_NUM);

Log, platformio.ini and code included.

Platform versions:
Espressif32 6.4.0
Atmel AVR 4.2.0

attachments.zip

Compile-time check attach() parameters?

Is it possible to have some compile-time check of the attach() parameters, e.g. with constexp?

https://github.com/RoboticsBrno/ESP32-Arduino-Servo-Library/blob/4b1f4a560bc0e25c51faf7ac7a7fc35773a4ba7c/src/Servo.cpp#L60

I am now thinking about modification like this:

    _minAngle = constrain(minAngle, MIN_ANGLE, MAX_ANGLE);
    _maxAngle = constrain(maxAngle, MIN_ANGLE, MAX_ANGLE);
    _minPulseWidth = constrain(minPulseWidth, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
    _maxPulseWidth = constrain(maxPulseWidth, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);

But that is a hidden side effect and I would like rather make the check in the compile-time and show same compile message (if the parameters would be constexp). Is that possible @yaqwsx ?

Or is there some better way? Are exceptions available in the Arduino EPS32 framework or how the ESP-IDF throws the error message?

Naming conflicts with "default" <Servo.h>

When compiling for the DOIT ESP32 DEVKIT V1 board on Arduino IDE 1.8.10 I am getting conflicts with the "default" Arduino servo library.

Due to this problem, none of the examples compiles.

This is easily fixed by changing the name of the header file to something more unique. Ideally, Arduino IDE would have a way of differentiating them, but yeah, it doesn't.

I suggest by renaming Servo.h ➡️ ESP32ArduinoServo.h. Based on the name you have chosen for your repository, this sounds like a reasonable choice to me.

#10 should solve this issue.

Frequency parameter is not working as expected

Hi and thanks for this library.

It seems like the frequency parameter that can be passed to the attach function is not actually changing the frequency but the pulse width.
I have initialized two Servos like this:

  myservo1.attach(servoPin1, Servo::CHANNEL_NOT_ATTACHED, Servo::DEFAULT_MIN_ANGLE,
            Servo::DEFAULT_MAX_ANGLE, Servo::DEFAULT_MIN_PULSE_WIDTH_US,
            Servo::DEFAULT_MAX_PULSE_WIDTH_US, 200);

  myservo2.attach(servoPin2);

Here are signals measured on two pins, the first (yellow) one was set at 200Hz, and the second (blue) one was left at default value which is 50Hz
And both signals are actually at 50Hz
image

Here is what is expected: the yellow signal has a frequency 4 times higher:
image

Repeated use of attach / detach leaks channels

The channel allocation mechanism in this library is primitive and buggy. As a result, if you use the automatic channel allocation feature, repeated calls to detach and attach will cause the library to exhaust the supply of channels and it will stop functioning.

I have worked around this by avoiding the automatic allocation and instead passing servo-specific channel references in the call to attach. However, this bug can be easily fixed as follows:

Instead of treating the "channel_next_free" static integer as single channel number, treat it as a bitfield. Then, when allocating a channel in attach(), set the bit associated with that channel to mark it as in use. Finally, clear said bit in the detach() function.

In other words in the implementation of attach() replace this code:

    if(channel == CHANNEL_NOT_ATTACHED) {
        if(channel_next_free == CHANNEL_MAX_NUM) {
            return false;
        }
        _channel = channel_next_free;
        channel_next_free++;
    } else {
        _channel = channel;
    }

with this:

    if(channel == CHANNEL_NOT_ATTACHED) {
        for (int mask = 1; mask != 0; mask <<= 1)  {
            ++channel;
            if((channel_next_free & mask) == 0) {
                break;
            }
        }
    }
    if(channel > CHANNEL_NOT_ATTACHED && channel < CHANNEL_MAX_NUM) {
        _channel = channel;
        channel_next_free |= (1 << channel);
    } else {
        return false;
    }

and in the implementation of detach() replace this code:

    if(_channel == (channel_next_free - 1))
        channel_next_free--;

with this:

    channel_next_free &= ~(1 << _channel);

'ledcSetup' was not declared in this scope

I use this lib in PlatformIO for CLion, but the console print these msgs shown in the img after PlatformIO build.
image
So I clone this repository, and also can not find the methods was not declared.

Would the extraction of a Servo interface be a welcome contribution?

Thanks for this library, first of all! I am using it to flip the light switch in my workshop after receiving a command over MQTT. Works nicely!

I was looking into testing my code a bit more and wanted to fake (mock) the real servos in my test code. I found ArdiunoFake, which is based on FakeIt. Both are focused on providing mocks of interfaces, which is hard (impossible?) to do with concrete classes like Servo.

Looking at the code, it seems quite possible to rename the current Servo class to e.g. ServoESP32 and then extract a new Servo interface from it. Doing this would make mocking and testing the code using Servo possible or at least much easier.

The downside would that it breaks backward compatibility. If that is an issue, keeping Servo and extracting IServo would be a possibility (not preferred by me, at least).

Before investing time into that, I wanted to check if there is interest in a contribution like this, or if there are alternative suggestions how to unit-test code that is using this library without having to attach an actual servo during testing.

Different values for write() and read()

Hi,
First of all thanks for your code.
I am starting with ESP32 and I did a small snippet to control a servo with a webpage. All works good but I add an api route to the server to read the position of the servo, and there is always a difference of 1 degree. I do for example myServo.write(90), and when I do myServo.read() I get 89, is this normal? is because of my servo?
I am using one old hitec HS-300

Thanks!

servo YUKI model family SUMO 1143HB

hi
this servo (SUMO 1143HB) has a mechanical 360 degree rotation.
I'm testing this servo on ESP32 and using this library is not possible to obtain more than 180 degree rotation.
For my use I need a 260 degree rotation
I tried to modify the .h and .cpp but i'm not smart in c++, so at moment i found no solution.
someone may help me to do this ?
I saw a lot of video but all them make a hardware modification.
I'm sure that with this kind of servo is possible to obtain 260 degree via software too.
Please look the video that solve the problem without opening of servo
https://www.aliexpress.com/item/1005002973104855.html?spm=a2g0o.order_list.order_list_main.19.21ef1802XnKwfL

your help is appreciated

Compilling error

Hi, fist of all, thx for your lib!

I can't build my sketch because of these errors, I'm using PlatformIO, but I've tested ArduinoIDE and received diferent erro on the same file and line of code:

PlatformIO
Executing task in folder Nykko_Brat_rsuzano: platformio run --target upload

Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)

Verbose mode can be enabled via -v, --verbose option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (5.2.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:

  • framework-arduinoespressif32 @ 3.20005.220925 (2.0.5)
  • tool-esptoolpy @ 1.40201.0 (4.2.1)
  • tool-mkfatfs @ 2.0.1
  • tool-mklittlefs @ 1.203.210628 (2.3)
  • tool-mkspiffs @ 2.230.0 (2.30)
  • toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
    LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 37 compatible libraries
    Scanning dependencies...
    Dependency Graph
    |-- DabbleESP32 @ 1.5.1
    |-- PWMMotorControl @ 2.1.0
    |-- ESP32 AnalogWrite @ 0.2.0
    |-- ServoESP32 @ 1.1.1
    |-- Preferences @ 2.0.0
    Building in release mode
    Compiling .pio/build/esp32dev/src/main.cpp.o
    In file included from src/main.h:5,
    from src/main.cpp:1:
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:68:81: error: call to non-'constexpr' function 'const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]'
    static const int TIMER_RESOLUTION = std::min(16, SOC_LEDC_TIMER_BIT_WIDE_NUM);
    ^
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h: In member function 'T ServoTemplate::mapTemplate(T, T, T, T, T) const':
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:256:12: warning: 'if constexpr' only available with -std=c++17 or -std=gnu++17
    if constexpr (std::is_floating_point_v) {
    ^~~~~~~~~
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:256:28: error: 'is_floating_point_v' is not a member of 'std'
    if constexpr (std::is_floating_point_v) {
    ^~~~~~~~~~~~~~~~~~~
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:256:28: note: suggested alternative: 'is_floating_point'
    if constexpr (std::is_floating_point_v) {
    ^~~~~~~~~~~~~~~~~~~
    is_floating_point
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:256:49: error: expected primary-expression before '>' token
    if constexpr (std::is_floating_point_v) {
    ^
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:256:50: error: expected primary-expression before ')' token
    if constexpr (std::is_floating_point_v) {
    ^
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h: In member function 'T ServoTemplate::mapTemplate(T, T, T, T, T) const [with T = int]':
    .pio/libdeps/esp32dev/ServoESP32/src/Servo.h:262:5: warning: control reaches end of non-void function [-Wreturn-type]

ArduinoIDE
In file included from /home/rsuzano/Arduino/libraries/ServoESP32/examples/01-SimpleServo/01-SimpleServo.ino:1:0:
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h:68:54: error: 'SOC_LEDC_TIMER_BIT_WIDE_NUM' was not declared in this scope
static const int TIMER_RESOLUTION = std::min(16, SOC_LEDC_TIMER_BIT_WIDE_NUM);
^
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h: In member function 'bool ServoTemplate::attach(int, int, T, T, int, int, int)':
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h:52:24: error: 'SOC_LEDC_CHANNEL_NUM' was not declared in this scope
#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM)
^
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h:132:38: note: in expansion of macro 'LEDC_CHANNELS'
if (channel_next_free == LEDC_CHANNELS) {
^
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h: In member function 'T ServoTemplate::mapTemplate(T, T, T, T, T) const':
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h:256:12: error: expected '(' before 'constexpr'
if constexpr (std::is_floating_point_v) {
^
/home/rsuzano/Arduino/libraries/ServoESP32/src/Servo.h:258:11: error: 'else' without a previous 'if'
} else {

Order of parameters in attach

This library changes the order of parameters in the attach() function as compared to the standard arduino library, please change this or document it!
Thanks

Can't use library with MG 996R servos

This library works perfectly with regular servos, like SG90, but when I tried high torque MG996R, behavior is stochastic. I know, MG996R is continuous servo but motor don't want "behave" even as described here (in answer).

Any suggestions guys? Or please share a link to the standard high torque servos, not expensive as a hell 😉

Compatibility with ESP32-S2

Hello, thank you for developing this library. I am using the ESP32-S2 and from expressif's documentation, the PWM's are limited to 14-bits. As written, I have not been able to get the code to work on an S2, however, once I changed the ledcSetup call to 14, it works well.

From:
ledcSetup(_channel, 50, 16); // channel X, 50 Hz, 16-bit depth

To:
ledcSetup(_channel, 50, 14); // channel X, 50 Hz, 14-bit depth

You may want to consider with passing this as a variable or creating a function that allows this to be set. Thanks again.

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.