Continuously running a stepper motor without blocking?

Hi everyone,
I have an application where I would like to use a stepper motor for its holding torque, but I want to control it with feedback control similar to a DC motor. Now before you say "That's not what steppers are for!" I know! Generally steppers are meant for open loop positional control. But I want to pick your brains on how this might be done or even if it's possible!

Basically I'd like to continuously spit out square wave to the stepper driver to keep the motor moving, but dynamically vary the frequency of the pulses to control the speed. My thought is that I can achieve some semblance of speed control by doing so. Then, when I need to hold a load, I can stop the motor (signal of "0 Hz") and have it hold.

The problem I face is that, at least in the stock arduino stepper library, the step() function blocks until it is finished executing. Obviously this is a good thing when using the stepper in its normal configuration, but for me it makes it impossible to update the speed on the fly.

Anyone have any thoughts on implementing what I need to? It doesn't need to include the stepper library.

Thanks!

You do not need a square wave to control a stepper (assuming you are using a stepper driver that takes step and direction signals). All you need is a short pulse (about 10 microsecs wide) at intervals that determine the speed.

Have a look at the second example in this Simple Stepper Code. It can easily be modified to produce pulses indefinitely. Changing the value of millisBetweenSteps will change the speed.

Post a link to the datasheet for the stepper motor you are using and tell us what stepper driver you are using.

...R
Stepper Motor Basics

Robin,
Thanks for the reply - I misspoke when I said square wave, I meant pulses. In any case, I had a look at your code, and I think it still has the issue I'm trying to solve - blocking, that is. While sending pulses to the stepper motor, nothing else can be done.

What I'm really looking to do it sort of a "start and forget" type of scheme. It would send a continuous pulse train to the motor, and while that's executing in the background I can do all my control calculations and update the frequency (time between pulses) on the fly.

Doing this with an Arduino might be difficult since you can't do any type of multithreading. But maybe there's a way to do it with a separate oscillator or timer chip? Like have a circuit that I can provide a high signal to to start producing pulses, then a separate analog input to adjust the frequency of the pulses? Any thoughts?

Thanks,
Brock

PS - I haven't chosen a motor or driver yet, but I will be choosing one that accepts pulses as an input, as is most common.

plus1etal:
In any case, I had a look at your code, and I think it still has the issue I'm trying to solve - blocking, that is. While sending pulses to the stepper motor, nothing else can be done.

The second example was carefully crafted so it does not block :slight_smile:

...R

The AccelStepper library will probably do what you want.

MorganS:
The AccelStepper library will probably do what you want.

Fixed that for you. :wink:

I'd suggest using a potentiometer to control back/forth movement. In your main loop perform an analogRead() of the pot; when the reading is ~2.5V (give yourself some margin) change the stepper.moveTo() to zero. When the reading is > ~3V set the stepper.moveTo() to some large and irrelevant number, and then vary the stepper.setAcceleration() based on the potentiometer's position so that the farther that you turn the pot the more acceleration you use. Do the opposite for < ~2V and set a negative moveTo() position. That will give you a nice, smooth movement.