Use of timers

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
jk007
Posts: 4
Joined: Tue Nov 12, 2019 1:34 am

Use of timers

Post by jk007 » Wed Nov 20, 2019 2:38 pm

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!

User avatar
pythoncoder
Posts: 3915
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Use of timers

Post by pythoncoder » Wed Nov 20, 2019 5:19 pm

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):
        p(1)
        p(0)
    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.
Peter Hinch

jk007
Posts: 4
Joined: Tue Nov 12, 2019 1:34 am

Re: Use of timers

Post by jk007 » Thu Nov 21, 2019 1:42 am

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

User avatar
jimmo
Posts: 1156
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Use of timers

Post by jimmo » Thu Nov 21, 2019 3:19 am

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).

User avatar
Roberthh
Posts: 1837
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Use of timers

Post by Roberthh » Thu Nov 21, 2019 7:05 am

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.

OutoftheBOTS_
Posts: 714
Joined: Mon Nov 20, 2017 10:18 am

Re: Use of timers

Post by OutoftheBOTS_ » Thu Nov 21, 2019 12:02 pm

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 https://www.youtube.com/watch?v=sD4bG8hPYLQ&t=88s and this code https://github.com/OutOfTheBots/5x5x5_c ... stepper.py

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 https://github.com/OutOfTheBots/stepper ... nch/main.c

User avatar
mattyt
Posts: 273
Joined: Mon Jan 23, 2017 6:39 am

Re: Use of timers

Post by mattyt » Fri Nov 22, 2019 2:48 pm

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. :)

Post Reply