Skip to content

Commit

Permalink
(fix) large step count move on Nano Every #96
Browse files Browse the repository at this point in the history
Use `labs()` explicitly when `abs()` was needed for `long` parameters.

Reason:
On Arduino Nano Every (ATmega4809), `int abs(int)` is used for a `long` argument,
resulting in failure to move more than 32767 steps in one move operation.
  • Loading branch information
laurb9 committed Sep 24, 2020
1 parent 468b1cd commit dd4b0f4
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/UnitTest/UnitTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool result(float rpm, int microstep, int steps, long elapsed_micros, long expec
char t[128];
float error = float(elapsed_micros) / float(expected_micros);
unsigned step_micros = expected_micros / steps;
unsigned error_micros = abs(elapsed_micros - expected_micros) / steps;
unsigned error_micros = labs(elapsed_micros - expected_micros) / steps;
sprintf(t, " rpm=%-4d expected=%10luµs elapsed=%10luµs step_err=%6uµs avgstep=%6uµs",
int(rpm), expected_micros, elapsed_micros, error_micros, step_micros);
Serial.print(t);
Expand Down
2 changes: 1 addition & 1 deletion src/BasicStepperDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void BasicStepperDriver::startMove(long steps, long time){
// set up new move
dir_state = (steps >= 0) ? HIGH : LOW;
last_action_end = 0;
steps_remaining = abs(steps);
steps_remaining = labs(steps);
step_count = 0;
rest = 0;
switch (profile.mode){
Expand Down
2 changes: 1 addition & 1 deletion src/SyncDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void SyncDriver::startMove(long steps1, long steps2, long steps3){
*/
long move_time = 0;
FOREACH_MOTOR(
long m = motors[i]->getTimeForMove(abs(steps[i]));
long m = motors[i]->getTimeForMove(labs(steps[i]));
if (m > move_time){
move_time = m;
}
Expand Down

0 comments on commit dd4b0f4

Please sign in to comment.