I’d like to control several stepper motors from an mbed board. Before I do so I’d like to see if my idea seems reasonable.
Conditions:
-Stepper motors are controlled by creating a positive flank on a gpio pin in variable time intervals
-Pulse width of those gpio pins should be ~10us (minimum)
-Pulses should be created between 0-60kHz e.g with a 16 us period
Is it realistic to think that i can create 6 threads with high priority and run this pseudocode within each:
while true:
wait(period) //as low as 16 us
setGpioHigh()
wait(10us)
setGpioLow()
From outside those threads i can then vary the period to vary the speed of the motor
Obviously exact timing will depend on the processor used, but lets assume a NUCLEO-H743ZI2 with 400MHz. Is this something I can realistically expect to work? Or do these timings seem too tight? Are threads even the right way to go for something like this or is there too much overhead in thread switching? Does the wait_us function free the processor to switch do a different thread? Apart from these 6 threads I’d like to run a TCP Server in a lower priority thread to accept movement commands.
EDIT: Would it make more sense to create a single ticker running at some maximum frequency (16us period) which checks if a motor is ready for a pulse and if so the GPIO is set high, and in the next cycle low again? I think I prefer this solution actually!
Using threads or the Ticker class have too much overhead for such frequencies. It is more efficient to use the hardware timers, I have used this also for a stepper control, and used variable timings between the pulses for the accel/decceleration ramps. The code was for STM32F4 and uses hardware registers, it needs to be updated for F7 or H7 MCUs. Its still in a private github area because there is a lot undocumented stuff.
But for first tests you can start with the Ticker class, on a H7 it will work with 10 µs pulses.
Do you know what frequency the Ticker class is limited too? How do I find this out for a specific board?
Also, do you know what the GPIO toggle speed is for Mbed OS when using the DigitalOut Class? is there some overhead in the OS or is GPIO toggling “bare metal” speed?
Where can I find details about this kind of information=
The max ticker frequency depends very much on the target, there are no fixed limits. The resolution is in µs, it uses the main Mbed timer with 1 µs resolution. There is some overhead for scheduling the timer events, and for times of some 10 µs it will be notable.
You can activate perfomance statistics, that gives you the CPU load and you can check the cost. https://os.mbed.com/docs/mbed-os/v6.9/apis/mbed-statistics.html
I have to port my HWTimer class also to a F7 and can check also the H7 registers.
You have also to decide abaout a strategy to create the pulses, with a fixed period time interrupt or variable time periods, which would need more timers.
There are also forks of grbl, a standard software for steppers which handles G-Code commands, e.g.
The performance of DigitalOut is pretty good (we are not talking about Arduino ), it will be inlined and I would start optimizing here only when you see perfomance problems.