OPENMV Line Patrol Turning Code Optimization Version 2022.6.29
I graduated from senior year and I can’t use it any more. I took advantage of my spare time to optimize it these days. The previous account was too complicated.
OPENMV communicates via UART
ROIS is 5 perceptual areas, which are divided into 5 blocks within the range of 160x120, which is convenient for dealing with intersections and no road ahead
largest_blob is the blob module [2] is the color block w, [3] is the color block h, as explained in the official docs
Set out_str to see the output of the serial port
Then delete some variables when they are useful or not. There is still a problem with the logic of this copy.
I use this code for the 2021 drug delivery trolley to reproduce successfully with the K210 digital identification
It can be used directly by copying and pasting. If you can’t use it, reflect on whether you know nothing
import sensor, image, time, math,pyb
from pyb import Pin, Timer,UART
uart = pyb.UART(1,115200,timeout_char =1000)#串口初始化# Tracks a black line. Use [(128, 255)] for a tracking a white line.
Red_Thresholds =[(33,61,16,85,21,69)]
ROIS =[#[ROI, weight]越近,权重越大,在这里权值暂时不考虑(30,90,100,30,0),#下面1(40,40,80,40,0),#中间2(30,0,100,30,0),#上面3 160 *120(0,20,30,100,0),#左边4(130,20,30,100,0)#右边5]
weight_sum =0for r in ROIS: weight_sum += r[4]# r[4] is the roi weight.#---------------------------------------摄像头初始化-----------------------------------------#
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)#320*240
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)# 颜色追踪关闭自动增益
sensor.set_auto_whitebal(False)# 颜色追踪关闭白平衡
flag=0;
i=0;#记录第几行数据
j=0;#记录直线数量
led=pyb.LED(3)#必要的时候进行红外补光
cross_num =0#第几个路口
cross_flag =0#转弯的标志
back_flag =0;#返回的标志
center_flag1 =0#区域标志
center_flag2 =0
center_flag3 =0
center_flag4 =0
center_flag5 =0
out_str1='';
clock = time.clock()#帧率
old_img= sensor.snapshot().mean_pooled(4,4)# 160x120 -> 40x30,前一张照片#定义一个定时发送数据的函数deftick(timer):#we will receive the timer object when being calledglobal flag
flag=1
tim = Timer(4,freq=100)# create a timer object using timer 4 - trigger at 1Hz
tim.callback(tick)# set the callback to our tick function#--------------------------------------while循环开始-----------------------------------------#while(True):
pyb.LED(1).on()if(flag==1):
img=sensor.snapshot()
img.lens_corr(1.5)# for 2.8mm lens...摄像头畸变纠正#--------------------------------------寻找红色色块的位置--------------------------------------##检测色块位置for r in ROIS:
i=i+1;
blobs=img.find_blobs(Red_Thresholds, roi=r[0:4], merge=True,pixels_area=10)# r[0:4] is roi tuple.if blobs:#如果找到了颜色块# Find the blob with the most pixels.
largest_blob =max(blobs, key=lambda b: b.pixels())#print("0", largest_blob[0])#print("1", largest_blob[1])#print("2", largest_blob[2])#print("3", largest_blob[3])if(i==1):#下面矩形if(largest_blob[2]>=5):#排除瑕疵点if(largest_blob[3]>=5):
center_flag1=1;#下面的矩形找到的标志
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(),largest_blob.cy(),2)elif(i==2):#中间矩形if(largest_blob[2]>=5):if(largest_blob[3]>=5):
center_flag2=1;
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(),largest_blob.cy(),2)elif(i==3):#上面的矩形
center_flag3=2;#有找到,但是不符合要求if(largest_blob[2]>=5):if(largest_blob[3]>=5):
center_flag3=1;
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(),largest_blob.cy(),2)elif(i==4):#左边的矩形找到了if(largest_blob[2]>=5):if(largest_blob[3]>=5):
center_flag4=1;
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(),largest_blob.cy(),2)elif(i==5):#右边的矩形找到了if(largest_blob[2]>=5):if(largest_blob[3]>=5):
center_flag5=1;
img.draw_rectangle(largest_blob.rect())
img.draw_cross(largest_blob.cx(),largest_blob.cy(),2)#--------------------------------------遇到的路口--------------------------------------#if(center_flag2>0and center_flag4>0and center_flag5>0):
cross_num = cross_num +1#第几个路口#--------------------------------------前方无路--------------------------------------#if(cross_num>0and center_flag2==0and center_flag4==0and center_flag5==0and center_flag3==0):
back_flag =1
i=0;
out_str1+='%.d'%int(center_flag1);#直角标志位
out_str1+='%.d'%int(center_flag2);#直角标志位
out_str1+='%.d'%int(center_flag3);#直角标志位
out_str1+='%.d'%int(center_flag4);#直角标志位
out_str1+='%.d'%int(center_flag5);#直角标志位
out_str1+='%.4d'%int(cross_num);#路口标志位
out_str1+='%.d'%int(back_flag);#返回标志位
uart.write('s'+out_str1+'#')print(out_str1)#像素位移之和清零
turn_flag=0;
center_flag1 =0#区域标志
center_flag2 =0
center_flag3 =0
center_flag4 =0
center_flag5 =0#数组清零
out_str1=''#清除之前的数据
flag=0;#-----------------------------------串口打印数据-----------------------------------------#