gin66 / FastAccelStepper

A high speed stepper library for Atmega 168/328p (nano), Atmega32u4, Atmega 2560, ESP32, ESP32S2, ESP32S3, ESP32C3, ESP32C6 and Atmel SAM Due
MIT License
311 stars 71 forks source link

Help for smooth motion #289

Open tomassasovsky opened 1 week ago

tomassasovsky commented 1 week ago

Hey there! I'm trying to make simple movements with the motor, having it just move at a constant speed. I'm facing an issue where the motor makes a stuttering sound, sorta sounds like a locomotive. The motion is not smooth at all, it's very interrupted.

Running this with a TMC2209 V2.0 and ESP32-WROOM-32.

I'm using the code from the example:

#include "FastAccelStepper.h"

// As in StepperDemo for Motor 1 on ESP32
#define dirPinStepper 18
#define enablePinStepper 26
#define stepPinStepper 14

FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;

void setup() {
  engine.init();
  stepper = engine.stepperConnectToPin(stepPinStepper);
  if (stepper) {
    stepper->setDirectionPin(dirPinStepper);
    stepper->setEnablePin(enablePinStepper);
    stepper->setAutoEnable(true);

    // If auto enable/disable need delays, just add (one or both):
    // stepper->setDelayToEnable(50);
    // stepper->setDelayToDisable(1000);

    stepper->setSpeedInUs(1000);  // the parameter is us/step !!!
    stepper->setAcceleration(100);
    stepper->move(1000);
  }
}

void loop() {}
gin66 commented 1 week ago

I would assume, this is not a sw issue. Could you please check the cable ? Especially ground connections are often cause of unexpected behaviors.

tomassasovsky commented 1 week ago

Very well might be. Do I need to add any capacitors, resistors, etc. to my circuit?

gin66 commented 1 week ago

I do not know your setup. Do you have an oscilloscope or similar to check, that the steps are generated properly ? Or just start without FastAccelStepper and control the stepper with a loop, which contains just a toggle of step pin and a delay ?

tomassasovsky commented 1 week ago

I don't have an oscilloscope, no. Could you provide an example snippet for that?

gin66 commented 1 week ago

Something like:

// As in StepperDemo for Motor 1 on ESP32
#define dirPinStepper 18
#define enablePinStepper 26
#define stepPinStepper 14

void setup() {
   pinMode(dirPinStepper, OUTPUT);
   pinMode(stepPinStepper, OUTPUT);
   pinMode(enablePinStepper, OUTPUT);

   digitalWrite(dirPinStepper, LOW);
   digitalWrite(stepPinStepper, LOW);
   digitalWrite(enablePinStepper, LOW);
}

void loop() {
   digitalWrite(stepPinStepper, digitalRead(stepPinStepper) == LOW ? HIGH:LOW);
   delay(50); // => one step in 2*50ms, so 10 steps/s
}
tomassasovsky commented 1 week ago

I tried your code with a delay of 1ms which is closer to the speed I need, and it's very noisy.

https://github.com/user-attachments/assets/1ffbf29e-eb48-4f73-a4be-34a9379bf9a6
gin66 commented 1 week ago

Thanks for sharing the video. If this low level code does not work, then something is wrong with the hardware.: Ground connection, assignment of dir/step/enable to the gpio, cable from stepper to stepper controller - especially the pin assignment, is the power supply strong enough, are the cables not too long, is the uC constantly rebooting….? Good luck in your endeavor