In this video lesson we show how you can work with more than one state machine at the same time in microPython on the Raspberry Pi Pico W. We show this in the context or controlling multiple servos. We show different ways to utilize multiple state machines. For your convenience, we provide the code developed in the video below. Enjoy!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import time from machine import Pin import rp2 @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW,out_shiftdir=rp2.PIO.SHIFT_RIGHT) def servoSet(): wrap_target() mov(x,osr) mov(y,isr) set(pins,0) label('timeLoop') jmp(x_not_y,'nxt') set(pins,1) label('nxt') jmp(y_dec,'timeLoop') wrap() sm0 = rp2.StateMachine(0,servoSet, freq=2000000, set_base=Pin(20)) sm0.active(1) sm0.put(20000) sm0.exec("pull()") sm0.exec("mov(isr,osr)") sm1 = rp2.StateMachine(1,servoSet, freq=2000000, set_base=Pin(21)) sm1.active(1) sm1.put(20000) sm1.exec("pull()") sm1.exec("mov(isr,osr)") while True: for angle in range(0,180,1): pw=int(500+angle*2000/180) sm0.put(pw) sm0.exec("pull()") sm1.put(3000-pw) sm1.exec("pull()") ts=5 time.sleep(ts) for angle in range(180,0,-1): pw=int(500+angle*2000/180) sm0.put(pw) sm0.exec("pull()") sm1.put(3000-pw) sm1.exec("pull()") |