logo

运行途中出现Sensor Timerout!,怎么解决

2021-11-06 13:44
5
xvb2
13f3a301-c797-452d-b393-faef08b61af3.jpg
2021-11-06 14:09
I
网红小智智 星瞳科技

如果涉及代码,需要报错提示与全部代码文本,请注意不要贴代码图片

5
xvb2

@kidswong999

import sensor, image, time, ustruct
from pyb import UART,  LED


RED_THRESHOLD = [(13, 65, 11, 42, -25, 27)]
BLACK_THRESHOLD = [(12, 34, -30, -2, -6, 22) ]
#BLACK_THRESHOLD = [(18, 34, -25, -4, -14, 13)]

ROI1 = (18,29,94,62)
ROI2 = (210,29,94,62)
num = 0
move = 0  #0空操作1停止2前进
move_flag = 0 #0空操作1左转2右转
cx = 0
cy = 0
ch = 0
cw = 0
area = 0
num = 0
#number1 = 0
number2 = 0
first_number = 0
match_number = 0
red = (255,0,0)
greed = (0,255,0)
blue =(0,0,255)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(5)
sensor.set_auto_gain(False)#关闭自动增益
sensor.set_auto_whitebal(False) # 必须关闭白平衡
sensor.set_hmirror(True)#水平方向翻转
sensor.set_vflip(True)#垂直方向翻转
clock = time.clock()

uart = UART(3,9600,timeout_char = 1000000)

#uart.write(bytearray([0xff,0xfe,0,1,1]))

#数字识别
def number():
    number1 = 0
    for blob in img.find_blobs(BLACK_THRESHOLD, pixels_threshold=500, area_threshold=5000, merge=True,roi=(160,0,160,240)):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        cx = blob.cx()
        cy = blob.cy()
        ch = blob.h()
        cw = blob.w()
        img.draw_rectangle(cx-int(cw/2),cy,cw,int(ch/2),color = red)
        img.draw_cross(cx, cy+int(ch/4), size=5, color=red)
        for blob1 in img.find_blobs(BLACK_THRESHOLD, roi=(cx-int(cw/2),cy,cw,int(ch/2)),pixels_threshold=50, area_threshold=300, merge=True):
            '''print("面积1 = ",blob1.area())
            print("像素点1 = ",blob1.density())'''
            area = blob1.area()*blob1.density()
            area1 = blob1.area()*blob1.area()/blob1.density()
            '''print("面积1.1 = ",area)
            print("面积1.2 = ",area1)
            print("\n")'''
            if(400<area<1200):
                #print("222")
                if(0.35<blob1.density()<0.45 and 600<area<900 and area1 < 1.25e+07):
                    number1 = 3
                    print("number1 = 3")
                elif(0.3<blob1.density()<0.4 and 280<area<500):
                    number1 = 7
                    print("number1 = 7")
                if(number1 != 0):
                    num = number1
                    return(num or 0)
            area = blob1.area()*blob1.density()
            area1 = blob1.area()*blob1.area()/blob1.density()
            area = blob1.area()*blob1.density()
            area1 = blob1.area()*blob1.area()/blob1.density()
            if(400<area<900):
                print("222")
                if(0.35<blob1.density()<0.45 and 600<area<900 and area1 < 1.15e+07):
                    number1 = 3
                    print("number1 = 3")
                elif(0.3<blob1.density()<0.4 and 280<area<500):
                    number1 = 7
                    print("1 = 7")
                if(2000<blob1.area()<2800):
                    if(blob1.density()>0.43 and area > 900):
                        number1 = 8
                        print("number1 = 8")
    
                if(500<area<1100):
                   # print("333")
                    if(0.48 < blob1.density() < 0.55 and area < 1500):
                        number1 = 2
                        print(111)
                        print("number1 = 2")
                    elif(0.35<blob1.density()<0.4 and 800<area<900 and 1.45e+07 > area1 > 1.3e+07):
                        number1 = 4
                        print("number1 = 4")
    
                    elif(0.35 < blob1.density() < 0.45 and area > 900 and blob1.area() < 2450 and 1.45e+07 > area1 > 1.3e+07):
                        number1 = 6
                        print("number1 = 6")
    
                    if(0.2<blob1.density()<0.35):
                        if(700<area<900 and blob1.area() > 2300):
                            number1 = 5
                            print("number1 = 5")
    if(number != 0):
        num = number1
        return(num)

    for blob in img.find_blobs(BLACK_THRESHOLD, pixels_threshold=1500, area_threshold=5000, merge=True,roi=(0,0,160,240)):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        cx = blob.cx()
        cy = blob.cy()
        ch = blob.h()
        cw = blob.w()
        img.draw_rectangle(cx-int(cw/2),cy,cw,int(ch/2),color = red)
        img.draw_cross(cx, cy+int(ch/4), size=5, color=red)
        for blob2 in img.find_blobs(BLACK_THRESHOLD, roi=(cx-int(cw/2),cy,cw,int(ch/2)),pixels_threshold=50, area_threshold=300, merge=True):
            print("面积2 = ",blob2.area())
            print("像素点2 = ",blob2.density())
            area = blob2.area()*blob2.density()
            area2 = blob2.area()*blob2.area()/blob2.density()
            print("面积2.1 = ",area)
            print("面积2.2 = ",area2)
            '''if(blob2.area()<5000):
                if(area==0):
                    number2 = 1;
                    print("number2 = 1")'''
            if(400<area<900):
                print("222")
                if(0.35<blob2.density()<0.45 and 600<area<900 and area2 < 1.15e+07):
                    number2 = 3
                    print("number2 = 3")
                elif(0.3<blob2.density()<0.4 and 280<area<500):
                    number2 = 7
                    print("number2 = 7")

            if(2200<blob2.area()<2800):
                if(blob2.density()>0.45 and area > 1050):
                    number2 = 8
                    print("number2 = 8")

            if(500<area<1100):
                #print("333")
                if(0.45 < blob2.density() < 0.55 and area < 1500):
                    number1 = 2
                    print("number2 = 2")
                elif(0.35<blob2.density()<0.45 and 930<area<1100 and area2 > 1.35e+07):
                    number2 = 4
                    print("number2 = 4")

                elif(0.41 < blob2.density() < 0.45 and area > 950 and blob2.area() < 2450 and 1.36e+07 > area2 > 1.25e+07):
                    number2 = 6
                    print("number2 = 6")

                if(0.2<blob2.density()<0.35):
                    if(700<area<900 and blob2.area() > 2300):
                        number2 = 5
                        print("number2 = 5")


while(True):
    clock.tick()
    match_blob1 = 0
    match_blob2 = 0
    img = sensor.snapshot().lens_corr(strength = 1.8,zoom = 1)

    #LED(1).on()

    #画ROI区域
    img.draw_rectangle(ROI1, thickness = 2, fill = False)
    img.draw_rectangle(ROI2, thickness = 2, fill = False)
    img.draw_rectangle(160,0,160,240,color=greed)
    img.draw_rectangle(160,0,160,240,color=blue)

    
    #识别数字first_munber
    first_number = 2
    data = bytearray([0xff, 0xfe, move, first_number, move_flag])
    uart.write(data)
    print(data)

    #if first_number is not None:
     #  print("true")

    #识别到十字路口
    for blob in img.find_blobs(RED_THRESHOLD, roi = ROI1, area_threshold = 500, merge=True):
        if blob.code() == 1:
            match_blob1 = 1
            img.draw_rectangle(blob.rect())

    for blob in img.find_blobs(RED_THRESHOLD, roi = ROI2, area_threshold = 500, merge=True):
        if blob.code() == 1:
            match_blob2 = 1
            img.draw_rectangle(blob.rect())

    if(match_blob1 == 1 or match_blob2 == 1):
        move = 1
        data = bytearray([0xff,0xfe,move,first_number, move_flag])
        uart.write(data)
        print(data)
        if(first_number == 1 or first_number == 2):
            if(first_number == 1):
                #move = 1
                move_flag = 1
                data = bytearray([0xff,0xfe,move,first_number, move_flag])
                uart.write(data)
                print(data)
            elif(first_number == 2):
                move = 3
                move_flag = 2
                data = bytearray([0xff,0xfe,move,first_number, move_flag])
                uart.write(data)
                print(data)
        else:
            move = 1
            data = bytearray([0xff,0xfe,move,first_number, move_flag])
            uart.write(data)
            print(data)

            #识别数字match_munber
            match_number = number()

            if(match_number == first_number):
                #右转
                if(cx>80):
                    move_flag = 2
                    data = bytearray([0xff,0xfe,move,match_number, move_flag])
                    uart.write(data)
                    print(data)
                #左转
                elif(cx<80):
                    move = 0
                    move_flag = 1
                    data = bytearray([0xff,0xfe,move,match_number, move_flag])
                    uart.write(data)
                    print(data)
            elif(match_number != first_number):
                move = 2
                data = bytearray([0xff,0xfe,move,first_number, move_flag])
                uart.write(data)
                print(data)



        #识别黑色虚线
    '''for blob in img.find_blobs(BLACK_THRESHOLD, area_threshold = 1000, merge=True):
        if blob.code() == 1:
            move_flag = 0
            img.draw_rectangle(blob.rect())
            data = bytearray([0xff,0xfe,move,first_number, move_flag])
            uart.write(data)
            print(data)'''
2021-11-06 14:31
I
网红小智智 星瞳科技

我测试了好久,也没有出现错误。

你看一下是不是摄像头模组没有插好。