Accuracy of Accelstepper speed timing

416 views
Skip to first unread message

Daniel Vaughan

unread,
Mar 8, 2021, 5:18:42 AM3/8/21
to accelstepper
Hi All,

I've set up an Arduino Uno to use an external clock source running at 8MHz. The source is a Ublox Neo M8N that is synced to GNSS frequency. The uno works fine, albeit at the slower 8MHz speed.

I've run a simple test as follows, with start, finish and elapsed being unsigned long:

  start=micros();
  delay(10000);
  finished=micros();
  elapsed=finished-start;
  Serial.println(elapsed);

This code is returning varying results, none of which are spot on 10000000 microseconds, eg:

20:56:24.495 -> 10000016
20:56:34.484 -> 10000024
20:56:44.479 -> 10000032

Further, if I increase the delay in the code ten-fold, the results are:

21:03:16.239 -> 100000016
21:04:56.237 -> 100000032
21:06:36.259 -> 100000024

I'm not sure why, but to me, the results suggest there's a variable "overhead" in the timing (which is always positive, by the way). The results don't suggest a timing error or drift - or not a very large one.

Anyway, I understand the accuracy of the accestepper is tied to the accuracy of the clock source, but I wonder i whether I will see similar errors through the library. Will they be compounding I will such error be seen at the start of the stepper motor operation?

My use of the Accelstepper library in this case will always be to drive the stepper motor at a constant speed, with no acceleration or changes (other than to start and stop).

Basically, I'm looking to understand whether I am able to drive the motor with precise accuracy.

I have a plan to calibrate the rotational speed over a long duration (i.e. 24 hours), so I'm not so concerned with minor and consistent error - i'll be able to compensate for that. I'm really just concerned about the speed being very consistent.

FYI I'll be driving a 4 wire NEMA 17 with 1/16 microstepping at somewhere between 95-100 steps/second - so relatively slow. I'll also be setting the speed to a number of decimal points.

Any guidance or insight anyone can provide would be greatly appreciated. Let me know if there's any detail i've missed that may help.


Ian Bowden

unread,
Mar 8, 2021, 3:00:35 PM3/8/21
to accels...@googlegroups.com

Hi Daniel,

I have duplicated your simple test program, and I don't get the same result as you.  I'm using an Arduino Uno with an inbuilt 16 MHz crystal.

My result for 'elapsed' is 10000008

If I change the delay from 10000 to 1, 'elapsed' comes out as 1008.

The reason for this is that the functions delay() and micros() both take a finite processing time.  Although the delay is accurate, it takes further time to process and record the value of the microseconds counter and load the number into 'finished'.

You are registering processing times of 16 - 32 microseconds.  With an 8MHz clock, that's your 16 microseconds explained away.  

In summary, I think your delays are due to processing time.

I can't say why you sometimes get 24 or 32.  Try changing the delay to 1 and see whether that gives similar results. 

Does the Arduino software know that you are using an 8MHz clock? All the timing depends on an interrupt when timer0 overflows, which does something odd with the fractional number of milliseconds per timer0 overflow.  I don't fully understand what it does, but it certainly needs to know the number of clock cycles per microsecond.

There could be error introduced if it's assuming the clock is 16MHz.  Further, the delay() function calls micros() frequently while it's timing the delay.

Good luck

    Ian B.

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/accelstepper/1127af4c-29b1-4a9c-8b29-4d2d6cf4fa0en%40googlegroups.com.

Brian Engel

unread,
Mar 8, 2021, 3:20:59 PM3/8/21
to accels...@googlegroups.com
There's some random overhead involved (probably from the serial output).... 

Try using timer interrupts if you need high precision but use caution if you try to output to serial - it may take too long and the next timer will interrupt then things will seem to freeze up.  






Daniel Vaughan

unread,
Mar 8, 2021, 3:24:08 PM3/8/21
to accels...@googlegroups.com
Thanks Ian,

The Uno has been bootloaded using the MiniCore Atmega328 using the external 8Mhz option. To be sure, I set F_CPU = 8000000L. I'm pretty sure the system knows and is running to the 8MHz clock speed.

If I set the delay to 1 - I get about 1024 micros fairly consistently, but it occasionally jumps to 1032 or down to 1016. I understood micros maximum resolution was 4us, so not sure why it consistently jumps 8us.

One thought I had was whether my clock source has some small shift in it. For arguments sake, my 8Mhz clock might have  a fairly consistent 24us processing time (or somewhere within 4us of that) and the clock source is actually compressing and decompressing slightly, but hovering around the 8MHz - perhaps explaining why its usually a 24Mhz overhead and sometimes shifts down or up.

Daniel Vaughan

unread,
Mar 8, 2021, 3:26:25 PM3/8/21
to accels...@googlegroups.com
Thanks Brian,

I have started looking at interrupts and will follow through with that.

It may be that given my project only requires one function - to drive the stepper accurately at one speed, with no acceleration or change in direction, that I end up just using interrupts and my own simple code to drive the stepper motor.

Brian Engel

unread,
Mar 8, 2021, 3:37:16 PM3/8/21
to accels...@googlegroups.com
I used it for my project because I needed high precision stepping with minimal jitter. It was for a telescope drive system which is like a high precision clock (except the actual time isn't relevant :-)).  That did require me to implement my own acceleration solution which involved creating an array of ints that were timer intervals. I would then move up and down the array as the motor would speed up or slow down to get my next timer interrupt value.   

For applications that need to run up and down to/from a constant speed (versus random movement to a particular position), that approach works great. Nice if accelstepper could work in an option to pre-calculate the acceleration profile at startup and use that array for its step time intervals -vs- doing floating point math everytime.  I know from my own experience that would take significant rework to incorporate that.... and there's some challenges like memory availability to store that array across various arduino devices.






Daniel Vaughan

unread,
Mar 8, 2021, 4:30:05 PM3/8/21
to accels...@googlegroups.com
Hi Brian,

I'm doing a similar thing with my stepper. It's a geared camera mount that tracks the astronomical day.

I'm not worried about acceleration, just an accurate body of rotational movement.

I will need floating points at some point because I need to step at something like 96.33331376 steps per minute. However, I hope that can be resolved accurately enough for my purposes.


Brian Engel

unread,
Mar 8, 2021, 4:47:25 PM3/8/21
to accels...@googlegroups.com
Sounds like a barndoor tracker :-)...   It probably will not need to be that accurate given the focal length of your camera len(s). You can calculate the step angle on the sky if you wish but I suspect polar alignment and periodic error will far exceed the accuracy of the stepper step angle. 

If you're lucky enough that you don't need to accelerate the stepper and thus can just start it at a particular speed (i.e. it doesn't stall),  then timer interrupts is definitely what you want.  Just set the interrupt to X microseconds and have it call an interrupt handler that sets the step pin high (then low after a certain minimum pulse width). 

If super high precision is important, note that most arduinos use ceramic resonators vs quartz crystals for their oscillators.  I used a "Ruggeduino" that is industrialized for cold temps and uses a crystal oscillator..... more expensive but offers protection against bad things happening. 







Reply all
Reply to author
Forward
0 new messages