@kidswong999
from pyb import Pin, Timer
inverse_left=False #change it to True to inverse left wheel
inverse_right=False #change it to True to inverse right wheel
IN1 = Pin('P0', Pin.OUT_PP)
IN2 = Pin('P1', Pin.OUT_PP)
IN3 = Pin('P2', Pin.OUT_PP)
IN4 = Pin('P3', Pin.OUT_PP)
IN1.low()
IN2.low()
IN3.low()
IN4.low()
ENA = Pin('P7')
ENB = Pin('P8')
tim = Timer(4, freq=1000)
ch1 = tim.channel(1, Timer.PWM, pin=ENA)
ch2 = tim.channel(2, Timer.PWM, pin=ENB)
ch1.pulse_width_percent(0)
ch2.pulse_width_percent(0)
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:
IN1.low()
IN2.high()
else:
IN1.high()
IN2.low()
ch1.pulse_width_percent(abs(left_speed))
if right_speed < 0:
IN3.low()
IN4.high()
else:
IN3.high()
IN4.low()
ch2.pulse_width_percent(abs(right_speed))
您看一下 这样写对吗
import car
while True:
car.run(100,100)
然后第二个是main.py,第一个是car.py