Openmv巡线程序,代码、图片,描述如下,这问题怎么解决?在线等
-
THRESHOLD = (100, 4, 12, 71, 57, -16) # Grayscale threshold for dark things... import sensor, image, time, math, pyb from pyb import LED, UART #import car from pid import PID import json import ustruct rho_pid = PID(p=0.4, i=0) theta_pid = PID(p=0.001, i=0) roi1 = [(0,22,28,16),# 左 x y w h (52,22,28,16),# 右 (28,0,24,20),#上 (0,0,28,22), #左上 (52,0,28,22),# 右上 (0,38,30,18), # 下左 (50,38,30,18)]#下右 led = pyb.LED(3) sensor.reset() #sensor.set_vflip(True) #sensor.set_hmirror(False) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000. #sensor.set_windowing([0,20,80,40]) sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds clock = time.clock() # to process a frame sometimes. uart = UART(3,115200) #定义串口3变量 uart.init(115200, bits=8, parity=None, stop=1) i=0 while(True): clock.tick() led.on() img = sensor.snapshot().binary([THRESHOLD]) line = img.get_regression([(100,100)], robust = True) left_flag,right_flag,up_flag=(0,0,0) for rec in roi1: img.draw_rectangle(rec, color=(255,0,0))#绘制出roi区域 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(),rho_err) if line.magnitude()>6: #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 output_str = "%d" %output if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[0]): #left left_flag=1 else: left_flag=0 if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[1]): #right right_flag=1 else: right_flag=0 if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[2]): #up up_flag=1 else: up_flag=0 if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[3]): #up_left up_left_flag=1 else: up_left_flag=0 if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[4]): #up_right up_right_flag=1 else: up_right_flag=0 if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[5]): #dowm-left dowm_left_flag=1 else: dowm_left_flag=0 if img.find_blobs([(96, 100, -13, 5, -11, 18)],roi=roi1[6]): #dowm-right dowm_right_flag=1 else: dowm_right_flag=0 if (left_flag==1 or dowm_left_flag==1) and (right_flag==1 or dowm_right_flag==1) and up_flag==1 and up_right_flag==0 and up_left_flag==0: num=int(output_str) flag = 1 print('十字路口',num,flag) elif left_flag==1 and up_flag==1 and right_flag==0 and dowm_right_flag==0 and dowm_left_flag==0 and up_right_flag==0 and up_left_flag==0: num=0 flag = 2 print('左转路口',num,flag) elif right_flag==1 and up_flag==1 and left_flag==0 and dowm_right_flag==0 and dowm_left_flag==0 and up_right_flag==0 and up_left_flag==0: num=0 flag = 3 print('右转路口',num,flag) elif right_flag==1 and up_flag==0 and left_flag==1 and up_left_flag==0 and up_right_flag==0: num=0 flag = 4 print('丁字路口',num,flag) elif up_flag==1 and right_flag==0 and left_flag==0 and dowm_right_flag==0 and dowm_left_flag==0: num=int(output_str) flag = 0 print("前进前进前进",num) elif up_flag==1 and right_flag==0 and left_flag==0 and dowm_right_flag==1 and dowm_left_flag==1: num=0 flag = 0 print("克服![0_1690536448916_dbb2ef0a922e74d6c2f1e9e2d3ab21d.jpg](https://fcdn.singtown.com/3eaf92ce-9173-409f-bbac-7499a8508829.jpg) 干扰前进",num) elif up_flag==0 and right_flag==1 and left_flag==0: num=int(output_str) flag = 0 print("偏右",num,flag) elif up_flag==0 and right_flag==0 and left_flag==1: num=int(output_str) flag = 0 print("偏左",num,flag) elif up_flag==1 and up_right_flag==1: num=0 flag = 0 print('前进道路',num,flag) elif up_flag==1 and up_left_flag==1: num=0 flag = 0 print('前进道路',num,flag) elif up_flag==1 and up_right_flag==1 and up_left_flag==1: num=0 flag = 0 print('前进道路',num,flag) elif up_flag==1 and up_left_flag==1: num=0 flag = 0 print('前进道路',num,flag) elif up_flag==1 and up_left_flag==0: num=0 flag = 0 print('左前检测道路',num,flag) elif up_flag==1 and up_right_flag==0: num=0 flag = 0 print('右前检测道路',num,flag) else: num = 0 flag = 0 print("<8",flag) else: num = 101 flag = 0 print("101",flag) #pass data = bytearray([0xb3,0xb3,num,flag,0x5b]) uart.write(data) #print(clock.fps())
图片接线就接了个VCC(5V)、GND和P4(负责向STM32发送数据)
当脱机运行时,如果摄像头被近距离遮挡(我是用手捂住)后,openmv就会闪红灯接着程序挂掉,不遮挡的话就会正常执行
-
-
代码中有个图片标签是我失误整上的,测试时记得删除
-
使用电脑OpenMV IDE,看报错信息。
-
找到问题了,是openmv在刚运行过程中,状态不稳定,读取到了一种未列出的情况,然后导致串口无值传输,就直接报错了,算是我考虑不周,我在我的情况集尾加上else就可以解决了,谢谢佬
-