# open mv主代码
# 必须先扫描二维码,再识别颜色,返回抓取顺序
# uart 通讯
import sensor, image, time, pyb
from pyb import UART
ROI = (20, 120, 280, 100)
red_threshold = (47, 68, 55, 103, 25, 63) # red_thresholds
green_threshold = (45, 65, -50, -30, 0, 40)# green_thresholds
blue_threshold = (28, 81, -52, 81, -29, -87)# blue_thresholds
#红绿蓝色环阈值
objthresholds=[(42, 76, 5, 35, -3, 88),
(53, 66, -23, -5, 17, -5),
(49, 61, -12, 18, -27, -3)]
graythreshold=[(100,255)]#二值化阈值
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
red_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
uart = pyb.UART(3,9600)
uart.init(9600, bits=8, parity=0, stop=2, timeout_char = 500)
clock = time.clock()
qrcode1='' #后文第一层二维码所用参数
qrcode2='' #后文第二层二维码所用参数
RGB_Order_F1=['0','0','0'] #第一层物块的排列顺序
RGB_Order_F2=['0','0','0'] #第二层物块的排列顺序
qr_code_F1=['0','0','0'] #二维码要求第一层抓取的顺序,默认为RGB
qr_code_F2=['0','0','0'] #二维码要求第二层抓取的顺序,默认为RGB
sehuan_order_F1=['0','0','0']#第一层色环顺序
sehuan_order_F2=['0','0','0']#第二层色环顺序
Put_Order1=0#放置顺序参数(一层)
Put_Order2=0#放置顺序参数(二层)
put_place1=''#放置顺序参数(一层)
put_place2=''#放置顺序参数(二层)
put_number1=0#放置顺序参数(一层)
put_number2=0#放置顺序参数(二层)
a=0#a为将前三和后三个物块放置顺序整合的一个整数
r=0
g=0
b=0
flag=0
m=n=0#第一层二维码和物块顺序综合分析的抓取顺序所需参数
i=j=0#第二层二维码和物块顺序综合分析的抓取顺序所需参数
p=q=0#靶环顺序和分析放置顺序所需参数
first_one1=0
second_one1=0 #第一层二维码和物块顺序综合分析的抓取顺序所需参数
third_one1=0
first_one2=0
second_one2=0 #第二层二维码和物块顺序综合分析的抓取顺序所需参数
third_one2=0
first_one3=0
second_one3=0 #第一层靶环顺序和分析放置顺序所需参数
third_one3=0
first_one4=0
second_one4=0 #第二层靶环顺序和分析放置顺序所需参数
third_one4=0
#flag_w=1
flag_find_r1=0
flag_find_g1=0
flag_find_b1=0
flag_find_r2=0
flag_find_g2=0
flag_find_b2=0
flag_sehuan_r=0
flag_sehuan_g=0
flag_sehuan_b=0
order1=0 #寻找第一层色块的顺序所需参数
order2=0 #寻找第二层色块的顺序所需参数
sehuan=0#色环顺序
uart_receive=0
time.sleep(500)
#接收数据定义
while(True):
if uart.any():
uart_respond = uart.readline().decode()
uart_receive = int(uart_respond)
print(uart_receive)
uart = pyb.UART(3,9600)
clock = time.clock()
uart.init(9600, bits=8, parity=0, stop=2, timeout_char = 500)
##################################################################################################
#扫描二维码部分 qrcode1=0 qrcode2=0
#输入:uart输入1
#输出:二维码内容代码: 'a':123(搬运·顺序也如此)
# 'b':132
# ‘c':213
# 'd’:231
# 'e':312
# 'f':321
#################################################################################################
while (uart_receive==1):
img = sensor.snapshot()
img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
for code in img.find_qrcodes():
img.draw_rectangle(code.rect(), color = (255, 0, 0))
one,two=code.payload().split('+');#one和two分别对应第一次和第二次的抓取顺序
if one=='123':
qrcode1='a'
qr_code_F1=['R','G','B']
#uart.write('a')
#print('a');
elif one=='132':
qrcode1='b'
qr_code_F1=['R','B','G']
#uart.write('b')
#print('b');
elif one=='213':
qrcode1='c'
qr_code_F1=['G','R','B']
#uart.write('c')
#print('c');
elif one=='231':
qrcode1='d'
qr_code_F1=['G','B','R']
#uart.write('d')
#print('d');
elif one=='312':
qrcode1='e'
qr_code_F1=['B','R','G']
#uart.write('e')
#print('e');
elif one=='321':
qrcode1='f'
qr_code_F1=['B','G','R']
#uart.write('f')
#print('f');
if two=='123':
qrcode2='g'
qr_code_F2=['R','G','B']
#uart.write('g')
#print('g');
elif two=='132':
qrcode2='h'
qr_code_F2=['R','B','G']
#uart.write('h')
#print('h');
elif two=='213':
qrcode2='i'
qr_code_F2=['G','R','B']
#uart.write('i')
#print('i');
elif one=='231':
qrcode2='j'
qr_code_F2=['G','B','R']
#uart.write('j')
#print('j');
elif two=='312':
qrcode2='k'
qr_code_F2=['B','R','G']
#uart.write('k')
#print('k');
elif two=='321':
qrcode2='l'
qr_code_F2=['B','G','R']
#uart.write('l')
#print('l');
uart.write(qrcode1,qrcode2)
print(qrcode1+qrcode2)
print('扫描完毕')
red_led.on()
green_led.off()
blue_led.off()
uart_receive=0
##################################################################################################
#扫描二维码部分(第二层顺序) qrcode2=0
#输入:uart输入2
#输出:二维码内容代码: 'g':123(搬运·顺序也如此)
# 'h':132
# 'i':213
# 'j':231
# 'k':312
# 'l':321
#################################################################################################
while (uart_receive==2):
img = sensor.snapshot()
img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
for code in img.find_qrcodes():
img.draw_rectangle(code.rect(), color = (255, 0, 0))
one,two=code.payload().split('+');#one和two分别对应第一次和第二次的抓取顺序
if two=='123':
qrcode2=1
qr_code_F2=['R','G','B']
uart.write('g')
print('g');
elif two=='132':
qrcode2=2
qr_code_F2=['R','B','G']
uart.write('h')
print('h');
elif two=='213':
qrcode2=3
qr_code_F2=['G','R','B']
uart.write('i')
print('i');
elif one=='231':
qrcode2=4
qr_code_F2=['G','B','R']
uart.write('j')
print('j');
elif two=='312':
qrcode2=5
qr_code_F2=['B','R','G']
uart.write('k')
print('k');
elif two=='321':
qrcode2=6
qr_code_F2=['B','G','R']
uart.write('l')
print('l');
red_led.on()
green_led.off()
blue_led.off()
uart_receive=0
##################################################################################################
#寻找第一层色块的顺序 order1=0 flag_find_r1=0 flag_find_g1=0 flag_find_b1=0
#输入:uart输入3
#输出:一层物块摆放顺序:一层:RGB_Order_F1=['R','G','B'](以其中一种可能性为例)
#################################################################################################
while (uart_receive==3):
img = sensor.snapshot().lens_corr(1.8)
for blob in img.find_blobs([red_threshold], pixels_threshold=800, area_threshold=800):
if flag_find_r1==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#r=blob.cx()
RGB_Order_F1[order1]='R'
order1=order1+1
flag_find_r1=1
red_led.on()
green_led.off()
blue_led.off()
else:
#pass
print("i have already find the r")
#print("1111111111111");
#print(r);
for blob in img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200):
if flag_find_g1==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#g=blob.cx()
RGB_Order_F1[order1]='G'
order1=order1+1
flag_find_g1=1
red_led.off()
green_led.on()
blue_led.off()
else:
#pass
print("i have already find the g")
#print("222222222222()");
#print(g);
for blob in img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200):
if flag_find_b1==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#b=blob.cx()
RGB_Order_F1[order1]='B'
order1=order1+1
flag_find_b1=1
red_led.off()
green_led.off()
blue_led.on()
else:
#pass
print("i have already find the b")
#print("3333333333333");
#print(b);
print(RGB_Order_F1)
if order1==3 :
uart_receive=0;
##################################################################################################
#寻找第二层色块的顺序 order2=0 flag_find_r2=0 flag_find_g2=0 flag_find_b2=0
#输入:uart输入4
#输出:二层物块摆放顺序:二层:RGB_Order_F2=['R','G','B'](以其中一种可能性为例)
#################################################################################################
while (uart_receive==4):
img = sensor.snapshot().lens_corr(1.8)
for blob in img.find_blobs([red_threshold], pixels_threshold=800, area_threshold=800):
if flag_find_r2==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#r=blob.cx()
RGB_Order_F2[order2]='R'
order2=order2+1
flag_find_r2=1
red_led.on()
green_led.off()
blue_led.off()
else:
#pass
print("i have already find the r")
#print("1111111111111");
#print(r);
for blob in img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200):
if flag_find_g2==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#g=blob.cx()
RGB_Order_F2[order2]='G'
order2=order2+1
flag_find_g2=1
red_led.off()
green_led.on()
blue_led.off()
else:
#pass
print("i have already find the g")
#print("222222222222()");
#print(g);
for blob in img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200):
if flag_find_b2==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#b=blob.cx()
RGB_Order_F2[order2]='B'
order2=order2+1
flag_find_b2=1
red_led.off()
green_led.off()
blue_led.on()
else:
#pass
print("i have already find the b")
#print("3333333333333");
#print(b);
print(RGB_Order_F2)
if order2==3 :
uart_receive=0
##################################################################################################
#第一层二维码和物块顺序综合分析的抓取顺序 m=n=0 first_one1=0 second_one1=0 third_one1=0
#输入:uart输入5
#输出:实际动作顺序: 'p':实际动作:左 中 右
# 'q':实际动作:左 右 中
# 'r':实际动作:中 左 右
# 's':实际动作:中 右 左
# 't':实际动作:右 左 中
# 'u':实际动作:右 中 左
#################################################################################################
while(uart_receive==5):
for m in range(3):
for n in range(3):
if(qr_code_F1[m]==RGB_Order_F1[n]):
if m==0:
first_one1=(m+1)*10+(n+1)
elif m==1:
second_one1=(m+1)*10+(n+1)
elif m==2:
third_one1=(m+1)*10+(n+1)
Get_Order1=first_one1%10*100+second_one1%10*10+third_one1%10#抓取顺序
print(Get_Order1)
print("Doen duibiwukuai")
uart_receive=0
if Get_Order1==123:#抓取顺序为左,中,右
uart.write('g')
#break
elif Get_Order1==132:#抓取顺序为左,右,中
uart.write('h')
#break
elif Get_Order1==213:#抓取顺序为中,左,右
uart.write('i')
#break
elif Get_Order1==231:#抓取顺序为中,右,左
uart.write('j')
#break
elif Get_Order1==312:#抓取顺序为右,左,中
uart.write('k')
#break
elif Get_Order1==321:#抓取顺序为右,中,左
uart.write('l')
##################################################################################################
#第二层二维码和物块顺序综合分析的抓取顺序 i=j=0 first_one2=0 second_one2=0 third_one2=0
#输入:uart输入6
#输出:实际动作顺序: 'p':实际动作:左 中 右
# 'q':实际动作:左 右 中
# 'r':实际动作:中 左 右
# 's':实际动作:中 右 左
# 't':实际动作:右 左 中
# 'u':实际动作:右 中 左
#################################################################################################
while(uart_receive==6):
for i in range(3):
for j in range(3):
if(qr_code_F2[i]==RGB_Order_F2[j]):
if i==0:
first_one2=(i+1)*10+(j+1)
elif i==1:
second_one2=(i+1)*10+(j+1)
elif i==2:
third_one2=(i+1)*10+(j+1)
Get_Order2=first_one2%10*100+second_one2%10*10+third_one2%10#抓取顺序
print(Get_Order2)
print("Doen duibiwukuai")
uart_receive=0
if Get_Order2==123:#抓取顺序为左,中,右
uart.write('g')
#break
elif Get_Order2==132:#抓取顺序为左,右,中
uart.write('h')
#break
elif Get_Order2==213:#抓取顺序为中,左,右
uart.write('i')
#break
elif Get_Order2==231:#抓取顺序为中,右,左
uart.write('j')
#break
elif Get_Order2==312:#抓取顺序为右,左,中
uart.write('k')
#break
elif Get_Order2==321:#抓取顺序为右,中,左
uart.write('l')
##################################################################################################
#靶环顺序和分析放置顺序 a=0 sehuan=0#色环顺序 p=q=x=y=0 sehuan_order_F1=['0','0','0']
#输入:uart输入7 first_one3=0 second_one3=0 third_one3=0
#输出:靶环颜色: 'x':红色 Put_Order1=0 Put_Order2=0
# 'y':绿色 put_number1=0 put_number2=0
# 'z':蓝色 put_place1='' put_place2=''
##################################################################################################
while(uart_receive==7):
img = sensor.snapshot().lens_corr(1.8)
img.binary([objthresholds[0]])
img.dilate(2)
for blob in img.find_blobs(graythreshold, roi=ROI, pixels_threshold=200, area_threshold=200):
if flag_sehuan_r==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#uart.write('x')
flag_sehuan_r=1
sehuan_order_F1[sehuan]='R'
sehuan+=1
print('x')
red_led.on()
green_led.off()
blue_led.off()
else:
print("finded red_huan")
img.binary([objthresholds[1]])
img.dilate(2)
for blob in img.find_blobs(graythreshold, roi=ROI, pixels_threshold=200, area_threshold=200):
if flag_sehuan_g==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#uart.write('y')
c=0
sehuan_order_F1[sehuan]='G'
flag_sehuan_g=1
sehuan+=1
#g=blob.cx()
print('y')
red_led.off()
green_led.on()
blue_led.off()
else:
print("fined green_huan")
img.binary([objthresholds[2]])
img.dilate(2)
for blob in img.find_blobs(graythreshold, roi=ROI,pixels_threshold=200, area_threshold=200):
if flag_sehuan_b==0:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
#uart.write('z')
flag_sehuan_b=1
sehuan_order_F1[sehuan]='B'
sehuan+=1
#b=blob.cx()
print('z')
red_led.off()
green_led.off()
blue_led.on()
else:
print("fined blue_huan")
if sehuan==3:
print(sehuan_order_F1)
for p in range(3):
for q in range(3):
if(qr_code_F1[p]==sehuan_order_F1[q]):
if p==0:
first_one3=(p+1)*10+(q+1)
elif p==1:
second_one3=(p+1)*10+(q+1)
elif p==2:
third_one3=(p+1)*10+(q+1)
Put_Order1=first_one3%10*100+second_one3%10*10+third_one3%10#放置顺序
#print(Put_Order1)
#print("Doen duibisehuan")
for x in range(3):
for y in range(3):
if(qr_code_F2[x]==sehuan_order_F1[y]):
if x==0:
first_one4=(x+1)*10+(y+1)
elif x==1:
second_one4=(x+1)*10+(y+1)
elif x==2:
third_one4=(x+1)*10+(y+1)
Put_Order2=first_one4%10*100+second_one4%10*10+third_one4%10#放置顺序
#print(Put_Order2)
#print("Doen duibisehuan")
uart_receive=0
if Put_Order1==123:#放置顺序为左,中,右
put_number1=1
put_place1='m'
#uart.write('m')
#break
elif Put_Order1==132:#放置顺序为左,右,中
put_number1=2
put_place1='n'
#uart.write('n')
#break
elif Put_Order1==213:#放置顺序为中,左,右
put_number1=3
put_place1='o'
#uart.write('o')
#break
elif Put_Order1==231:#放置顺序为中,右,左
put_number1=4
put_place1='p'
#uart.write('p')
#break
elif Put_Order1==312:#放置顺序为右,左,中
put_number1=5
put_place1='q'
#uart.write('q')
#break
elif Put_Order1==321:#放置顺序为右,中,左
put_number1=6
put_place1='r'
#uart.write('r')
if Put_Order2==123:#放置顺序为左,中,右
put_number2=1
put_place2='m'
#uart.write('m')
#break
elif Put_Order2==132:#放置顺序为左,右,中
put_number2=2
put_place2='n'
#uart.write('n')
#break
elif Put_Order2==213:#放置顺序为中,左,右
put_number2=3
put_place2='o'
#uart.write('o')
#break
elif Put_Order2==231:#放置顺序为中,右,左
put_number2=4
put_place2='p'
#uart.write('p')
#break
elif Put_Order2==312:#放置顺序为右,左,中
put_number2=5
put_place2='q'
#uart.write('q')
#break
elif Put_Order2==321:#放置顺序为右,中,左
put_number2=6
put_place2='r'
#uart.write('r')
a=put_number1*10+put_number2#a为将前三和后三个物块放置顺序整合的一个整数
uart.write(a)
print(a)
while(uart_receive==8):
print(clock.fps());
#pyb.hard_reset()看上去是哪里的逻辑卡住了,具体是哪里不知道。
可能是大循环的问题吗
应该是,你可以多加几个print看一下,
这个太长了,实在看不懂。。。