gin66 / FastAccelStepper

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

ESP32-S3 backward/anti-clockwise movement ignores speed settings #215

Closed rqi14 closed 7 months ago

rqi14 commented 7 months ago

Hi. I tried the following code. I found that when the motor is asked to move backward. It doesn't respond to setSpeedInHz(). It uses the old speed set before an instruction telling it to move forward.

steppers[0]->setSpeedInHz(speed);  // speed is negative for backward, positive for forward
steppers[0]->setAcceleration(acceleration);  // acceleration is negative for backward movement, and positive for forward
steppers[0]->moveByAcceleration(acceleration);
// set the speed
steppers[0]->setSpeedInHz(speed);
// set the acceleration
steppers[0]->setAcceleration(acceleration);
int32_t new_position = distance + steppers[0]->getCurrentPosition();
steppers[0]->moveTo(new_position);

I tried applySpeedAcceleration();. It didn't help.

For example, if the motor is set to move forward at 10hz, 20hz, 30hz using the above code, you can see the speed change. If the motor is set to move backward, at 10hz, 20hz, 30hz, there is no speed change. If the board is reset and the motor hasn't been moved before, the motor doesn't move. If the motor is set to move forward at 10hz, then backward at 20hz, it moves backward at 10hz. If the motor is set to move forward at 10hz, then backward at 30hz, it moves backward at 10hz. If it is further set to move at other speeds for backward movement, it will keep moving at 10hz. Unless a new speed is set with forward movement instruction, followed by a new backward movement call.

In short, setSpeedInHz doesn't work if followed by a backward movement call, evenif apply speed acceleration is called. Only setSpeedInHz followed by a forward movement call changes the speed.

gin66 commented 7 months ago

most of the calls yields an return code. could you please check those in order to narrow down the issue ?

rqi14 commented 7 months ago

most of the calls yields an return code. could you please check those in order to narrow down the issue ?

Thank for the hint! I found I understood it wrong.. It seems the definitions of acceleration for setAcceleration and moveByAcceleration are different. For setAceleration, it cannot be negative and for moveByAcceleration, it can be negative for backward movement.