logo

串口通信和云台有冲突吗,为什么使用了UART3后云台不响应?

2025-07-19 17:52
A
_3tvw

import sensor

import display

import image

import time

import pyb

import ml

from pyb import UART

from pid import PID

from pyb import Servo

red = (23, 56, 19, 43, -4, 25)

#-------------------------------------------------------

#脱机调阈值参数

state = 1

mode = 1

trans_data = 0

Lmin = 0

Lmax = 100

Amin = -128

Amax = 127

Bmin = -128

Bmax = 127

#-------------------------------------------------------

#识别数字对应操作参数

num = [0,0,0,0,0,0,0,0,0,0]

count = 0

model = ml.Model("trained.tflite", load_to_fb=True)

norm = ml.Normalization(scale=(0, 1.0))

#-------------------------------------------------------

#云台追踪参数及函数

def find_max(blobs):

max_size=0

for blob in blobs:

if blob[2]*blob[3] > max_size:

max_blob=blob

max_size = blob[2]*blob[3]

return max_blob

#-------------------------------------------------------

#red = (9, 73, 5, 55, 1, 32)

def uart_callback(timer):

global received_data

if uart.any():

received_data = uart.readchar()

uart = UART(3,9600)

uart.init(9600, bits=8, parity=None, stop=1)

received_data = None

# 初始化定时器,每10ms触发一次

timer = pyb.Timer(4, freq=100)

timer.callback(uart_callback)

sensor.reset() # 初始化sensor

# 初始化lcd屏幕。

# 注意:如果支持,可以使用 DAC 或 PWM 背光控制器来控制背光强度:

# lcd = display.SPIDisplay(backlight=display.DACBacklight(channel=2))

# lcd.backlight(25) # 25% intensity

# 否则,将使用默认的 GPIO(开on/关off)控制器。

lcd = display.SPIDisplay()

while True:

if(received_data == 115):#小写s的ASCLL码,向openmv发送字符s切换模式

state += 1

received_data = None

if(state > 3):

state = 1

if(state == 1):

color = (Lmin,Lmax,Amin,Amax,Bmin,Bmax)

sensor.set_framesize(sensor.QQVGA2)

sensor.set_pixformat(sensor.RGB565)

img = sensor.snapshot()

img.binary([color])

lcd.write(img)

if(received_data == 99):#小写c的ASCLL码,向openmv发送字符c改变要调整的LAB的值

mode += 1

if(mode > 6):

mode = 1

received_data = None

if(mode == 1):

if(received_data == 43): # 43 是字符 '+' 的ASCII码,发送一个字符+后对应LAB值+2

trans_data = received_data

Lmin += 5

received_data = None

if(received_data == 45): #45 是字符 '-' 的ASCII码,发送一个字符-后对应LAB值-2

trans_data = received_data

Lmin += -5

received_data = None

if(mode == 2):

if (received_data == 43):

trans_data = received_data

Lmax += 5

received_data = None

if(received_data == 45):

trans_data = received_data

Lmax += -5

received_data = None

if(mode == 3):

if (received_data == 43):

trans_data = received_data

Amin += 5

received_data = None

if(received_data == 45):

trans_data = received_data

Amin += -5

received_data = None

if(mode == 4):

if (received_data == 43):

trans_data = received_data

Amax += 5

received_data = None

if(received_data == 45):

trans_data = received_data

Amax += -5

received_data = None

if(mode == 5):

if (received_data == 43):

trans_data = received_data

Bmin += 5

received_data = None

if(received_data == 45):

trans_data = received_data

Bmin += -5

received_data = None

if(mode == 6):

if (received_data == 43):

trans_data = received_data

Bmax += 5

received_data = None

if(received_data == 45):

trans_data = received_data

Bmax += -5

received_data = None

print("color:",color,"trans_data:",trans_data,"color_mode:",mode,"state:",state)

if(state == 2):

if(count < 100):

sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to GRAYSCALE (or RGB565)

sensor.set_framesize(sensor.QQVGA2) # Set frame size to QVGA (320x240)

sensor.set_windowing((240, 240)) # Set 240x240 window.

img = sensor.snapshot().binary([(0,60)]).dilate(2)

#img.binary([color])

lcd.write(img)

input = [norm(img)] # scale 0~255 to 0~1.0

result = model.predict(input)[0].flatten().tolist()

number = result.index(max(result))

print("number", number)

num[number] += 1

count += 1

if(count >= 100):

max_num = max(num)

final_num = num.index(max_num)

print("final_num",final_num,"state:",state)

#对应数字操作

if(state == 3):

pan_servo=Servo(1)

tilt_servo=Servo(2)

pan_servo.calibration(500,2500,500)

tilt_servo.calibration(500,2500,500)

pan_pid = PID(p=0.10, i=0, d=0.002,imax=90) #脱机运行或者禁用图像传输,使用这个PID

tilt_pid = PID(p=0.09, i=0, d=0.002,imax=90) #脱机运行或者禁用图像传输,使用这个PID

2025-07-24 11:53
Z
星小瞳xs

不影响的:https://book.openmv.cc/MCU/pyb.html