logo

正确连接测距扩展版后运行代码,少部分呢情况下会出现ETIMEDOUT错误?

2021-05-28 15:41
6
静看长安花落
from machine import I2C
from vl53l1x import VL53L1X

import sensor, image, time ,math

from pyb import UART

uart = UART(1, 115200)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
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()

i2c = I2C(2)
distance = VL53L1X(i2c)

x=0
y=0

while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(1.8)
    for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
            r_min = 2, r_max = 100, r_step = 2):
        area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
        #area为识别到的圆的区域,即圆的外接矩形框
        statistics = img.get_statistics(roi=area)#像素颜色统计
        print(statistics)
        #(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
        #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
        if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is red
            img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
            x = c.x()
            y = c.y()
            #uart.write("find\r\n")
    print("range: mm ", distance.read(),x,y)
    #distance_head = distance.read()//256
    #distance_tail = distance.read()%256
    #data = bytearray([0x2C,0x18,x,y,distance_head,distance_tail,0x5B])
    #uart.write(data)
aaefd6bd-cd4d-420e-9fbf-e80ac0395b0a.png
2021-05-31 17:34
I
网红小智智 星瞳科技

引脚插的不紧,松了。