bblanchon / ArduinoContinuousStepper

An Arduino library to spin stepper motors in continuous motions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Acceleration bug after reaching speed 0

guillaume-dorczynski opened this issue · comments

Hello again

Please see this video, especially, the last 10 seconds :

https://mega.nz/file/dN8WVJpS#iqVkulLG6svWgDDNpjS3Bi8mxf5pNM9cFxXds1pblqI

As you can see, everytime the speed is 0, the stepper completely stops moving for a varying time, then it starts more abruptly than the acceleration setting. But if the speed was 1 or -1, there is no problem, it will accelerate normally. It's only happening when speed is exactly 0 !

And it seems to be only when it's decelerating but at the same time accelerating in the other direction. I mean, if I release the button until the speed is 0, and then I press the other button, there is no problem. No, I was wrong, it's happening as well, but it seems to be less noticeable.

I have no hardware to try IRL, but I really think this is a bug in your library

The code I used in the video is the example I posted earlier in another issue, you can try it yourself to confirm the bug :

https://wokwi.com/projects/345467092978369107

Edit: I tried to delete the stepper motor and driver from the simulation, just in case, but no change. Then I tried to remove Serial prints, still no luck. Finally, I also tried the exact same code on a real Arduino Mega, without stepper motor and driver as I don't have them yet, and the same problem is happening as well (I see it in the Serial monitor)

Edit: if you change to these values then the problem is much more noticeable:

const int16_t acceleration     = 10;
const int16_t deceleration     = 10;
const int16_t target_rpm       = 10;

Here is another video with these settings, it's very strange :

https://mega.nz/file/9YM1BDRD#bmpXZsL5sU0pnWvM8VV7ajMWKKhwXh5dy48R84IIR7c

Hi @guillaume-dorczynski,

While creating this library, I plotted the speed-over-time curve and sometimes noticed a very small flat area when crossing zero.
It was unnoticeable on my hardware, but I used much higher values than you.

From what I remember, the problem relates to the interval, which is computed by dividing by the speed.
To avoid a division by zero or an overflow of the interval variable, I added a MIN_SPEED constant.
You can see it in action here:

if (abs(_currentSpeed) > MIN_SPEED) {
setInterval(oneSecond / abs(_currentSpeed) / 2);
_status = STEP;
} else if (abs(_targetSpeed) > MIN_SPEED) {
// crossing the zero on the speed graph
_status = SKIP;
} else {
// stop moving
_status = WAIT;
setInterval(0);
}

My intuition is that this MIN_SPEED constant was working fine with my settings but not with yours.
I'll try to find a workaround next week.

Thank you very much for reporting this bug.

Best regards,
Benoit