Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refuses to move high number of steps #96

Closed
joymonkey opened this issue Sep 16, 2020 · 7 comments
Closed

Refuses to move high number of steps #96

joymonkey opened this issue Sep 16, 2020 · 7 comments
Assignees
Labels
Milestone

Comments

@joymonkey
Copy link

Describe the bug
I'm having trouble telling my stepper to move a large amount of steps. This sketch successfully moves the motor 22934 steps, but when it is told to move 45868 steps, it will only move 19668 steps (checked using stepper.getStepsCompleted(), but I can also visually tell that the steps were not right).

To Reproduce
https://gist.github.com/joymonkey/b95d479af7c2375a7479c3a6775653e7

Expected behavior
Motor should move 45868 steps.

Platform Setup (please complete the following information):

  • Arduino IDE version 1.8.13
  • Board type : Arduino Nano Every
  • Stepper driver type : Pololu MP6500
  • Wiring : driver has microstep pins MS1 and MS2 set high (1/8th step setting)

Additional context
Add any other context about the problem here.

@joymonkey
Copy link
Author

Tested with full steps and confirm same behavior.
Swapped out my 400-steps-per-rotation stepper motor for a more standard 200-steps-per-rotation motor and still seeing the same behavior.
Also did some more tests...
When commanded to move 32767 steps, it moves appropriately.
When commanded to move 32768 steps, it refuses to move at all.
When commanded to move 32769 steps or higher, it moves an incorrect number of steps.

@laurb9
Copy link
Owner

laurb9 commented Sep 21, 2020

Thank you for providing the details and the sample code. The number seems to indicate an issue with 16-bit signed math as you probably surmised, but we use long even on 8-bit platform... will take a look

@joymonkey
Copy link
Author

joymonkey commented Sep 23, 2020

I replaced the Arduino Nano Every with a knockoff Arduino Nano (common version with old bootloader), and this problem does not seem to persist. So it seems to be a problem particular to the Arduino Nano Every.

@laurb9
Copy link
Owner

laurb9 commented Sep 24, 2020

I was unable to reproduce the problem with an Arduino Uno either. I don't have any megaAVR boards unfortunately.

In Arduino the Every board has an option for 328 register emulation, is there a difference when enabling it ?

@laurb9
Copy link
Owner

laurb9 commented Sep 24, 2020

Hmm, I have a suspicion. Can you please add this to the test sketch and let me know if it prints the expected value or not ?

  long v = 32768;
  Serial.println(abs(v));

@joymonkey
Copy link
Author

Hmm, I have a suspicion. Can you please add this to the test sketch and let me know if it prints the expected value or not ?

  long v = 32768;
  Serial.println(abs(v));

It prints the value as -32768 (negative)

laurb9 added a commit that referenced this issue Sep 25, 2020
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.
@laurb9 laurb9 added this to the 1.3.1 milestone Sep 25, 2020
@laurb9
Copy link
Owner

laurb9 commented Sep 25, 2020

Thank you for confirming, I will be creating 1.3.1 to hopefully address this

@laurb9 laurb9 closed this as completed Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants