


import sensor, image, time,car
from pyb import LED
from pid import PID
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_auto_gain(False,gain_db=6.02)
#sensor.set_auto_whitebal(False,rgb_gain_db=(63.4416, 60.2071, 63.7403))#H7 PLUS
sensor.set_auto_whitebal(False,rgb_gain_db=(-5,-6,-3))#H7
'''sensor.set_saturation(3)#饱和度
sensor.set_contrast(3)#对比度
sensor.set_brightness(3)#亮度'''
sensor.skip_frames(time = 2000)
clock = time.clock() # 跟踪FPS帧率
rho_pid = PID(p=0.6, i=0)
theta_pid = PID(p=0.001, i=0)
THRESHOLD= (32, 69, -12, 22, -13, 15)
BLACK=((100,100),[250,255])
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0).mean(2).binary([THRESHOLD])
line = img.get_regression([BLACK[0]],roi=(0,sensor.height()-40,sensor.width(),40),x_stride=2,y_stride=2,pixels_threshold=1,robust = True)
if (line):
rho_err = abs(line.rho())-img.width()/2
if line.theta()>90:
theta_err = line.theta()-180
else:
theta_err = line.theta()
img.draw_line(line.line(), color = 127)
print(rho_err,line.magnitude(),theta_err)
#if -40<b_err<40 and -30<t_err<30:
rho_output = rho_pid.get_pid(rho_err,1)
theta_output = theta_pid.get_pid(theta_err,1)
output = rho_output+theta_output
car.run(30+output, 30-output)
else:
car.run(20,-20)
pass
print(clock.fps(),sensor.get_gain_db())启动后亮度不一致,是因为没有手动设置曝光。参考这里面的代码
https://forum.singtown.com/topic/7098/openmv4plus%E7%9A%84%E5%9B%BE%E5%83%8F%E4%B8%BA%E4%BB%80%E4%B9%88%E8%BF%99%E4%B9%88%E5%81%8F%E6%9A%97%E5%81%8F%E7%BB%BF/7
你串口打印的不是白平衡,是增益get_gain_db, 白平衡用get_rgb_gain_db