Page 1 of 1

Use of timers

Posted: Wed Nov 20, 2019 2:38 pm
by jk007
Can I use the timer of pyb1.1 as the fixed length controller? I have a 1.8 ° stepper motor and stepper motor driver. I send 200 pulses to the driver. The stepper motor can rotate 360 °. I want to concede that the stepper motor will stop after 3.5 turns (700 pulses are needed). Can pyb1.1 be realized? Thank!

Re: Use of timers

Posted: Wed Nov 20, 2019 5:19 pm
by pythoncoder
It might be simplest not to use a timer: I'm not sure if there's a way to get a timer to emit an exact number of pulses.

Code: Select all

import pyb
from time import ticks_us, ticks_diff
def rats():
    t = ticks_us()
    for _ in range(700):
    print(ticks_diff(ticks_us(), t))
executes in 6.8ms. If your stepper motor can turn that quickly. It's likely you'll have to deliberately slow this down, or ramp the speed up and down at the end of the rotation to enable the inertia of the rotor to respond properly.

Re: Use of timers

Posted: Thu Nov 21, 2019 1:42 am
by jk007
It's because of the need for accuracy that I think of using timers. I'll try to find out if there's a good solution

Re: Use of timers

Posted: Thu Nov 21, 2019 3:19 am
by jimmo
Another option is to use the hardware SPI (or UART) to transmit a precise waveform on the MOSI (or TX) pin.

This will give you very precise control of the pulse width, and spacing (and even allow you to vary it over time, allowing for an acceleration profile).

Re: Use of timers

Posted: Thu Nov 21, 2019 7:05 am
by Roberthh
The drawback using SPI is, that it may send word sized bursts of pulses, not a continuous stream of pulses. For many applications that does not matter, but ut has to be considered.

Re: Use of timers

Posted: Thu Nov 21, 2019 12:02 pm
by OutoftheBOTS_
Python has things it is good at and things it is not so good at. Fast real time processes is something python isn't so good at.

Also you will need to include acceleration in the step timings.

I have got a single stepper at a time to work quite well using MicoPython but it isn't a very good way about doing it. see this robot with 3 steppers and 3 servos and this code ...

I am currently working a robot for a comp that uses 4 steppers to drive mecuam wheels with a STM32F4 but I have used C for this code. Basically this is how I do it (also how all CNC machine work). I do use a timer for each stepper motor but the timer doesn't output PWM to a pin but rather when the timer reaches the count in the ARR (auto reload register) then it fires a hardware interrupt. In the interrupt call back I pulse the step pin and calculate the next needed timing for the next pulse based upon the required acceleration and finial required speed.

here is some of the C code that I am working on atm for the 4 steppers ... nch/main.c

Re: Use of timers

Posted: Fri Nov 22, 2019 2:48 pm
by mattyt
If you are able to switch to an ESP32 without much pain, this is one application where the RMT module excels. It provides a way to output a very precise (down to 12.5ns) pattern to a GPIO.

In other words, you could generate a bitstream of exactly 200 pulses with each pulse precisely the width you desire.

A disclaimer though, this feature is still very new to MicroPython and isn't yet on mainline. See PR 5184 for more details. Further, it's worth noting that the current implementation blocks - so won't return until after the 200 pulses have been executed. Non-blocking is provided by the hardware and is relatively easy to implement; I just haven't gotten around to it yet!

Anyway, it's another option to consider. :)