PDA

View Full Version : Right way to step a stepper



tomfrisch
06-12-2013, 03:35 PM
I'm making a device that moves a camera along a linear slider controlled by a stepper.

The step speed is affected by the amount of stuff the code does in the main loop before returning to the beginning when it sends another step. This stuff includes checking to see if one of the end stops have been hit, and checking to see if one of the 3 buttons has been pushed. If I get around to utilizing the accelstepper library, that would also add more overhead in there.

I don't need the maximum speed, but do need consistent speed while the processor is doing other things.

I don't need to check the buttons every cycle, but do need the end stops to reverse direction immediately on contact.

I'm sure there are 10 better ways to do this- maybe interrupts? maybe some sort of multi-threaded state machine?

Ideas?

-Tom

nox771
06-12-2013, 05:37 PM
I'm sure there are 10 better ways to do this- maybe interrupts? maybe some sort of multi-threaded state machine?

You don't mention what you are using for control. If it is a Teensy3, then for making something occur with a fixed period you can use IntervalTimer. I didn't see a writeup on it yet on the web docs, but it's pretty straightforward:



IntervalTimer timer1;

void setup()
{
...
timer1.begin(stepFunction, period); // replace "period" with interval in microseconds
...
}

void stepFunction(void)
{
// do stuff

// make it one-shot
if(condition)
timer1.end();

// make it change period
else if(other condition)
timer1.begin(stepFunction, period2);
}

tomfrisch
06-12-2013, 09:15 PM
Yep, I'm using Teensy3 for this project.

That intervaltimer is pretty cool. Does it run concurrently with other tasks?

-Tom

nox771
06-12-2013, 09:20 PM
The IntervalTimer is using the PIT interrupt, so it will run in the background.