用户使用 OpenMV4 H7 Plus 运行自研 LK 光流算法(64×64 灰度图,软件 Timer 50Hz 触发),输出弧度×10000 后通过串口发送至 STM32F4;同时接入优象光流模块(50Hz 硬件时钟),两者数据均经串口送入 STM32 做帧序号级比对。现象:绿线(OpenMV)与蓝线(优象)运动趋势长期无法对齐,非固定相位差,而是整体失同步。
import sensor
import math
import time # 用于计算FPS
from pyb import UART, LED, Timer
# ==================== 光流参数 ====================
W = 64
H = 64
SMOOTH = 0.65 # 仅从 0.7 → 0.65(几乎不影响平滑,但响应变快)
WINDOW = 3
H_FOV = 72.0
V_FOV = 55.0
H_RAD = H_FOV *math.pi / 180.0
V_RAD = V_FOV *math.pi / 180.0
SCALE_X = H_RAD / W
SCALE_Y = V_RAD / H
OUTPUT_SCALE = 10000
# ==================== 摄像头初始化(关键:调亮) ====================
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.B64X64)
sensor.set_auto_exposure(False, exposure_us=10000) # 提高曝光
sensor.set_auto_gain(False, gain_db=15) # 提高增益
sensor.set_auto_whitebal(False)
sensor.skip_frames(100)
# ==================== 串口与定时器 ====================
uart = UART(3, 115200, timeout_char=200)
led = LED(2)
send_flag = False
tim = Timer(4, freq=50)
def timer_callback(t):
global send_flag
send_flag = True
tim.callback(timer_callback)
# ==================== LK光流 ====================
def optical_flow_lk(old_img, new_img, x, y, win, max_iter=5):
dx = 0.0
dy = 0.0
for i in range(max_iter):
A = B = C = Dx = Dy = 0.0
count = 0
for ky in (-win // 2, win // 2 + 1):
for kx in (-win // 2, win // 2 + 1):
gx = int(x + kx + dx)
gy = int(y + ky + dy)
if 1 <= gx < W - 1 and 1 <= gy < H - 1:
ix = old_img.get_pixel(gx + 1, gy) - old_img.get_pixel(gx - 1, gy)
iy = old_img.get_pixel(gx, gy + 1) - old_img.get_pixel(gx, gy - 1)
it = old_img.get_pixel(gx, gy) - new_img.get_pixel(gx, gy)
A += ix *ix
B += ix *iy
C += iy *iy
Dx += ix *it
Dy += iy *it
count += 1
if count < 3:
break
det = A *C - B *B
if abs(det) < 0.1:
break
dx_inc = (B *Dy - C *Dx) / det
dy_inc = (B *Dx - A * Dy) / det
dx += dx_inc
dy += dy_inc
if abs(dx_inc) < 0.03 and abs(dy_inc) < 0.03:
break
return dx, dy
def get_grid_points(step=16):
points = []
for y in range(step, H - step, step):
for x in range(step, W - step, step):
points.append((x, y))
return points
# ==================== 发送协议 ====================
def send_flow(out_x, out_y, valid):
buf = bytearray(14)
buf[0] = 0xFE
buf[1] = 0x0A
buf[2] = int(out_x) & 0xFF
buf[3] = (int(out_x) >> 8) & 0xFF
buf[4] = int(out_y) & 0xFF
buf[5] = (int(out_y) >> 8) & 0xFF
buf[6] = 0
buf[7] = 0
buf[8] = 0xE7
buf[9] = 0x03
buf[10] = 0xF5 if valid else 0x00
buf[11] = 0x11
xor = 0
for i in range(2, 12):
xor ^= buf[i]
buf[12] = xor
buf[13] = 0x55
uart.write(buf)
# ==================== 主循环 ====================
points = get_grid_points(step=16)
old_img = sensor.snapshot()
smooth_dx = 0.0
smooth_dy = 0.0
STATIC_THRESHOLD = 0.15 # 从 0.3 → 0.15(不吞小幅运动)
# ==================== FPS 计算变量 ====================
frame_count = 0
start_time = time.ticks_ms()
current_fps = 47 # 默认初始值
while True:
new_img = sensor.snapshot()
# 实时计算 FPS(每秒更新一次)
frame_count += 1
if time.ticks_diff(time.ticks_ms(), start_time) >= 1000:
current_fps = frame_count
frame_count = 0
start_time = time.ticks_ms()
sum_dx = 0.0
sum_dy = 0.0
cnt = 0
for (x, y) in points:
dx, dy = optical_flow_lk(old_img, new_img, x, y, WINDOW)
# ==================== ✅ 只改这一行:4.0 → 5.1 ====================
if abs(dx) < 5.1 and abs(dy) < 5.1:
sum_dx += dx
sum_dy += dy
cnt += 1
if cnt > 0:
avg_dx = sum_dx / cnt
avg_dy = sum_dy / cnt
valid = True
led.on()
else:
avg_dx = 0
avg_dy = 0
valid = False
led.off()
# 平滑滤波
smooth_dx = SMOOTH * smooth_dx + (1 - SMOOTH) * avg_dx
smooth_dy = SMOOTH * smooth_dy + (1 - SMOOTH) * avg_dy
# 静态归零
if abs(smooth_dx) < STATIC_THRESHOLD:
smooth_dx = 0
if abs(smooth_dy) < STATIC_THRESHOLD:
smooth_dy = 0
# 像素 -> 弧度 -> 输出
rad_x = smooth_dx * SCALE_X
rad_y = smooth_dy * SCALE_Y
output_x = rad_x * OUTPUT_SCALE
output_y = rad_y * OUTPUT_SCALE
# ==================== ✅ send 完全保持原状 ====================
send_x = round(output_x)
send_y = round(output_y)
old_img = new_img
# 50Hz发送
if send_flag:
send_flag = False
send_flow(send_x, send_y, valid)
# 输出:实时FPS,不再写死47
print("FPS:{}, X:{}, Y:{}".format(current_fps, send_x, send_y))
OpenMV的定时器是准的,但是运行算法和串口发送会消耗处理器时间,即实际运行时间会略微
大于20ms