I am trying to make a pio program on my rpi pico that drives 2 stepper motors at different speeds based on inputs. whenever I try to run this, there is an error in the pio block syntax. I was very confused by the documentation. the 3 commented lines are the ones I cant work out does anyone know the correct syntax?
any help is much appreciated
-rob
import time
from machine import Pin
import rp2
motor_1 = False # To be able to check if a motor is completed
motor_2 = False # - " -
x_last = 0
y_last = 0
drv_ms = 1
motor_steps_per_rev = 200
gear_ratio_x = 1
steps_per_rev_x = motor_steps_per_rev * drv_ms * gear_ratio
gear_ratio_y = 1
steps_per_rev_y = motor_steps_per_rev * drv_ms * gear_ratio
activation_pin = Pin(25, Pin.OUT) # Pin 25 is used to trigger our PIO programs/functions
# and is mandatory to have synchronous activation of motors
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW) # Assembly decorator, sideset pin default Low
# when the program starts.
def run_motor():
wait(1, gpio, 25) # waiting for Pin 25 to activate
pull(block) # wait for FIFO to fill (put), then pull data to OSR
mov(y, osr) # copy OSR data into y
label("loop") # this is a header we jump back to
# if gpio 25 is off (zero), jmp to end
# step pin high
nop() [y] #delay desired amount
# step pin low
nop() [y] #delay desired amount
jmp(x_inc, "loop") #incrament x by 1 and the return to the top of the loop
label("end") # This is a header we can jmp to if gpio 25 is 0.
irq(block, rel(0)) # Signals IRQ handler that all steps have been made and waits for handler to clear the flag (block)
def pio_0_handler(sm): # Motor 1
global motor_1
motor_1 = True
def pio_1_handler(sm): # Motor 2
global motor_2
motor_2 = True
sc_freq = 2_000 # step_counter frequency
# Motor 1 - Pio Block 0
step_pin_1 = Pin(0, Pin.OUT)
dir_pin_1 = Pin(1, Pin.OUT)
sm_0 = rp2.StateMachine(0, # Creates object called sm_0 and binds it to state machine 0 inPIO block 0
run_motor,
freq=sc_freq,
first_set_pin=step_pin_1)
sm_0.irq(pio_0_handler) # Directs interrupts from sm_0 to the interrupt handler pio_0_handler()
# Motor 2 - Pio Block 1
step_pin_2 = Pin(2, Pin.OUT)
dir_pin_2 = Pin(3, Pin.OUT)
sm_1 = rp2.StateMachine(4, run_motor, freq=sc_freq, first_set_pin=step_pin_2)
sm_1.irq(pio_1_handler)
# Activating all state machine
sm_0.active(1), sm_1.active(1)
def motors(x, y): # Feeds the PIO programs and activates them.
global motor_1, motor_2
global x_prev, y prev
motor_1 = False
motor_2 = False
if x != x_prev:
activation_pin.value(0)
sm_0.put(x)
sm_1.put(y)
activation_pin.value(1)
x_prev = x
y prev = y
print("\n### Stepping the steps ###")
print("\nstepping to: " + "\nx:" + str(x_last) + "\ny:" + str(y_last))
while True:
if motor_1 and motor_2:
activation_pin.value(0) # This is active until both processes have signaled that they are done.
return
time.sleep_ms(1)