2022电赛泊车openmv代码
- 仅作为比赛参考,具体数值请自己调整
- 注释写的很清楚了
import sensor, image, time, math,pyb
from pyb import Pin, Timer,UART
# 串口通信
uart = pyb.UART(1,115200,timeout_char = 1000)#串口初始化
# 跟踪黑色线条(阈值依据实验条件进行更改)
thresholds = (0, 40)
# 五块感性区域
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 = 0
for r in ROIS: weight_sum += r[4] # r[4] is the roi weight.
#---------------------------------------摄像头初始化-----------------------------------------#
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # 灰度图做巡线
sensor.set_framesize(sensor.QQVGA) # 像素160*120
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False) # 颜色追踪关闭自动增益
sensor.set_auto_whitebal(False) # 颜色追踪关闭白平衡
#---------------------------------------标志位和变量-----------------------------------------#
flag = 0
i = 0 #记录第几行数据
led=pyb.LED(3)#必要的时候进行红外补光
center_flag1 = 0 #区域标志
center_flag2 = 0
center_flag3 = 0
center_flag4 = 0
center_flag5 = 0
mid_num = 0
# 串口输出变量
out_str1 = ''
count_RT = 0
RT_tag = 0
T_tag = 0
clock = time.clock()
#定义一个定时发送数据的函数
def tick(timer):#we will receive the timer object when being called
global 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([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())
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):#上面的矩形
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)
#--------------------------------------“|-”型T型路口--------------------------------------#
if(center_flag1>0 and center_flag3>0 and center_flag5>0):
RT_tag = 1
pass
#--------------------------------------“T”型T型路口--------------------------------------#
if(center_flag1>0 and center_flag4>0 and center_flag5>0):
T_tag = 1
pass
#--------------------------------------走到空白路段“|-”型路口计数清除--------------------------------------#
if (center_flag1 == 0 and center_flag2 == 0 and center_flag3 == 0 and center_flag4 == 0 and center_flag5 == 0):
count_RT = 0
pass
#--------------------------------------“|-”型路口计数--------------------------------------#
if(RT_tag > 0):
mid_num += 1
# 识别路口,mid_num变量即控制计数时间,不能过大也不能过小,依据小车速度和环境决定
if(mid_num > 20):
count_RT += 1
mid_num = 0
if(mid_num < 0 ):
mid_num = 0
# 串口发送的字符串: 按照位的顺序
out_str1 += '%.d' % int(RT_tag); # 1.检测到“|-”型T型路口
out_str1 += '%.d' % int(T_tag); # 2.检测到“T”型T型路口
out_str1 += '%.d' % int(count_RT); # 3.“|-”型T型路口计数
# 以s开头#结尾的串口字符串发送,可以去掉s和#
uart.write('s'+out_str1+'#')
print(out_str1)
# 清零标志
center_flag1 = 0 # 区域标志
center_flag2 = 0
center_flag3 = 0
center_flag4 = 0
center_flag5 = 0
i = 0
flag = 0
RT_tag = 0
T_tag = 0
# 串口数组清零
out_str1 = '' # 清除之前的数据
#-----------------------------------串口打印数据-----------------------------------------#
文章来源地址https://www.toymoban.com/news/detail-589981.html
文章来源:https://www.toymoban.com/news/detail-589981.html
到了这里,关于2022电赛泊车openmv代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!