logo

驱动无刷电调,pwm值需要用到小数点,但报错TypeError: can't convert float to int

2023-05-15 18:56
Z
kxa2
from pyb import Pin, Timer
pwma = Pin('P7')
pwmb = Pin('P8')
inverse_left=False  #change it to True to inverse left wheel
inverse_right=False #change it to True to inverse right wheel
tim = Timer(4, freq=50)
ch1 = tim.channel(1, Timer.PWM, pin=pwma)
ch2 = tim.channel(2, Timer.PWM, pin=pwmb)
ch1.pulse_width_percent(7.5)
ch2.pulse_width_percent(7.5)
def run(left_speed, right_speed):
    if inverse_left==True:
        left_speed=(-left_speed)
    if inverse_right==True:
        right_speed=(-right_speed)
    if left_speed > 0:
        ch1.pulse_width_percent(abs(left_speed))
    if right_speed > 0:
        ch2.pulse_width_percent(abs(right_speed))
2023-05-16 20:15
I
网红小智智 星瞳科技

可以输出脉宽长度转换一下

https://forum.singtown.com/topic/9575/pwm%E5%8F%A3%E5%8D%A0%E7%A9%BA%E6%AF%94%E5%8F%AA%E8%83%BD%E6%95%B4%E6%95%B0%E5%90%97-%E4%B8%8D%E8%83%BD%E6%98%AF%E5%B0%8F%E6%95%B0/4