logo

openmv打开uart脱机运行为什么会自动断开?

2022-05-08 21:25
F
g4kx

你好,

我想用通过uart使openmv传数据到stm32, 连接电脑ide的时候是正常运行的,stm32也能接收到数据。但是一旦脱机运行,openmv运行几秒钟之后就会闪两下红灯自动断开。我尝试过注释掉uart代码,再脱机运行又能正常工作了。所以我不知道是不是uart有什么问题使openmv断开。

下面是我的代码:

import sensor, image, time, math
from pid import PID
from pyb import UART, LED
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
rho_weight = 1
theta_weight = 1
count = 0
w = 80
h = 60
mid_x = w/2
mid_y = h/2
gain = 5
uart = UART(3, 115200, timeout_char = 1000)
green_led = LED(2)
def pooling_filter(img):
    img.midpoint_pool(2, 2,bias=0.5)
    img.binary([(120,130)])
    img.midpoint_pool(2, 2,bias=0.5)
    img.binary([(120,130)])
def find_middle_line(img):
    try:
        pooling_filter(img)
        line = img.get_regression([(240,255)], robust = True)
        img.draw_line(line.line(), color = 139,thickness=2)
        rho_error, theta_error = line_to_theta_and_rho(line.rho(),line.theta())
        return rho_error, theta_error
    except:
        return 0,0
def line_to_theta_and_rho(rho,theta):
    if rho < 0:
        trans_rho = -rho
        trans_theta = theta + 180
    else:
        trans_rho = rho
        trans_theta = theta
    cos_trans_theta = math.cos(math.radians(trans_theta))
    sin_trans_theta = math.sin(math.radians(trans_theta))
    distance = abs(mid_x*cos_trans_theta + mid_y*sin_trans_theta - trans_rho)
    if trans_theta == 90 or trans_theta == 270:
        distance_error = theta_error = 0
    elif (trans_rho-mid_y*sin_trans_theta)/cos_trans_theta > mid_x:
        distance_error = distance
        if theta > 90:
            theta_error = theta - 180
        else:
            theta_error = theta
    else:
        distance_error = -distance
        if theta > 90:
            theta_error = theta - 180
        else:
            theta_error = theta
    return distance_error, theta_error
while(True):
    clock.tick()
    img = sensor.snapshot()
    img.find_edges(image.EDGE_CANNY, threshold=(150,190))
    rho_error,theta_error = find_middle_line(img)

    if count != 10:
        if count == 0:
            rho_error_sum = theta_error_sum = 0
        rho_error_sum += rho_error
        theta_error_sum += theta_error
        count += 1
        continue
    else:
        rho_error_sum /= 10
        theta_error_sum /= 10
        count = 0
    rho_pid = PID(p=0.40, i=0.1, d=0, imax=10)
    theta_pid = PID(p=0.25, i=0.1, d=0, imax=10)
    rho_output = rho_pid.get_pid(rho_error_sum,1)
    theta_output = theta_pid.get_pid(theta_error_sum,1)
    output = gain*int(rho_weight*rho_output+theta_weight*theta_output)
    if output > 0:
        output = 'A'
    else:
        output = 'B'
    uart.write(str(output)+'\r\n')
    green_led.on()
    print('output=',output)
2022-05-09 20:46
I
网红小智智 星瞳科技

@g4kx 在 [openmv打开uart脱机运行为什么会自动断开?](/post/25806) 中说:

> 但是一旦脱机运行,openmv运行几秒钟之后就会闪两下红灯自动断开

脱机运行,哪里断开?是说OpenMV会重启?

我脱机运行这个代码没有出现闪红灯的现象,你脱机运行时通过什么供电的?把全部电路都拔掉,只通过USB供电试试。