当前位置:网站首页>Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
2022-07-01 01:16:00 【Mrli0530】
Record 201 Electric competition F The question is openmv Patrol the line , At that time, I borrowed the idea of an old brother on the Internet , I can't find the old brother , I'm sorry . I'm a rookie , If there is something wrong with the big guys, please point out .
One 、 Line patrol
1. principle
At first, I wanted to use openmv Inside the binary linear regression algorithm to do ](/img/e8/0140a3041ecf78826586ef8f60e099.png)
, But it will shake after encountering the intersection , Therefore, it adopts openMv It is directly used as a line patrol sensor .
2. Grayscale Sensor
Our common line patrol sensors are generally gray-scale line patrol , The specific principle will not be introduced ,
After that, the track is red , How to say that, so there is the following openmv Line patrol , Look at two
Two 、openmv Line patrol
openmv Who can't patrol , And there are routines on the official website , Some friends will say , But there must have been other controllers at that time , This thing , Tut tut Ze , There is no time for fun in four days and three nights , Let's be specific .
1.openmv Specific route finding scheme
Two communication schemes , It is recommended to use 2
1. Serial communication
To put it simply, put openmv Become a normal sensor , Well, post the code first
roi 1-7 It is the position that needs to be detected , Adjust it by yourself according to the installation position of your car .
import sensor, image, time
from pyb import UART
uart = UART(3, 115200)
#********** Color /ROI threshold **********#
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)
#************ Variable ************#
a_1=a_2=a_3=a_4=a_5=0
s1=s2=s3=0
#********** Camera parameters **********#
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
#********** Detect the front horizontal line subfunction **********#
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
#***** Send the 7 The value of a point *****# Be similar to 7 A sensor
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:# At the same time 7 individual , It means to recognize the horizontal line
uart.write('s1')
else:
s2=a_1+a_2+a_3+a_6
if s2==5:
uart.write('s2') # Two kinds of T
s3=a_3+a_4+a_5+a_7
if s3==5:
uart.write('s3') # Two kinds of T
I believe you can understand this at a glance , Direct is to extract openmv The average color in the area where the camera acquires the image , It's white and red , See this for specific introduction Use statistics .
After that, I used 7 A place , Average distribution , Well, the more you use it, the smoother it will be , The more trouble .5 Send after simultaneous detection ’sss’, Why is 5 A? , Because the car may go askew , Write 5 It may be safer .
2.I/O mouth
The code detection part is all the same , Is to directly detect the red line and then openmv The pins are pulled up or down , Completely made into a universal color line patrol sensor , The whole code will not be posted , Let's make some changes ,I/O The code is here
from pyb import Pin
p_out = Pin('P7', Pin.OUT_PP)# Set up p_out Is the output pin
p_out.high()# Set up p_out Pin is high
p_out.low()# Set up p_out The pin is low
p_in = Pin('P7', Pin.IN, Pin.PULL_UP)# Set up p_in For input pins , And turn on the pull-up resistor
value = p_in.value() # get value, 0 or 1# Read in p_in Pin value
Is to put if It's OK to change it
2021/11/24 Mr.li to update
3、 ... and 、K210 call openmv api To patrol the line
forehead , Words K210 use openmv Of api It's very convenient and really fragrant. After all, it's one piece openmv You can buy two yuan at any price K210 了 , Without further ado, let's get to work
1. Update firmware
stay here Download tape openmv Word firmware 
Then the firmware is burned to the board
If you can't burn, please see here 
2. Program
import sensor,image,lcd
import KPU as kpu
from Maix import FPIOA
from Maix import GPIO
#**************** Camera parameter settings ************#
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
#********** Color /ROI threshold **********#
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)
#************ Variable ************#
a_1=a_2=a_3=a_4=a_5=0
s=0
#***********I/O To configure ***********#
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)
#*********** Statistics *************#
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:# At the same time 5 individual , It means to recognize the horizontal line
print('sss')
3. Use
Same as other sensors ,I/O Will be output , Adjust according to your car ROI
边栏推荐
猜你喜欢

Set different background colors for the border and text of the button

PHP online confusion encryption tutorial sharing + basically no solution

双位置继电器ST2-2L/AC220V

What if the disk of datanode is full?

写给 5000 粉丝的一封信!

Left join displays the specified value when the left join matching data is null

集群与LVS介绍及原理解析

Shift operators

Can JDBC based on openjdk connect to MySQL?

HDU 2488 A Knight's Journey(DFS)
随机推荐
分割链表[先取next再斩断链表防止断链]
Multi graph explanation of resource preemption in yarn capacity scheduling
Vnctf 2022 cm CM1 re reproduction
技术人进阶画业务大图,手把手教学来了
剑指 Offer 19. 正则表达式匹配
写给 5000 粉丝的一封信!
Day31-t1380-2022-02-15-not answer by yourself
NE555 waveform generator handle tutorial NE555 internal structure (I)
对libco的一点看法
Oracle temporary table explanation
DLS-42/6-4 DC110V双位置继电器
Vnctf 2022 cm CM1 re reproduction
Dls-42/6-4 dc110v double position relay
High quality pump SolidWorks model material recommended, not to be missed
The principle of journal node
解析创客教育实践中的智慧原理
ESP8266 RC522
How to do the performance pressure test of "Health Code"
The communication mechanism and extension of Supervisor
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解