pyb.Timer problem controlling motor movement via PWM #16184
Unanswered
ChenYujia66
asked this question in
STM32 / Pyboard
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Use the pyb.Timer channel and set the duty cycle to make the motor move, then define an interrupt function to implement counting statistics, and make the duty cycle become 0 after reaching the corresponding position. But now there is a problem, if I start multiple asynchronous tasks, my position will shift forward after the duty cycle becomes 0. What is the cause of this problem? I think it is caused by the task, but there is no solution at present. Can you provide some help?
`isTickTimer = False
isTickTimerTig = False
isTickTimer1 = False
isTickTimer1Tig = False
OneSucceed = False
TwoSucceed = False
p = pyb.Pin('PC6',pyb.Pin.OUT)
tim = pyb.Timer(3)
tim.init(freq=20000,period=6400)
p1 = pyb.Pin('PC7',pyb.Pin.OUT)
tim1 = pyb.Timer(8)
tim1.init(freq=2200,period=400)
ch = tim.channel(1, pyb.Timer.PWM , pin=p)
encoder_count = 0
def encoder_interrupt(pin):
global encoder_count,ch
if encoder_count >= 640000:
ch.pulse_width_percent(0)
encoder_count += 1
encoder_count1 = 0
def encoder_interrupt1(pin):
global encoder_count1
encoder_count1 += 1
async def test111():
global tim,p,p1,tim1,isTickTimer1,isTickTimer,OneSucceed,TwoSucceed,ch
async def check_time1():
global isTickTimerTig,isTickTimer,OneSucceed,accel,decel,p,ch
maxSpeed = 12000
accel = 100
decel = 10000
if isTickTimerTig:
return
isTickTimer = True
isTickTimerTig = True
while isTickTimer:
current_count = encoder_count
print(f"Now pos is {current_count}")
if current_count >= 640000:
# ch.pulse_width_percent(0)
# ch.pulse_width(0)
# tim.deinit()
OneSucceed = True
break
await uasyncio.sleep_ms(1)
isTickTimerTig = False
async def check_time2():
global isTickTimer1Tig,isTickTimer1,TwoSucceed
if isTickTimer1Tig:
return
isTickTimer1Tig = True
isTickTimer1 = True
while isTickTimer1:
current_count = encoder_count1
print(f"two now is {current_count}")
await uasyncio.sleep_ms(2)
isTickTimer1Tig = False`
Beta Was this translation helpful? Give feedback.
All reactions