发送字符串简单:直接write(“........”)
发送一个数字一个字符可以write(k)
int kk=1;write(kk);
发送数字数组可以: kk=[51,100]
uart.write(bytes(kk))
import sensor, image, time, math, pyb
from machine import UART
import display
#用来调binary值
NUMBER1=51
NUMBER2=255
#这两变量是用来你调阈值,是为了适应在不同场合光线下刚好把黑色与白色分开 ,借助按键调整这个阈值就可以完成现场调试
turnGRAYSCALE_THRESHOLD = [(NUMBER1, NUMBER2)]
GRAYSCALE_THRESHOLD =[(0,0)]
# 每个roi为(x, y, w, h),线检测算法将尝试找到每个roi中最大的blob的质心。
# 然后用不同的权重对质心的x位置求平均值,其中最大的权重分配给靠近图像底部的roi,
# 较小的权重分配给下一个roi,以此类推。
rois = [(0, 100, 160, 20), (0, 50, 160, 20), (0, 0, 160, 20)]
# roi代表三个取样区域,(x,y,w,h,weight),代表左上顶点(x,y)宽高分别为w和h的矩形,
# weight为当前矩形的权值。注意本例程采用的QQVGA图像大小为160x120,roi即把图像横分成三个矩形。
# 三个矩形的阈值要根据实际情况进行调整,离机器人视野最近的矩形权值要最大,
# 如上图的最下方的矩形,即(0, 100, 160, 20, 0.7)
# 初始化sensor
sensor.reset()
# 设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种
sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
# 设置图像像素大小
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
# 让新的设置生效。
sensor.skip_frames(time=2000) # Let new settings take effect.
# 颜色跟踪必须关闭自动增益
sensor.set_auto_gain(False) # must be turned off for color tracking
# 颜色跟踪必须关闭白平衡
sensor.set_auto_whitebal(False) # must be turned off for color tracking
# 跟踪FPS帧率
lcd = display.SPIDisplay() # Initialize the lcd screen.
kk='y'
sensor.set_vflip(1)
sensor.set_hmirror(1)
clock = time.clock() # Tracks FPS.
largest2_blob=0
deflection_angle = 0 # Initialize deflection_angle outside of conditional blocks
uart = UART(3,9600,timeout_char=1000)
while True:
turnGRAYSCALE_THRESHOLD = [(NUMBER1, NUMBER2)]
clock.tick() # Track elapsed milliseconds between snapshots.
img = sensor.snapshot() # Capture an image.
img.binary(turnGRAYSCALE_THRESHOLD)
largest_blob = None
#largest2_blob = None
#largest3_blob = None
# Track lines in each defined ROI.
#blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[0], merge=True)
#if blobs:
# largest_blob = max(blobs, key=lambda b: b.pixels())
blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[1], merge=True)
if blobs:
largest2_blob = max(blobs, key=lambda b: b.pixels())
#
#blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[2], merge=True)
#if blobs:
# largest3_blob = max(blobs, key=lambda b: b.pixels())
# Calculate deflection_angle based on largest2_blob.cx() - 79
#通过计算图像中心点x与块中心点x的差判断偏移
pianyi = 0
if largest2_blob:
pianyi = largest2_blob.cx() - 79
img.draw_rectangle(largest2_blob.rect(),color=(0));
if -5 <= pianyi < 5:
deflection_angle = 0
elif -15 <= pianyi < -5:
deflection_angle = -2
elif 5 <= pianyi < 15:
deflection_angle = 2
elif -30 <= pianyi < -15:
deflection_angle = -3
elif 15 <= pianyi < 30:
deflection_angle = 3
elif -50 <= pianyi < -30:
deflection_angle = -5
elif 30 <= pianyi < 50:
deflection_angle = 5
if NUMBER1>255:
NUMBER1=255
if NUMBER2>255:
NUMBER2=255
if NUMBER1<0:
NUMBER1=0
if NUMBER2<0:
NUMBER2=0
kk=[NUMBER1,NUMBER2]
uart.write(bytes(kk))
lcd.write(img) # Take a picture and display the image.
byte=uart.read(1)
if byte :
print(byte)
if byte ==b'a':
NUMBER1=NUMBER1+5
if byte ==b'b':
NUMBER2=NUMBER2+5
if byte ==b'c':
NUMBER1=NUMBER1-5
if byte ==b'd':
NUMBER2=NUMBER2-5