Re: Multiaxis stepper motors using RMT
Posted: Fri Jan 03, 2020 6:20 am
Probably not the most elegant method, but I'm close to the expected frequency with Axis.steps. I'll try the same approach in MultiAxis.
Code: Select all
from machine import Pin
from esp32 import RMT
DRV8825_mp = const(2)
class Axis(object):
id_axis = 0
def __init__(self, dir_pin, step_pin):
self.d_pin = Pin(dir_pin, Pin.OUT)
self.s_pin = RMT(Axis.id_axis, pin=Pin(step_pin), clock_div=80)
self.wp = self.s_pin.write_pulses
Axis.id_axis += 1
self.pos = 0
def set_dir(self, steps):
d = self.d_pin()
if steps < 0 and d:
self.d_pin(0)
elif steps > 0 and not d:
self.d_pin(1)
def steps(self, steps, period=4, p_size=100):
self.set_dir(steps)
d, m = divmod(abs(steps), p_size)
pulse = (DRV8825_mp, max(DRV8825_mp, min(32767, period - DRV8825_mp)))
wp = self.wp
loops = range(d)
for _ in loops:
wp(pulse * p_size)
if m:
wp(pulse * m)
self.pos += steps
x = Axis(dir_pin=15, step_pin=2)
x.steps(steps=100017, period=200)
x.steps(steps=10017, period=2)
x.steps(steps=20017, period=2000)