madhephaestus / ESP32Servo

Arduino-compatible servo library for the ESP32
139 stars 55 forks source link

PWM channels are not deallocated correctly: detach and attach can only occur 16 times #64

Open arnaudlvq opened 2 months ago

arnaudlvq commented 2 months ago
#include <ESP32Servo.h>

Servo myServo;

void setup() {
    Serial.begin(115200);
    for (int i = 0; i < 16; i++) {
        myServo.attach(18);
        myServo.detach();
    }

    // final attach and proceed to move 
    delay(3000);
    myServo.attach(18);
    myServo.write(90);
}

void loop() {
    Serial.println("finished");
}

this doesn't work, while this does :

#include <ESP32Servo.h>

Servo myServo;

void setup() {
    Serial.begin(115200);
    for (int i = 0; i < 15; i++) {
        myServo.attach(18);
        myServo.detach();
    }

    // final attach and proceed to move 
    delay(3000);
    myServo.attach(18);
    myServo.write(90);
}

void loop() {
    Serial.println("finished");
}

I did not succeed in updating this library to correctly manage this problem. Or am I missing something ? I need to detach after each servo move

At first this was super weird to me. Why 16 ?? And I found out that ESP32 has 16 PWM channels..

stuzenz commented 1 week ago

I like this library a lot - thank you! I just want to write a note, that I am also having an error in the serial printout when detaching. This is only happening when using state machines versus delay(x)

dispense servo at 0 at 29554
MOVE_BACK_TO_START state, moving to HOLD_AT_START at 29554
HOLD_AT_START state at 29610
HOLD_AT_START state at 29656
HOLD_AT_START state at 29703
HOLD_AT_START state at 29749
HOLD_AT_START state at 29795
HOLD_AT_START state at 29841
HOLD_AT_START state at 29888
HOLD_AT_START state at 29934
HOLD_AT_START state at 29980
HOLD_AT_START state at 30026
HOLD_AT_START state at 30072
HOLD_AT_START state at 30119
HOLD_AT_START state at 30165
HOLD_AT_START state at 30211
HOLD_AT_START state at 30257
HOLD_AT_START state at 30304
HOLD_AT_START state at 30350
HOLD_AT_START state at 30397
HOLD_AT_START state at 30496
HOLD_AT_START state at 30543
HOLD_AT_START state at 30589
HOLD_AT_START state, moving to COMPLETE at 30589
COMPLETE state at 30635
dispense servo complete.
[ 30636][E][ESP32PWM.cpp:142] deallocate(): [ESP32PWM] PWM deallocating LEDc #0

If I don't detach, the above error is not present - just to state the obvious. I am trying to understand what the issue is and have momentarily just not used the detach() method - which might not be a good idea in itself.

Thanks again for the library - very handy!

    case COMPLETE:
      Serial.println("COMPLETE state at " + String(millis()));
      Serial.println("dispense servo complete.");
      dispenserServo.detach();
      Serial.println("COMPLETE state, moving to INIT at " + String(millis()));
      servoCurrentState = INIT; 
      dispenseConditionMet = false;
      break;
  }
}