
# Untitled - By: xiongM - 周日 7月 23 2023
如下,这段代码中,如果我同时使用了这些io和pwm和lcd,那么运行时帧率就为0,如果我把io和pwm去了或者把屏幕刷新去了那么就能正常工作了!!
THRESHOLD = (2, 100, -29, 17, -14, 38) # Grayscale threshold for dark things...
GRAY = (0, 16)
import sensor, image, time, lcd
from pyb import LED
from pyb import Pin, Timer
from pid import PID
#import car
sensor.reset()
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_pixformat(sensor.RGB565) #这里色彩模式可以设置为RGB565或者GRAYSCALE
sensor.set_framesize(sensor.QQQVGA) #如果下面robust = True那么这里的分辨率越小越好,大了花费时间较多
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds
clock = time.clock() # to process a frame sometimes.
lcd.init()
inverse_left=True #change it to True to inverse left wheel
inverse_right=True #change it to True to inverse right wheel
ain1 = Pin('P0', Pin.OUT_PP)
ain2 = Pin('P1', Pin.OUT_PP)
bin1 = Pin('P2', Pin.OUT_PP)
bin2 = Pin('P3', Pin.OUT_PP)
ain1.low()
ain2.low()
bin1.low()
bin2.low()
pwma = Pin('P7')
pwmb = Pin('P8')
tim = Timer(4, freq=1000) #实例化定时器4,并初始化频率为1000hz
ch1 = tim.channel(1, Timer.PWM, pin=pwma)
ch2 = tim.channel(2, Timer.PWM, pin=pwmb)
ch1.pulse_width_percent(0)
ch2.pulse_width_percent(0)
current_left_speed = 0
current_right_speed = 0
def run(left_speed, right_speed):
if abs(left_speed) >= 30 or abs(right_speed) >= 30:
return
#print(left_speed, right_speed)
global current_left_speed
global current_right_speed
current_left_speed = left_speed
current_right_speed = right_speed
if inverse_left==True:
left_speed=(-left_speed)
if inverse_right==True:
right_speed=(-right_speed)
if left_speed < 0:
ain1.low()
ain2.high()
else:
ain1.high()
ain2.low()
ch1.pulse_width_percent(int(abs(left_speed)))
if right_speed < 0:
bin1.low()
bin2.high()
else:
bin1.high()
bin2.low()
ch2.pulse_width_percent(int(abs(right_speed)))
def stop():
speed_left = current_left_speed
speed_right = current_right_speed
run(speed_left * 0.5, speed_right * 0.5)
time.sleep_ms(100)
run(speed_left * 0.5, speed_right * 0.5)
time.sleep_ms(100)
run(speed_left * 0.5, speed_right * 0.5)
time.sleep_ms(100)
run(speed_left * 0.5, speed_right * 0.5)
time.sleep_ms(100)
run(0,0)
ain1.low()
ain2.low()
bin1.low()
bin2.low()
def car_test():
run(50, 50)
time.sleep_ms(1000)
stop()
#run(10,10)
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
while(True):
img = sensor.snapshot().binary([THRESHOLD], invert=True)
img.erode(1)
line = img.get_regression([(100,100)], robust = True)
if(line):
img.draw_line(line.line(), color = 127)
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
rho_err = abs(line.rho())-img.width()/2
#print(theta_err, line.magnitude(), line.rho())
if line.magnitude()>8: #这里理解为回归置信度,数字越大越好
rho_output = rho_pid.get_pid(rho_err, 1)
theta_output = theta_pid.get_pid(theta_err, 1)
output = rho_output+theta_output
run(20+output, 20-output)
#print(output)
else:
#car.run(0,0)
pass
lcd.display(img)LCD和你的电机控制的io引脚冲突,不能一起使用。
请问一下在哪儿看lcd需要用到那些引脚呢?
https://singtown.com/product/636/opeenmv-lcd-shield/
产品页面,有引脚图。