2021电赛F题openmv巡线,完全开源。

记录下201电赛F题的openmv巡线吧,当时是借鉴了网上有个老哥的想法,也找不到是那个老哥了,抱歉了。本人菜鸟一枚,大佬们有不对的地方请指出。

一、巡线

1.原理

起初是一开始是想用openmv里面的二值化线性回归算法来做
线性回归](https://img-blog.csdnimg.cn/738147ea2bc64d08b7249c49be824f34.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBATXJsaTA1MzA=,size_20,color_FFFFFF,t_70,g_se,x_16)

,但是遇到十字路口之后会抖动一下,所以说就是采用了openMv直接当作巡线传感器来用。

2.巡线传感器

我们所常见的巡线传感器一般都是灰度巡线,具体原理就不介绍了,
在这里插入图片描述
之后赛道是红色线,怎么说呢所以就有了后面的openmv巡线,请看二

二、openmv巡线

openmv巡线谁不会啊,而且官网还有例程,有的哥们会说,但是那时候肯定是有别的主控啊,这玩意,啧啧则,四天三夜哪有时间闹着玩,下面具体说下吧。

1.openmv寻线具体方案

两个通讯方案,建议使用2

1.串行通讯

简单来说就是把openmv变为普通的传感器,额先贴出来代码吧
roi 1-7就是需要检测的位置,根据自己小车安装位置自己调节吧。

import sensor, image, time
#**********PID参数**********#
from pid import PID
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
#**********PID参数**********#
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
s=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=1
  #*****发送的是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()
    s=a_1+a_2+a_3+a_4+a_5+a_6+a_7
    if s>5:#同时识别到了5个,意思就是识别到了横线
      uart.write('sss')

这个相信大家一看就能明白,直接就是提取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判断里面改掉就可以了

猜你喜欢

转载自blog.csdn.net/Mrli0530/article/details/121424387
今日推荐