ESP32Encoder icon indicating copy to clipboard operation
ESP32Encoder copied to clipboard

PCNT issue using FastAccelStepper 0.28.1 with ESP32Encoder 0.10.1 + workaround

Open pit34 opened this issue 3 years ago • 0 comments

Hello, I have found an issue about PCNT using FastAccelStepper and ESP32Encoder library on same project. Behaviour seems indicate that both library use same PCNT unit, despite recent change done on each library about PCNT handling. Here is my code about encoder and stepper :

[...]
ESP32Encoder encoder;
FastAccelStepperEngine engine                   = FastAccelStepperEngine();
FastAccelStepper      *stepper                  = NULL;
[...]

void Setup(){
[...]
encoder.attachFullQuad(ENC_A_PIN, ENC_B_PIN);
engine.init();
stepper = engine.stepperConnectToPin(MOTOR_STEP_PIN);
[...]

This way the stepper motor can move and his behaviour is OK, but encoder 's count is reset proportionally to the stepper speed. I have tried to manually change the PCNT unit used by both library, but I am not competent enough to make it work.

Nevertheless, I have found a workaround to make it work. It consist of create a first encoder i.e. "encoder0" (which will work on same PCNT unit that the stepper motor) AND to attach pins to it. If .attach() is not used, the workaround doesn't work.

[...]
ESP32Encoder encoder0;
ESP32Encoder encoder;
FastAccelStepperEngine engine                   = FastAccelStepperEngine();
FastAccelStepper      *stepper                  = NULL;
[...]

void Setup(){
[...]
encoder0.attachFullQuad(ENC_A_PIN, ENC_B_PIN);
encoder.attachFullQuad(ENC_A_PIN, ENC_B_PIN);
engine.init();
stepper = engine.stepperConnectToPin(MOTOR_STEP_PIN);
[...]

Issue posted on FastAccelStepper repository : #https://github.com/gin66/FastAccelStepper/issues/148#issue-1429012410

In hope this could be used to improve both libraries ;).

pit34 avatar Oct 30 '22 20:10 pit34