logo

当STM用HAL_UART_Transmit()函数给openmv发送数据时,画面卡死,过了很久之后才会恢复正常

2021-03-12 17:05
W
fayn
import sensor, image, time, pyb
import ujson
from pyb import UART

green_threshold   = (73, 96, -79, -22, -128, 127)
#(0, 100, -128, -25, -128, 127)
red_threshold   = (41, 61, 42, 127, -128, 127)
#(41, 61, 42, 127, -128, 127)
blue_threshold   = (0, 44, -8, 47, -68, -13)
#(15, 100, -128, 127, -128, -41)
#red=2,green=1,blue=4
getrx = 0
getcmd = 0
renum = 0
colornum = 0
ptrposition = 0
sensor.reset()# 初始化摄像头
sensor.set_pixformat(sensor.RGB565)# 格式为 RGB565.
sensor.set_framesize(sensor.QQVGA) # 使用 QQVGA 速度快一些
sensor.skip_frames(time = 2000) # 跳过2000s,使新设置生效,并自动调节白平衡
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的,在颜色识别中,一定要关闭白平衡。
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。
clock = time.clock() # 追踪帧率
led = pyb.LED(1) # Red LED = 1, Green LED = 1, Blue LED = 2, IR LEDs = 4.
uart = UART(3, 115200, timeout_char = 1000)
led.on()

def Rec_NUM1(lista):
    if (lista[0]=='1' and lista[1]=='2' and lista[2]=='3'):
        return 1
    elif (lista[0]=='1' and lista[1]=='3' and lista[2]=='2'):
        return 2
    elif (lista[0]=='2' and lista[1]=='1' and lista[2]=='3'):
        return 3
    elif (lista[0]=='2' and lista[1]=='3' and lista[2]=='1'):
        return 4
    elif (lista[0]=='3' and lista[1]=='1' and lista[2]=='2'):
        return 5
    elif (lista[0]=='3' and lista[1]=='2' and lista[2]=='1'):
        return 6

def Rec_NUM2(lista):
    if (lista[4]=='1' and lista[5]=='2' and lista[6]=='3'):
        return 1
    elif (lista[4]=='1' and lista[5]=='3' and lista[6]=='2'):
        return 2
    elif (lista[4]=='2' and lista[5]=='1' and lista[6]=='3'):
        return 3
    elif (lista[4]=='2' and lista[5]=='3' and lista[6]=='1'):
        return 4
    elif (lista[4]=='3' and lista[5]=='1' and lista[6]=='2'):
        return 5
    elif (lista[4]=='3' and lista[5]=='2' and lista[6]=='1'):
        return 6

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    if uart.any():
        led.off()
        getrx = uart.readline()
        print(getrx)
        time.sleep(150)     #延时150ms
        led = pyb.LED(2)
        led.on()
        getcmd = int(getrx)
        print(getcmd)
        #print(img.find_qrcodes())
    img = sensor.snapshot()# 从感光芯片获得一张图像
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
#    getcmd=2
    if(getcmd==2):
        for code in img.find_qrcodes():
             output_str="%s" % code.payload() #方式1
             renum = int(Rec_NUM1(output_str)*10 + Rec_NUM2(output_str))
             uart.write(ujson.dumps(renum))
             getcmd = 0
             led.off()
    if blobs and getcmd==3:
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            #用矩形标记出目标颜色区域
            img.draw_cross(b[5], b[6]) # cx, cy
            #在目标颜色区域的中心画十字形标记
            #print(b[5], b[6], b[8])
            #uart.write(ujson.dumps(b[8]))
            #将颜色序号转为比赛序号
            if b[8]==1:
                colornum=2
            elif b[8]==2:
                colornum=1
            elif b[8]==4:
                colornum=3
            print('colornum=',colornum,'output_str[ptrposition]=',output_str[ptrposition])
            #若任务码对应
            if (int(output_str[ptrposition]) == colornum):
                uart.write('t')
                ptrposition+=1
                if ptrposition==4:
                    ptrposition+=1
                getcmd=0
9a2b5585-1abe-4731-b99f-bb40feba7520.png

串口是可以检测到数据的

但是却会画面会卡死,然后过了很久之后才会正常,然后又输出一个数据

54df259d-3061-4f6c-b86a-ec64a1edd72f.png
2021-03-15 13:35
I
网红小智智 星瞳科技

time.sleep(150)改成time.sleep_ms(150)