基于Openmv的简单巡线(送药小车)
该代码可以识别十字路口,终点线的判断,完成简单巡线的要求,并将数据传给32进行处理
import sensor, image, time,lcd
from pyb import UARTuart = UART(3, 115200, timeout_char=1000)
red_threshold = ((29, 100, 38, 93, -82, 61))#红色阈值
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #选择RGB模式
sensor.set_framesize(sensor.QQVGA) #160*120选择分辨率(分辨率越高帧率可能越低)
sensor.skip_frames(time = 2000)
lcd.init()
sensor.set_hmirror(True)#开启镜像
uart.init(115200,8,None,1) #对串口3给定参数初始化
line_roi1 = (25,5,110,10)#上方
#line_roi2 = (25,30,110,10)#中方
line_roi3 = (25,50,110,10)#下方
flag=0 #0正常巡线,1十字路口,2终点线
xx1=0
xx2=0
# 通信协议
def Send_data_packet(data1,data2,data3):#data1 = xx1,data2 = xx2,data3 = flagx = '%03d'%data1y = '%03d'%data2z = '%01d'%data3data = 'W'+x+y+z+'Z'uart.write(data)#求绝对值
def my_abs(x,y):xx = x - yif(xx>0):return xxelse:return -xxdef run():#巡线函数global flag # 0正常巡线,1十字路口,2终点线global xx1global xx2blobs_low = img.find_blobs([red_threshold], roi = line_roi1,pixels_threshold = 5)#blobs_high = img.find_blobs([red_threshold], roi = line_roi3, pixels_threshold = 5)if(blobs_low):#前方有线if(blobs_low[0].h()*blobs_low[0].w()>=210):#十字路口flag = 1xx1 = 80xx2 = 0print("across")else:#正常巡线img.draw_cross(blobs_low[0].cx(), blobs_low[0].cy(), (0,255,0), 5)#img.draw_cross(blobs_high[0].cx(), blobs_high[0].cy(), (255,255,255), 5)print("line")flag = 0xx1 = blobs_low[0].cx()xx2=0else:#前方无线flag = 4xx1 = 80xx2 = 0print("end")while (True):Send_data_packet(xx1,xx2,flag)img = sensor.snapshot()run()img_black=img.binary([red_threshold])#图像二值化,使图像只有黑白,白色为被追踪的颜色,注意: #要放在色块识别的后面,不然色块识别会出问题lcd.display(img)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!