I have written a simple program to move a stepper motor in one direction for a number of seconds, and then back in the opposite direction for the same amount of time. It is based on Gammon's breakfast cooking example, but the if clauses trigger while loops that run the motor for the given amount of time.
Yet I am wondering how the .run() function in AccelStepper can know when to decelerate: the stepper can't even come close reaching the destination I have set with .moveTo(), but I have the impression the stepper is decelerating before the while loop's exit condition is triggered.
The program is working as intended, but I don't understand exactly how AccelStepper is managing to decelerate before the end of the While loop. What is going on?
Many thanks.
#include <AccelStepper.h>
AccelStepper stepper(1,3,2);
int stepDest = 0;
int maxspeed = 500;
int accel = 500;
signed long endOfFirst = -4000;
signed long endOfSecond = 0;
signed long interval = 4000;
signed long loopstart;
void moveFW() {
loopstart = millis();
while ((millis() - loopstart) <= 4000) {
stepper.moveTo(50000);
stepper.run();
}
endOfFirst = millis();
}
void moveBW() {
loopstart = millis();
while ((millis() - loopstart) <= 4000) {
stepper.moveTo(0);
stepper.run();
}
endOfSecond = millis();
}
void setup() {
Serial.begin(115200);
stepper.setMaxSpeed(maxspeed);
stepper.setAcceleration(accel);
stepper.moveTo(stepDest);
}
void loop() {
if (((millis() - endOfFirst) >= interval) && ((millis() - endOfSecond) < interval)) {
moveFW();
}
if (((millis() - endOfSecond) >= interval) && ((millis() - endOfFirst) < interval)) {
moveBW();
}
}
On the surface, what's being sent to AccelStepper is: moveTo(50000), run(), moveTo(50000), run(), ... and then moveTo(0), run(), moveTo(0), run()....
stop() sets a new target position considering the current speed, and it calls moveTo(). So do you think that calling moveTo() to a point in the opposite direction triggers the calculation of a new speed and acceleration to first slow down to zero speed and then accelerate in the opposite direction, and all this happens AFTER the programs exits the first While loop?
That would mean it's just my perception that's not accurate enough, which would make plenty of sense.
Ok, so I logged it and it was as expected (should have begun there...), it starts decelerating gradually only after receiving a new moveTo() call, after exiting the while loop that is.
Speed FIRST loop: 500.00, Time (inside) FIRST loop: 3980 ms
Speed FIRST loop: 500.00, Time FIRST loop: 3984
Speed FIRST loop: 500.00, Time FIRST loop: 3988
Speed FIRST loop: 500.00, Time FIRST loop: 3993
Speed FIRST loop: 500.00, Time FIRST loop: 3997
Speed FIRST loop: 500.00, Time FIRST loop: 4001
Speed SECOND loop: 498.00, Time SECOND loop: 4005
Speed SECOND loop: 497.00, Time SECOND loop: 4009
Speed SECOND loop: 495.99, Time SECOND loop: 4014
Speed SECOND loop: 494.98, Time SECOND loop: 4019
Speed SECOND loop: 493.98, Time SECOND loop: 4023
It runs at top speed (500) until it exits the loop, then starts decelerating.
Thank you both very much for humouring me in any case!