记录下201电赛F题的openmv巡线吧,当时是借鉴了网上有个老哥的想法,也找不到是那个老哥了,抱歉了。本人菜鸟一枚,大佬们有不对的地方请指出。
一、巡线
1.原理
起初是一开始是想用openmv里面的二值化线性回归算法来做
,但是遇到十字路口之后会抖动一下,所以说就是采用了openMv直接当作巡线传感器来用。
2.巡线传感器
我们所常见的巡线传感器一般都是灰度巡线,具体原理就不介绍了,
之后赛道是红色线,怎么说呢所以就有了后面的openmv巡线,请看二
二、openmv巡线
openmv巡线谁不会啊,而且官网还有例程,有的哥们会说,但是那时候肯定是有别的主控啊,这玩意,啧啧则,四天三夜哪有时间闹着玩,下面具体说下吧。
1.openmv寻线具体方案
两个通讯方案,建议使用2
1.串行通讯
简单来说就是把openmv变为普通的传感器,额先贴出来代码吧
roi 1-7就是需要检测的位置,根据自己小车安装位置自己调节吧。
import sensor, image, time
from pyb import UART
uart = UART(3, 115200)
#**********颜色/ROI阈值**********#
THRESHOLD = ((50, 22, 26, 75, 17, 55)) # Grayscale threshold for dark things...
roi1=(32,100,10,10)
roi2=(96,100,10,10)
roi3=(160,100,10,10)
roi4=(224,100,10,10)
roi5=(288,100,10,10)
roi6=(132,100,10,10)
roi7=(192,100,10,10)
#************变量************#
a_1=a_2=a_3=a_4=a_5=0
s1=s2=s3=0
#**********摄像头参数**********#
sensor.reset()
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000.
#sensor.set_windowing([0,20,80,40])
sensor.set_auto_whitebal(False) # Create a clock object to track the FPS.
sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds
#**********检测前方横线子函数**********#
def tongji():
global a_1
global a_2
global a_3
global a_4
global a_5
global a_6
global a_7
#**********1**********#
statistics1=img.get_statistics(roi=(32,100,5,5))
color_l1=statistics1.l_mode()
print("color_l1:",color_l1)
if color_l1==100:
a_1=1
else:
a_1=0
#**********2**********#
statistics2=img.get_statistics(roi=(96,100,5,5))
color_l2=statistics2.l_mode()
print("color_l2:",color_l2)
if color_l2==100:
a_2=1
else:
a_2=0
#**********3**********#
statistics3=img.get_statistics(roi=(157,100,5,5))
color_l3=statistics3.l_mode()
print("color_l3:",color_l3)
if color_l3==100:
a_3=1
else:
a_3=0
#**********4**********#
statistics4=img.get_statistics(roi=(224,100,5,5))
color_l4=statistics4.l_mode()
print("color_l4:",color_l4)
if color_l4==100:
a_4=1
else:
a_4=0
#**********5**********#
statistics5=img.get_statistics(roi=(288,100,5,5))
color_l5=statistics5.l_mode()
print("color_l5:",color_l5)
if color_l5==100:
a_5=1
else:
a_5=0
#**********6**********#
statistics6=img.get_statistics(roi=(132,100,5,5))
color_l6=statistics6.l_mode()
print("color_l6:",color_l6)
if color_l6==100:
a_6=1
else:
a_6=0
#**********7**********#
statistics7=img.get_statistics(roi=(192,100,5,5))
color_l7=statistics7.l_mode()
print("color_l7:",color_l7)
if color_l7==100:
a_7=1
else:
a_7=0
#*****发送的是7个点的数值*****#类似于7个传感器
print('1a'+str(a_1))
uart.write('1a'+str(a_1)+'\r'+'\n')
print("a_2:",a_2)
uart.write('2a'+str(a_2)+'\r'+'\n')
print("a_3:",a_3)
uart.write('3a'+str(a_3)+'\r'+'\n')
print("a_4:",a_4)
uart.write('4a'+str(a_4)+'\r'+'\n')
print("a_5:",a_5)
uart.write('5a'+str(a_5)+'\r'+'\n')
print("a_6:",a_6)
uart.write('6a'+str(a_6)+'\r'+'\n')
print("a_7:",a_7)
uart.write('7a'+str(a_7)+'\r'+'\n')
while(True):
img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0).binary([THRESHOLD])
img.draw_rectangle(roi1)
img.draw_rectangle(roi2)
img.draw_rectangle(roi3)
img.draw_rectangle(roi4)
img.draw_rectangle(roi5)
img.draw_rectangle(roi6)
img.draw_rectangle(roi7)
tongji()
s1=a_1+a_2+a_3+a_4+a_5+a_6+a_7
if s1>6:#同时识别到了7个,意思就是识别到了横线
uart.write('s1')
else:
s2=a_1+a_2+a_3+a_6
if s2==5:
uart.write('s2') #两种T
s3=a_3+a_4+a_5+a_7
if s3==5:
uart.write('s3') #两种T
这个相信大家一看就能明白,直接就是提取openmv摄像头获取图像的区域内的平均颜色,就是白色和红色,具体的介绍可以看这个 使用统计信息 。
之后用的是7个位置,平均分布,额用得越多他就是会越平滑,越麻烦。5个同时检测到之后发送’sss’,为啥是5个呢,因为小车走的可能会歪,写5个可能会保险点。
2.I/O口
代码检测部分全部一样,就是直接检测到红线之后把openmv引脚拉高或者拉低,完完全全做成一个万能颜色巡线传感器,整体代码就不贴出来了,大家稍微改造,I/O代码在这里
from pyb import Pin
p_out = Pin('P7', Pin.OUT_PP)#设置p_out为输出引脚
p_out.high()#设置p_out引脚为高
p_out.low()#设置p_out引脚为低
p_in = Pin('P7', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
value = p_in.value() # get value, 0 or 1#读入p_in引脚的值
就是把if判断里面改掉就可以了
2021/11/24 Mr.li更新
三、K210调用openmv api来进行巡线
额,话说K210用openmv的api很方便还真香毕竟一块openmv价格都能买两块K210了,废话不多说开干
1.更新固件
在这里下载带openmv字样的固件
之后固件烧到板子
不会烧录的请看这里
2.程序
import sensor,image,lcd
import KPU as kpu
from Maix import FPIOA
from Maix import GPIO
#****************摄像头参数设置************#
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.set_vflip(0)
sensor.set_auto_gain(1,200)
sensor.run(1)
lcd.init(freq=15000000)
lcd.rotation(0)
sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds
#**********颜色/ROI阈值**********#
THRESHOLD = ((50, 22, 26, 75, 17, 55)) # Grayscale threshold for dark things...
roi1=(32,100,10,10)
roi2=(96,100,10,10)
roi3=(160,100,10,10)
roi4=(224,100,10,10)
roi5=(288,100,10,10)
roi6=(132,100,10,10)
roi7=(192,100,10,10)
#************变量************#
a_1=a_2=a_3=a_4=a_5=0
s=0
#***********I/O配置***********#
fpioa = FPIOA()
fpioa.set_function(0,fpioa.GPIOHS1)
led0 = GPIO(GPIO.GPIOHS1,GPIO.OUT)
fpioa.set_function(1,fpioa.GPIOHS2)
led1 = GPIO(GPIO.GPIOHS2,GPIO.OUT)
fpioa.set_function(2,fpioa.GPIOHS3)
led2 = GPIO(GPIO.GPIOHS3,GPIO.OUT)
fpioa.set_function(3,fpioa.GPIOHS6)
led3 = GPIO(GPIO.GPIOHS6,GPIO.OUT)
fpioa.set_function(6,fpioa.GPIOHS7)
led6 = GPIO(GPIO.GPIOHS7,GPIO.OUT)
#***********统计*************#
def tongji():
global a_1
global a_2
global a_3
global a_4
global a_5
global a_6
global a_7
#**********1**********#
statistics1=img.get_statistics(roi=(32,100,5,5))
color_l1=statistics1.l_mode()
print("color_l1:",color_l1)
if color_l1==100:
a_1=1
led0.value(1)
else:
a_1=0
led0.value(0)
#**********2**********#
statistics2=img.get_statistics(roi=(96,100,5,5))
color_l2=statistics2.l_mode()
print("color_l2:",color_l2)
if color_l2==100:
a_2=1
led1.value(1)
else:
a_2=0
led1.value(0)
#**********3**********#
statistics3=img.get_statistics(roi=(157,100,5,5))
color_l3=statistics3.l_mode()
print("color_l3:",color_l3)
if color_l3==100:
a_3=1
led2.value(1)
else:
a_3=0
led2.value(0)
#**********4**********#
statistics4=img.get_statistics(roi=(224,100,5,5))
color_l4=statistics4.l_mode()
print("color_l4:",color_l4)
if color_l4==100:
a_4=1
led3.value(1)
else:
a_4=0
led3.value(0)
#**********5**********#
statistics5=img.get_statistics(roi=(288,100,5,5))
color_l5=statistics5.l_mode()
print("color_l5:",color_l5)
if color_l5==100:
a_5=1
led6.value(1)
else:
a_5=0
led6.value(0)
while(True):
img = sensor.snapshot().binary([THRESHOLD])
img.draw_rectangle(roi1)
img.draw_rectangle(roi2)
img.draw_rectangle(roi3)
img.draw_rectangle(roi4)
img.draw_rectangle(roi5)
img.draw_rectangle(roi6)
img.draw_rectangle(roi7)
tongji()
s=a_1+a_2+a_3+a_4+a_5
if s>5:#同时识别到了5个,意思就是识别到了横线
print('sss')
3.使用
跟其他的传感器一样用,I/O会输出,根据自己小车去调整ROI