import sensor, image, time
from machine import UART,Timer
from fpioa_manager import fm
#映射串口引脚
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
#初始化串口
uart = UART(UART.UART1, 115200, read_buf_len=4096)
yellow_threshold = (25, 91, 20, 80, 9, 67)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) #160*120
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
cx=160/2
K=450
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([yellow_threshold])
if len(blobs) == 1:
# Draw a rect around the blob.
b = blobs[0]
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
c=cx-b[5]
if c > 20:
print("在右")
uart.write('left')
if c < -20:
print("在左")
uart.write('right')
Lm = (b[2]+b[3])/2
length = K/Lm
print(length)
uart.write(str(length))
time.sleep_ms(100)
能够实现检测色块的距离
返回色块中心坐标与图像中心坐标的像素差,但是一个是实际距离,另一个是像素,怎么才能得到色块的偏移角
下面是在星瞳科技的测距内容