Skip to content

Commit

Permalink
bugfix in moveTimed for the case of multiple steps per command
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Feb 20, 2025
1 parent 52bf6ab commit 2ee1cd4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
32 changes: 28 additions & 4 deletions examples/MoveTimed/MoveTimed.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct control_s controlY = {

// This means, the circle is executed in 360*4ms = 1440ms
#ifndef TIMESTEP_TICKS
#define TIMESTEP_TICKS (TICKS_PER_S/4000)
#define TIMESTEP_TICKS (TICKS_PER_S/250)
#endif

// Table generated by python:
Expand All @@ -40,6 +40,8 @@ int16_t steps[91] = { 0, 27, 55, 83, 111, 139, 167, 194, 222, 250, 277, 305, 332
};

void setup() {
Serial.begin(115200);
Serial.println("Init");
engine.init();
stepperX = engine.stepperConnectToPin(stepPinStepperX);
stepperY = engine.stepperConnectToPin(stepPinStepperY);
Expand All @@ -49,9 +51,9 @@ void setup() {
}
}
stepperX->setDirectionPin(dirPinStepperX);
stepperX->setEnablePin(enablePinStepperY);
stepperX->setEnablePin(enablePinStepperX);
stepperX->setAutoEnable(false);
stepperY->setDirectionPin(dirPinStepperX);
stepperY->setDirectionPin(dirPinStepperY);
stepperY->setEnablePin(enablePinStepperY);
stepperY->setAutoEnable(false);

Expand Down Expand Up @@ -105,22 +107,44 @@ void moveStepper(FastAccelStepper *stepper, struct control_s *control) {
rc = stepper->moveTimed(steps,duration,&actual,true);
switch(rc) {
case MOVE_TIMED_EMPTY:
Serial.println("Empty");
Serial.print("Empty:");
Serial.println(stepper->getStepPin() == dirPinStepperX ? 'X':'Y');
/* fallthrough */
case MOVE_TIMED_OK:
control->drift = duration-actual;
control->time += actual;
control->phi += 1;
break;
case MOVE_TIMED_BUSY:
//Serial.println("Busy");
break;
case MOVE_TIMED_TOO_LARGE_ERROR:
Serial.println("Too large");
break;
case AQE_ERROR_TICKS_TOO_LOW:
Serial.print("Ticks too low:");
Serial.print(duration/steps);
Serial.print(" steps:");
Serial.println(steps);
default:
Serial.println(rc);
}
}

uint32_t last_millis = 0;

void loop() {
if (last_millis != millis()) {
Serial.print(uint32_t(controlX.time));
Serial.print(' ');
Serial.print(uint32_t(controlY.time));
Serial.print(' ');
Serial.print(stepperX->getPositionAfterCommandsCompleted());
Serial.print(' ');
Serial.println(millis());
last_millis = millis();
}

if (controlX.time < controlY.time) {
moveStepper(stepperX, &controlX);
}
Expand Down
4 changes: 3 additions & 1 deletion src/FastAccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,9 @@ int8_t FastAccelStepper::moveTimed(int16_t steps, uint32_t duration,
uint32_t cmd_duration = cmd.ticks;
cmd_duration *= cmd.steps;
if (actual_duration) {
*actual_duration += cmd.ticks;
uint32_t d = cmd.ticks;
d *= steps;
*actual_duration += d;
}
steps -= cmd.steps;
}
Expand Down

0 comments on commit 2ee1cd4

Please sign in to comment.