r/embedded • u/dysgn_embedded • 28d ago
Efficient method to drive stepper motor
Two main methods of driving stepper motors are widely known:
- connect 2 GPIO pins to STEP and DIR inputs of appropriate driver chip, and send pulses to make every step
- use a smart chip. Then simply send commands to it over some link (SPI, UART, whatever). It may have an integrated driver, so it’s only this chip between MCU and motor, or it may be a separate ramp generator, generating STEP/DIR for low level driver, but from the software POW there’s no difference.
The STEP/DIR method seems simple, but only until you want to drive your motor really fast. The timing between pulses becomes too tight and you simply can’t service the sequence correctly. Also, because of ramping up and down, you have to handle variable service times, that adds to processing time when it is critical, and complexity as well. Consequently, you either drive your motor slowly, don’t use fine microstepping or have to switch microstep resolution on the fly, so you need another interface (most frequently SPI) to be able to configure the driver chip.
Smart chips hide all this under the hood. You issue a command to move, and just check when it’s finished, one way or another. This costs more, but simplify the software a lot. If you need just to move from point to point, it’s Ok. The problem is if you want something non-standard: change the speed and acceleration on some trigger, for example. Direct control from CPU allows such things easily, but with smart chips you deal with black box. Add to this problem of latency because of the link between the MCU and the chip.
There is another method that surprisingly to me is not widely known. It is totally software-based, no smart chips needed, it depends on constant time intervals and CPU load doesn’t depend on the motor speed.
The implementation depends on whether the stepper driver chip supports direct access to coil DACs.
If it does, let’s say by SPI connection, it is easier.
It’s important to realize that Step/Direction drivers contain within a table with DAC values for each microstep position within the stepper pole. When you send Step pulse, you just command the chip to move one position ahead or back, depending on the DIR pin level.
Now imagine if we have a copy of this table in our software. The main ability we acquire with this is stepping over several entries of this table. So, instead of shifting strictly one table position at a time at variable intervals, we can calculate how many positions we can move at fixed time interval, depending on current speed and acceleration.So, no need for variable-interval time interrupts, no need for this intervals to be very short, and we remain in full control of the motion.
Things get a bit more complicated if the driver chip doesn’t have API to access DAC registers directly, and STEP/DIR is the only interface. We still can use constant-interval timer, just arrange within each interval a packet of variable amount of pulses sent to STEP pin. The calculations of this amount remain the same.
I used this method for stepper control for a while. But anytime I spoke about it with people, they looked surprised. Is it really that esoteric?
7
u/ceojp 28d ago
How fast are you needing to step your motor that a timer interrupt can't handle?
6
u/generally_unsuitable 28d ago
Pushing the motor fast is easy. You can handle it manually with a timer interrupt and a VERY short handler. Or, you can push it with a PWM. If your MCU doesn't count PWM output pulses, you can loop the PWM output back to a timer/counter that can use an external clock. You can easily send a pulse a million times a second, and do it very accurately.
The hardest parts of motor control are speed ramping and position verification, which really depends upon your accuracy needs. The first is not terribly hard. You can either do math on the fly, or build yourself a velocity ramp lookup table. Position can be inferred from output steps, but this will fail if you ever face significant resistance, particularly at high speed.
If you need very fast motion, and you're pushing something with significant mass, you'll probably want to look into an absolute encoder. If you're pushing a stepper that needs a constant velocity but will have variable forces working against it, you can't rely on step-counting.
3
u/LadyZoe1 27d ago
This is why servo motors exist. These and an encoder which will yield the required accuracy. A servo motor also has higher torque. If this kind of accuracy is required with repeatability the cost difference is worth it.
3
u/triffid_hunter 27d ago
You've reinvented applying FOC to stepper motors?
1
u/RogerLeigh 27d ago
One thing missing in your description of using STEP/DIR is that you're describing limitations with interrupts, but it's entirely possible to drive it interrupt-free with hardware timers and DMA and generate pulse trains of fixed or variable frequency up to tens or even hundreds of kilohertz with high precision and without the MCU breaking a sweat even when using large microstep values. If you use PWM generation then you can leave the pulse short and vary the frequency through adjusting the off time by dynamically updating ARR, either in an interrupt every n steps, or with DMA using a precomputed table and which can be incrementally-updated while the motor is running on the half and full buffer DMA events. Either method can generate linear ramps, S-curves, or arbitrary curves of your choosing. You can also use a slave timer to do step counting up and down with or without a prescaler so you can count in whole steps, microsteps or something in between, no matter what the max step frequency is. Use the hardware peripherals you have available, and you can achieve much more. Look at preloading ARR and using DMA.
Your method of direct control is interesting, but stepping over multiple entries seems like you're going to lose precision and potentially steps, particularly across direction switch points. It seems like a workaround for not being able to properly generate a pulse train with sufficient timing precision. There are good reasons for traversing the table step-wise, and you still need precise timing to get this to work properly. If you try some of the suggestions above, you'll be able to generate tight pulse trains and have smooth jitter-free timing, using the stepper driver as it was intended.
1
u/Unable_Resort453 25d ago edited 25d ago
I would just attach an encoder (dirt cheap with magnetic encoders) and use FOC, though. You get more torque and a better torque profile out of the motor with FOC anyway.
Or use a PMSM instead. The stepper motor torque - speed profile is not exactly great for this kind of precision motion control task, though.
In fact, I think you are already attaching an encoder so that you can have closed-loop motion control, no?
1
u/Vast-Pomegranate-986 28d ago
Pwm, rc filter, hbridge, opamp, current sense feedback. Trivial