改了一下算法,大致是因为类型是float才输出不了的,将err_x转成int型就行了。但是Flag还是无法输出。
用write(Flag)也不行,改成数据用writechar(Flag)也不行
import sensor,time,pyb,math,time,utime,image
from pyb import Pin, Timer, UART
clock = time.clock() # Tracks FPS.
LINE_WIDTH = 10
THRESHOLD = 40
#屏幕尺寸
WIDTH = 160
HEIGHT = 120
ROI = (int((WIDTH-HEIGHT)/2),0,HEIGHT,HEIGHT)
XO = int((WIDTH-HEIGHT)/2)
CENTRAL_AREA = 20
#高度数据
highcnt = 0.0
high = 0
#xy平面误差数据
err_x = 0
err_y = 0
#超声波回调处理函数
def Ultrasound(line):
if(Echo.value()==1):
tim_count.init(prescaler=1799, period=1176)#打开定时器
if(Echo.value()==0):
global highcnt
tim_count.deinit()
highcnt = tim_count.counter()#计数
uart.writechar(0x55)
uart.writechar(0xAA)
uart.writechar(0x10)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(high>>8)
uart.writechar(high)
uart.writechar(err_x>>8)
uart.writechar(err_x)
uart.writechar(err_y>>8)
uart.writechar(err_y)
uart.writechar(Flag)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0x00)
uart.writechar(0xAA)
print(high)
#超声波发射端口配置
timpwm = Timer(4, freq=60) #超声波60赫兹发射频率
ch1 = timpwm.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=80) #100us发射角
#超声波接收端口配置
tim_count = pyb.Timer(1) #定时器计数
extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_DOWN,Ultrasound)#开启外部中断
Echo = Pin('P0', Pin.IN, Pin.PULL_DOWN)
#串口三配置
uart = UART(3, 115200)#115200
uart.init(115200, bits=8, parity=None, stop=1)
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)#设置灰度信息
sensor.set_framesize(sensor.QQVGA)#设置图像大小
#sensor.skip_frames(20)#相机自检几张图片
sensor.set_auto_whitebal(False)#关闭白平衡
while(True):
high = int(1.7*highcnt)
line_x1 = [0,0,0,0]
line_x2 = [0,0,0,0]
line_y1 = [0,0,0,0]
line_y2 = [0,0,0,0]
line_H = [0,0,0,0]
line_V = [0,0,0,0]
line_temp = [0,0,0,0]
line_num = 0
line_num_y = 0
line_num_x = 0
num_H = 0
num_V = 0
Flag = int(0)
weight = 0
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
#print(img.height())
#检测图形
# img_2=img.binary([(0,127)],invert = 1)
#image.binary(thresholds, invert=False)此函数将在thresholds内的
#图像部分的全部像素变为1白,将在阈值外的部分全部像素变为0黑。invert将图像
#的0 1(黑 白)进行反转,默认为false不反转。
# img_2.median(1, percentile=0.5)
# img_2.erode(2)
for i in range(int(HEIGHT/3)):
for j in range(int(HEIGHT/3)):
if img.get_pixel(i*3+XO,j*3) < 127 :
weight += 1
#print(img.get_pixel(i,j))
print(weight)
#下面开始检测特征线数(可去?)
img.find_edges(image.EDGE_CANNY, threshold=(50, 80)) # Find edges
lines = img.find_lines(roi = ROI,threshold = THRESHOLD) # Find lines.
# lines = img.find_lines(threshold = THRESHOLD)
img.draw_rectangle(ROI)
#for l in lines: img.draw_line(l, color=(127)) # Draw lines
#print(lines) # Note: Your OpenMV Cam runs about half as fast while
if len(lines) != 0:
#print(len(lines),lines)
for i in range(len(lines)):
#print(i)
if lines[i][0] == 0: #为横线时
if line_x1 == [0,0,0,0]: #横一无值时
line_x1 = list(lines[i])
elif abs(line_x1[1]-lines[i][1]) < LINE_WIDTH: #为标准横一线时
for j in range(4):
line_x1[j] = (i * line_x1[j] + lines[i][j])/(i+1)
else: #不为标准横一线时
if line_x2 == [0,0,0,0]: #横二无值时
line_x2 = list(lines[i])
elif abs(line_x2[3]-lines[i][3]) < LINE_WIDTH: #为标准横二线时
for j in range(4):
line_x2[j] = (i * line_x2[j] + lines[i][j])/(i+1)
#为其他横线时舍弃
elif lines[i][1] == 0: #为竖线时
if line_y1 == [0,0,0,0]: #竖一无值时
line_y1 = list(lines[i])
elif abs(line_y1[0]-lines[i][0]) < LINE_WIDTH: #为标准竖一线时
for j in range(4):
line_y1[j] = (i * line_y1[j] + lines[i][j])/(i+1)
else: #不为标准竖一线时
if line_y2 == [0,0,0,0]: #竖二无值时
line_y2 = list(lines[i])
elif abs(line_y2[2]-lines[i][2]) < LINE_WIDTH: #为标准竖二线时
for j in range(4):
line_y2[j] = (i * line_y2[j] + lines[i][j])/(i+1) #为其他竖线时舍弃
#为其他线时舍弃
line_all = [line_x1,line_y1,line_x2,line_y2]
for k in range(len(line_all)):
if line_all[k][2] == 140:
line_num_x += 1
if line_all[k][3] == 120:
line_num_y += 1
line_num = line_num_y + line_num_x
if line_num == 2:
if (line_num_y == 2) or (line_num_x == 2):
Flag_0 = '-'
else:
Flag_0 = '+'
elif line_num < 2:
Flag_0 = '-'
else:
Flag_0 = '+'
print(line_num)
#line_all = [line_x1,line_y1,line_x2,line_y2]
#print(line_all)
#下面开始检测横纵中线
if len(lines) != 0:
for i in range(len(lines)):
#print(len(lines),lines)
if lines[i][0] == 0: #为横线时
if line_H == [0,0,0,0]: #横一无值时
line_H = list(lines[i])
num_H += 1
elif abs(line_H[1]-lines[i][1]) < 3*LINE_WIDTH and abs(line_H[3]-lines[i][3]) < 3*LINE_WIDTH:
for j in range(4):
line_H[j] = (num_H * line_H[j] + lines[i][j])/(num_H+1)
num_H += 1
elif lines[i][1] == 0: #为竖线时
if line_V == [0,0,0,0]: #横一无值时
line_V = list(lines[i])
num_V += 1
elif abs(line_V[0]-lines[i][0]) < 3*LINE_WIDTH and abs(line_V[2]-lines[i][2]) < 3*LINE_WIDTH:
for j in range(4):
line_V[j] = (num_V * line_V[j] + lines[i][j])/(num_V+1)
num_V += 1
#print(line_x1)
if line_H == [0,0,0,0]:
line_H = [0,HEIGHT/2,(WIDTH-XO),HEIGHT/2]
if line_V == [0,0,0,0]:
line_V = [(XO+HEIGHT/2),0,(XO+HEIGHT/2),HEIGHT]
line_H = [ int(x) for x in line_H ]
line_V = [ int(x) for x in line_V ]
img.draw_line(line_H,127)
img.draw_line(line_V,127)
print(line_H)
print(line_V)
#找直线的交点(已完成)
if line_V[2] != line_V[0]:
central_x = -(line_V[0]*HEIGHT/(line_V[2]-line_V[0])+line_H[1])/((line_H[3]-line_H[1])/(WIDTH-XO)-HEIGHT/(line_V[2]-line_V[0]))
else:
central_x = line_V[0]
central_y = (line_H[3]-line_H[1])/(WIDTH-XO)*central_x+line_H[1]
img.draw_cross(int(central_x),int(central_y),color=191)
print(central_x)
print(central_y)
err_x = int(central_x - WIDTH/2)
err_y = int(central_y - HEIGHT/2)
#接下来是FLAG
if (abs((central_y - (HEIGHT/2)) < CENTRAL_AREA) and (abs(central_x - (WIDTH/2)) < CENTRAL_AREA)):
if Flag_0 == '+' :
if weight > 300 :
Flag = 0xFF #T
else:
Flag = 0x7F #L
elif Flag_0 =='-' :
Flag = 0x3F #I
else:
Flag = 0x00 #D
print(Flag)