import sensor, time
from pyb import LED
import math
from pyb import UART #串口模块导入
#定义串口
uart = UART(1,115200) #串口配置
uart.init(115200, bits=8, parity=None, stop=1)
# 设置相机
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA) # 800 600
#sensor.skip_frames(time = 333 )#跳过100张图片
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
sensor.set_hmirror(True)#水平方向翻转
sensor.set_vflip(True)#垂直方向翻转
#clock = time.clock() # Create a clock object to track the FPS.
#sensor.set_hmirror(True)
#pin1 = Pin('D8', Pin.IN, Pin.PULL_UP)
#初始化每个格子的标志变量
flag1 = 0
flag2 = 0
flag3 = 0
flag4 = 0
flag5 = 0
flag6 = 0
flag7 = 0
flag8 = 0
flag9 = 0
rx_buff=[]
#标志位2
a = [1]*18
red_led = LED(1)
clock = time.clock()
CONXX = 1
Board_Flag = False
black = [(149, 199)]
white = [(194, 0)]
gray = [(38, 255)]
players = ['X', 'O']
x=0
lu_x=640-x
lu_y=480-x
rd_x=2*x
rd_y=2*x
roi1 = [200,90,285,275] #巡线敏感区
#def sending_data(cx,cy,c):
# global uart;
# data = ustruct.pack("<bbhhhb",
# 0x2C,
# 0x12,
# int(cx),
# int(cy),
# 0,
# 0x5B)
# uart.write(data);
########串口接收数据函数处理#########
def Receive_Prepare(): #data
global state
global x
global tx_flag
global data
global Find_Task
global Target_Num
if state==0:
data[0]=uart.readchar()
if data[0] == 0x0d:#帧头
state = 1
else:
state = 0
rx_buff.clear()
elif state==1:
data[1]=uart.readchar()
#rx_buff.append(data)
Target_Num=data[x+1]
state = 2
elif state==2:
data[2]=uart.readchar()
#rx_buff.append(data)
Find_Task=data[x+2]
state = 3
elif state == 3:
data[4]=uart.readchar()
if data[4] == 0x5b:
tx_flag = int(data[0])
state = 4
elif state == 4:
state=0
else:
state = 0
rx_buff.clear()
# 定义黑框颜色的阈值
thresholds = ((115, 0))
openmv_flag=6
def draw_chess(x,y):
#(197, 255)白色
#(255, 98)黑色
a=80#黑色
b=170#白色
pixel = img.get_pixel(x, y)
#print(f"坐标: ({x}, {y}), 像素值: {pixel}")
if pixel is None: # 处理无效像素
return -1
if pixel<a:
#img.draw_circle(x,y,15,thickness=3)
#print("黑色棋子")
flag=1
elif pixel>b:
#img.draw_circle(x,y,15,thickness=3)
#print("白色棋子")
flag=2
else:
#print("无效棋子")
flag=0
return flag
def find_box_corners(edges):
if len(edges) < 1:
return None
# 初始化四个角点的坐标
top_left = edges[0]
top_right = edges[0]
bottom_left = edges[0]
bottom_right = edges[0]
for edge in edges:
x, y = edge
# 找到最左上角的角点
if x + y < top_left[0] + top_left[1]:
top_left = edge
# 找到最右上角的角点
if x - y > top_right[0] - top_right[1]:
top_right = edge
# 找到最左下角的角点
if x - y < bottom_left[0] - bottom_left[1]:
bottom_left = edge
# 找到最右下角的角点
if x + y > bottom_right[0] + bottom_right[1]:
bottom_right = edge
return top_left, top_right, bottom_left, bottom_right
# 定义控制舵机缓慢移动到目标角度的函数
#lcd = display.SPIDisplay() #屏幕显示
while(True):
# 拍摄图像
#clock.tick() # Update the FPS clock.
img = sensor.snapshot()
#img.lens_corr(strength=1.8, zoom=1)
img.draw_rectangle(lu_x,lu_y,rd_x,rd_y)
# 找到黑框区域
blobs = img.find_blobs([thresholds], roi=roi1, pixels_threshold=5000, area_threshold=100,merge=True)
if not blobs: # 检查是否为空
continue # 跳过当前帧
# 如果找到黑框
if blobs:
# 绘制黑框的边界
img.draw_edges(blobs[0].min_corners(), thickness=3)
# 获取角点坐标
edges = blobs[0].min_corners()
if edges is None or len(edges) < 4: # 确保有至少4个角点
continue
corners = find_box_corners(edges)
if corners is None:
continue
top_left, top_right, bottom_left, bottom_right = corners
# 检查角点是否成功获取
if top_left is not None:
# 绘制角点
#img.draw_cross(top_left[0], top_left[1]) # 左上角
#img.draw_cross(top_right[0], top_right[1]) # 右上角
#img.draw_cross(bottom_left[0], bottom_left[1]) # 左下角
#img.draw_cross(bottom_right[0], bottom_right[1]) # 右下角
Board_Flag== True
# 打印四个角点的坐标
#print("Top left:", top_left)
#print("Top right:", top_right)
#print("Bottom left:", bottom_left)
#print("Bottom right:", bottom_right)
point_x=[top_left[0],top_right[0],bottom_left[0],bottom_right[0]]
point_y=[top_left[1],top_right[1],bottom_left[1],bottom_right[1]]
length=math.sqrt((top_left[0]-top_right[0])**2+(top_left[1]-top_right[1])**2)
theta=math.atan2((top_left[1]-top_right[1]),(top_left[0]-top_right[0]))
x_step=math.cos(theta)*length/3
y_step=math.sin(theta)*length/3
p5_x=int((max(point_x)+min(point_x))/2)
p5_y=int((max(point_y)+min(point_y))/2)
wc5_x=(p5_x - p5_x) + 100
wc5_y=(p5_y - p5_y) + 100
img.draw_string(p5_x, p5_y, "5", color=(0,0,0), scale=3)#区域1
FH5 =bytearray([0x2C,0x12,5,wc5_x,wc5_y,0x5B])
p4_x=int(p5_x+x_step)
p4_y=int(p5_y+y_step)
wc4_x=p4_x - p5_x + 100
wc4_y=p4_y - p5_y + 100
img.draw_string(p4_x, p4_y, "4", color=(0,0,0), scale=3)#区域4
FH4 = bytearray([0x2C,0x12,4,wc4_x,wc4_y,0x5B])
p6_x=int(p5_x-x_step)
p6_y=int(p5_y-y_step)
img.draw_string(p6_x, p6_y, "6", color=(0,0,0), scale=3)#区域6
wc6_x=p6_x - p5_x + 100
wc6_y=p6_y - p5_y + 100
FH6 = bytearray([0x2C,0x12,6,wc6_x,wc6_y,0x5B])
p2_x=int(p5_x-y_step)
p2_y=int(p5_y+x_step)
img.draw_string(p2_x, p2_y, "2", color=(0,0,0), scale=3)#区域2
wc2_x=p2_x - p5_x + 100
wc2_y=p2_y - p5_y + 100
FH2 = bytearray([0x2C,0x12,2,wc2_x,wc2_y,0x5B])
p8_x=int(p5_x+y_step)
p8_y=int(p5_y-x_step)
img.draw_string(p8_x, p8_y, "8", color=(0,0,0), scale=3)#区域8
wc8_x=p8_x - p5_x + 100
wc8_y=p8_y - p5_y + 100
FH8 = bytearray([0x2C,0x12,8,wc8_x,wc8_y,0x5B])
#uart.write(FH8)
p1_x=int(p2_x+x_step)
p1_y=int(p2_y+y_step)
img.draw_string(p1_x, p1_y, "1", color=(0,0,0), scale=3)#区域1
wc1_x=p1_x - p5_x + 100
wc1_y=p1_y - p5_y + 100
FH1 = bytearray([0x2C,0x12,1,wc1_x,wc1_y,0x5B])
p3_x=int(p2_x-x_step)
p3_y=int(p2_y-y_step)
img.draw_string(p3_x, p3_y, "3", color=(0,0,0), scale=3)#区域3
wc3_x=p3_x - p5_x + 100
wc3_y=p3_y - p5_y + 100
FH3 = bytearray([0x2C,0x12,3,wc3_x,wc3_y,0x5B])
p7_x=int(p8_x+x_step)
p7_y=int(p8_y+y_step)
img.draw_string(p7_x, p7_y, "7", color=(0,0,0), scale=3)#区域7
wc7_x=p7_x - p5_x + 100
wc7_y=p7_y - p5_y + 100
FH7 = bytearray([0x2C,0x12,7,wc7_x,wc7_y,0x5B])
p9_x=int(p8_x-x_step)
p9_y=int(p8_y-y_step)
img.draw_string(p9_x, p9_y, "9", color=(0,0,0), scale=3)#区域9
wc9_x=p9_x - p5_x + 100
wc9_y=p9_y - p5_y + 100
FH9 = bytearray([0x2C,0x12,9,wc9_x,wc9_y,0x5B])
#print("p1_x,p1_y:",p1_x,p1_y)
##########串口发坐标
draw_chess(p1_x,p1_y)
draw_chess(p2_x,p2_y)
draw_chess(p3_x,p3_y)
draw_chess(p4_x,p4_y)
draw_chess(p5_x,p5_y)
draw_chess(p6_x,p6_y)
draw_chess(p7_x,p7_y)
draw_chess(p8_x,p8_y)
draw_chess(p9_x,p9_y)
#黑棋是1,白棋是2
F1 = "PB1PE1PO\n"
F2 = "PB2PE1PO\n"
F3 = "PB3PE1PO\n"
F4 = "PB4PE1PO\n"
F5 = "PB5PE1PO\n"
F6 = "PB6PE1PO\n"
F7 = "PB7PE1PO\n"
F8 = "PB8PE1PO\n"
F9 = "PB9PE1PO\n"
F11 = "PB1PE2PO\n"
F22 = "PB2PE2PO\n"
F33 = "PB3PE2PO\n"
F44 = "PB4PE2PO\n"
F55 = "PB5PE2PO\n"
F66 = "PB6PE2PO\n"
F77 = "PB7PE2PO\n"
F88 = "PB8PE2PO\n"
F99 = "PB9PE2PO\n"
#定义识别时间
time_shijue = 5
#print(draw_chess(p5_x,p5_y))
#串口收到了数据,则接收数据
#if(uart.any()>0):
#Receive_Prepare()
dat = uart.readline()
if dat:
char_dat = dat.decode().strip().lower() # 统一转小写并去除空格/换行
print(char_dat)
if char_dat == "yes" :
CONXX = 1
elif char_dat == "no" :
CONXX = 0
if CONXX == 1 :
if (draw_chess(p1_x,p1_y) == 2) and (a[0]==1):
#白棋
if flag1 < time_shijue :
uart.write(F11)
print(F11)
flag1 = flag1 + 1
if flag1 == time_shijue:
CONXX = 0
a[0]=0
elif (draw_chess(p1_x,p1_y) == 1) and (a[1]==1):
#黑棋
if flag1 < time_shijue :
uart.write(F1)
print(F1)
flag1 = flag1 + 1
if flag1 == time_shijue:
CONXX = 0
a[1]=0
if (draw_chess(p2_x,p2_y) == 2) and (a[2]==1):
#白棋
if flag2 < time_shijue :
uart.write(F22)
print(F22)
flag2 = flag2 + 1
if flag2 == time_shijue:
CONXX = 0
a[2]=0
elif (draw_chess(p2_x,p2_y) == 1) and (a[3]==1):
#黑棋
if flag2 < time_shijue :
uart.write(F2)
print(F2)
flag2 = flag2 + 1
if flag2 == time_shijue:
CONXX = 0
a[3]=0
if (draw_chess(p3_x,p3_y) == 2) and (a[4]==1):
#白棋
if flag3 < time_shijue :
uart.write(F33)
print(F33)
flag3 = flag3 + 1
if flag3 == time_shijue:
CONXX = 0
a[4]=0
elif (draw_chess(p3_x,p3_y) == 1) and (a[5]==1):
#黑棋
if flag3 < time_shijue :
uart.write(F3)
print(F3)
flag3 = flag3 + 1
if flag3 == time_shijue:
CONXX = 0
a[5]=0
if (draw_chess(p4_x,p4_y) == 2) and (a[6]==1):
#白棋
if flag4 < time_shijue :
uart.write(F44)
print(F44)
flag4 = flag4 + 1
if flag4 == time_shijue:
CONXX = 0
a[6]=0
elif (draw_chess(p4_x,p4_y) == 1) and (a[7]==1):
#黑棋
if flag4 < time_shijue :
uart.write(F4)
print(F4)
flag4 = flag4 + 1
if flag4 == time_shijue:
CONXX = 0
a[7]=0
if (draw_chess(p5_x,p5_y) == 2) and (a[8]==1):
#白棋
if flag5 < time_shijue :
uart.write(F55)
print(F55)
flag5 = flag5 + 1
if flag5 == time_shijue:
CONXX = 0
a[8]=0
elif (draw_chess(p5_x,p5_y) == 1) and (a[9]==1):
#黑棋
if flag5 < time_shijue :
uart.write(F5)
print(F5)
flag5 = flag5 + 1
if flag5 == time_shijue:
CONXX = 0
a[9]=0
if (draw_chess(p6_x,p6_y) == 2) and (a[10]==1):
#白棋
if flag6 < time_shijue :
uart.write(F66)
print(F66)
flag6 = flag6 + 1
if flag6 == time_shijue:
CONXX = 0
a[10]=0
elif (draw_chess(p6_x,p6_y) == 1) and (a[11]==1):
#黑棋
if flag6 < time_shijue :
uart.write(F6)
print(F6)
flag6 = flag6 + 1
if flag6 == time_shijue:
CONXX = 0
a[11]=0
if (draw_chess(p7_x,p7_y) == 2) and (a[12]==1):
#白棋
if flag7 < time_shijue :
uart.write(F77)
print(F77)
flag7 = flag7 + 1
if flag7 == time_shijue:
CONXX = 0
a[12]=0
elif (draw_chess(p7_x,p7_y) == 1) and (a[13]==1):
#黑棋
if flag7 < time_shijue :
uart.write(F7)
print(F7)
flag7 = flag7 + 1
if flag7 == time_shijue:
CONXX = 0
a[13]=0
if (draw_chess(p8_x,p8_y) == 2) and (a[14]==1):
#白棋
if flag8 < time_shijue :
uart.write(F88)
print(F88)
flag8 = flag8 + 1
if flag8 == time_shijue:
CONXX = 0
a[14]=0
elif (draw_chess(p8_x,p8_y) == 1) and (a[15]==1):
#黑棋
if flag8 < time_shijue :
uart.write(F8)
print(F8)
flag8 = flag8 + 1
if flag8 == time_shijue:
CONXX = 0
a[15]=0
if (draw_chess(p9_x,p9_y) == 2) and (a[16]==1):
#白棋
if flag9 < time_shijue :
uart.write(F99)
print(F99)
flag9 = flag9 + 1
if flag9 == time_shijue:
CONXX = 0
a[16]=0
elif (draw_chess(p9_x,p9_y) == 1) and (a[17]==1):
#黑棋
if flag9 < time_shijue :
uart.write(F9)
print(F9)
flag9 = flag9 + 1
if flag9 == time_shijue:
CONXX = 0
a[17]=0
elif CONXX == 0:
print("待机中\n")