logo

OpenMV4 H7 Plus双串口配置问题:如何同时使用串口1和串口3发送不同类型数据

2025-11-07 09:10
M
m7nu8lu5d0fp

OpenMV4 H7 Plus双串口配置问题:如何同时使用串口1和串口3发送不同类型数据

用户在使用OpenMV4 H7 Plus开发智能追踪系统时遇到了双串口配置问题。系统需要同时使用串口3发送舵机角度信息,串口1发送测距信息,但不确定如何在代码中区分使用不同的串口。

项目背景:智能追踪系统,利用OpenMV识别物块,通过STM32控制舵机旋转,并报告物块的极坐标信息(距离和角度)。

  • 硬件信息:OpenMV4 H7 Plus
  • 固件版本:4.7.0
  • 问题类型:其他(串口配置问题)
  • 具体问题:如何在代码中区分使用串口1和串口3发送不同类型的数据,当前代码中只使用了uart.write函数,没有区分串口

用户提供的代码显示目前只配置了串口3,用于发送舵机角度信息,但需要增加串口1来发送测距信息。

# Untitled - By: RitaZ - Sat Oct 18 2025

#视觉功能仅识别颜色

import sensor, image, math
import time
from machine import UART
from pyb import Pin

p6_in = Pin('P6', Pin.IN, Pin.PULL_DOWN)#设置p_in为输入引脚,并开启下拉电阻
value = p6_in.value() 

Red_threshold = (13, 49, 18, 61, 6, 47) #红色色块
                 #green_threshold =(20,120,-128,-10,-50,50)      #寻找绿色色块

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)  #320*240
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)         #摄像头初始化配置,颜色追踪需要关闭这两项
sensor.set_hmirror(True)
sensor.set_vflip(True)

clock = time.clock()

uart = UART(3,115200)  #P4TX,P5RX    #配置通信串口

Kp = 0.9
Ki = 0.02
Kd = 0.2               #PID参数设置

last_error_x = 0   #上次误差
integral_x = 0     #误差累计量
last_error_y = 0
integral_y = 0

Servo_dx = 0
Servo_dy = 0
Servo_data = [0,0]   #打包一起发出去

HEADER = b'\xFF'
FOOTER = b'\xFE'

img_cx = int(sensor.width()/2 - 9)
img_cy = int(sensor.height()/2 + 11)      #视觉中心坐标

real_width_cm = 4
camera_focal_pixels = 375

def send_packet(data):    #数据发送(?)
    data_byte = bytes(data)
    packet = HEADER + data_byte + FOOTER    #包头数据包尾
    uart.write(packet)    #发送

def find_max(blobs):     #寻找最大红色色块
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob

def pid_control(error,last_error,integral,Kp,Ki,Kd):    #pid算法控制
    if math.fabs(error) < 10 : #当距离较小时锁定摄像头防止持续抖动
        error = 0
    
    P = Kp * error
    integral += error
    ##此处是否要考虑限幅

    I = Ki * integral
    D = Kd * (error - last_error)
    output = P + I + D
    if output >500: output = 500
    if output <-500: output = -500
    last_error = error
    return output,last_error,integral

def output_to_servo(output_x, output_y):        #舵机角度计算
    Servo_dx = int(math.atan2(output_x * 1/15, 15) * 180/3.14)    #角度计算 处于右半平面则输出角度为-,起始状态最大9°左右
    Servo_dy = int(math.atan2(output_y * 1/15, 15) * 180/3.14)   #输出角度最大变为—36°
    return Servo_dx + 128, Servo_dy + 128  #STM32接受的数据类型为UINT_8:0-225,没有负数

def calculate_distance(real_width_cm, camera_focal_pixels,captured_pixels):  #测距
    distance_cm = real_width_cm * camera_focal_pixels / captured_pixels
    return distance_cm

while True:           #主循环
    clock.tick()
    img = sensor.snapshot()
    img.draw_cross(img_cx,img_cy,size = 20)  #在视觉中心画十字
    flag = 0
    
    blobs = img.find_blobs([Red_threshold]) #识别红色
    
    if blobs:
        max_blob = find_max(blobs)  #寻找最大色块
        blob_cx = max_blob.cx()
        blob_cy = max_blob.cy()

        img.draw_rectangle(max_blob.rect()) #画出最大色块

        error_x = img_cx - blob_cx
        error_y = img_cy - blob_cy   #像素偏差值
        
        output_x, last_error_x,integral_x = pid_control(error_x,last_error_x,integral_x,Kp,Ki,Kd)
        output_y, last_error_y,integral_y = pid_control(error_y,last_error_y,integral_y,Kp,Ki,Kd)

        Servo_data[0], Servo_data[1] = output_to_servo(output_x, output_y)
        
        if flag == 0:
            if value:
                flag = 1
                
        
        if flag == 1:
            img.draw_cross(blob_cx, blob_cy)
            send_packet(Servo_data)
            print(Servo_data[0]-128+135,Servo_data[1]-128+135) #角度,pid输出
            
            if error_x == 0 and error_y == 0:
                flag = 2
                
                
        if flag == 2:
            captured_pixels = max_blob.w()
            distance_cm = calculate_distance(real_width_cm, camera_focal_pixels,captured_pixels)
            send_packet(distance_cm)
            print(distance_cm)

        #####锁定角度#########
        #if math.fabs(error_x) < 10 and math.fabs(error_y) < 10:
            #send_packet([128,128])  #
        #else:
            #send_packet(Servo_data)
    else:
        send_packet([128,128])  #舵机静止不动
        print(0)
        #print(clock.fps())
OpenMV正面照片
OpenMV反面照片
2025-11-07 17:10
Z
星小瞳xs

初始化不同的串口就行

uart = UART(3,115200)  #P4TX,P5RX    #配置通信串口3
uart2 = UART(1,115200)  #P1TX,P0RX    #配置通信串口1

然后向不同的串口发送数据即可