67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
# main.py - 主业务逻辑(人脸跟踪、舵机控制、LED)
|
|
from maix import camera, display, image, nn, app, sys
|
|
from maix import uart, pinmap, time, err, gpio
|
|
from maix import app, nn
|
|
import traceback
|
|
from config import *
|
|
from test_ball import *
|
|
|
|
# ------------------------------
|
|
# 系统初始化
|
|
# ------------------------------
|
|
def init_system():
|
|
"""初始化相机、舵机、串口、LED"""
|
|
global serial_dev # 声明使用全局serial_dev
|
|
|
|
init_led()
|
|
|
|
# 人脸检测器
|
|
detector = nn.YOLOv8(model="/root/models/yolov8n_face.mud", dual_buff=True)
|
|
img_width = detector.input_width()
|
|
img_height = detector.input_height()
|
|
print(f"[系统] 图像尺寸: {img_width}x{img_height}")
|
|
image_center = (img_width // 2, img_height // 2)
|
|
|
|
# 相机/显示器
|
|
cam = camera.Camera(img_width, img_height, detector.input_format())
|
|
disp = display.Display()
|
|
|
|
# 串口(舵机)
|
|
pin_function = {"A19": "UART1_TX", "A18": "UART1_RX"}
|
|
device = "/dev/ttyS1"
|
|
for pin, func in pin_function.items():
|
|
err.check_raise(pinmap.set_pin_function(pin, func),
|
|
f"Failed set pin{pin} function to {func}")
|
|
serial_dev = uart.UART(device, 115200)
|
|
|
|
# 初始化值 距离中间点像素距离
|
|
initial_cmd = f"DX:0,DY:0;"
|
|
serial_dev.write_str(initial_cmd)
|
|
print(f"[舵机] 初始角度: {initial_cmd}")
|
|
time.sleep(1)
|
|
|
|
return {
|
|
"detector": detector, "cam": cam, "disp": disp, "serial_dev": serial_dev,
|
|
"img_width": img_width, "img_height": img_height, "image_center": image_center
|
|
}
|
|
|
|
|
|
# ------------------------------
|
|
# 系统执行
|
|
# ------------------------------
|
|
|
|
try:
|
|
# 初始化系统资源
|
|
system_resources = init_system()
|
|
|
|
update_led()
|
|
|
|
# 进入主循环
|
|
trace_ball_main(system_resources)
|
|
|
|
except Exception as e:
|
|
print(f"[异常] 程序异常: {e}")
|
|
traceback.print_exc()
|
|
|
|
|