diff --git a/main.py b/main.py index a78c449..9228697 100644 --- a/main.py +++ b/main.py @@ -97,7 +97,7 @@ PASSWORD = None # 服务器连接参数 SERVER_IP = "www.shelingxingqiu.com" SERVER_PORT = 50005 -HEARTBEAT_INTERVAL = 60 # 心跳间隔(秒) +HEARTBEAT_INTERVAL = 6 # 心跳间隔(秒) # 激光校准配置 CONFIG_FILE = "/root/laser_config.json" @@ -154,8 +154,8 @@ LASER_OFF_CMD = bytes([0xAA, MODULE_ADDR, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x00, 0x # 相机标定参数(用于距离估算) # FOCAL_LENGTH_PIX = 3800.0 # 焦距(像素) -FOCAL_LENGTH_PIX = 2200.0 # 焦距(像素) -REAL_RADIUS_CM = 15 # 靶心实际半径(厘米) +FOCAL_LENGTH_PIX = 1900.0 # 焦距(像素) +REAL_RADIUS_CM = 15 # 靶心实际半径(厘米) # TCP 连接状态 tcp_connected = False @@ -470,29 +470,35 @@ def parse_packet(data: bytes): def tcp_send_raw(data: bytes, max_retries=2) -> bool: - """通过 4G 模块的 AT 指令发送原始 TCP 数据包""" global tcp_connected if not tcp_connected: return False + with uart4g_lock: - for attempt in range(max_retries): + for _ in range(max_retries): cmd = f'AT+MIPSEND=0,{len(data)}' - if ">" not in at(cmd, ">", 1500): - time.sleep_ms(100) + if ">" not in at(cmd, ">", 2000): + time.sleep_ms(50) continue - time.sleep_ms(10) - full = data + b"\x1A" # AT 指令结束符 - try: - sent = uart4g.write(full) - if sent != len(full): + # 关键:确保把 data 全部写出去 + total = 0 + while total < len(data): + n = uart4g.write(data[total:]) + if not n or n < 0: + time.sleep_ms(1) continue - except: - continue + total += n - if "OK" in at("", "OK", 1000): + # 关键:再发结束符(不算进 payload) + uart4g.write(b"\x1A") + + # 等发送完成确认(不同固件可能是 SEND OK / OK / +MIPSEND) + r = at("", "OK", 5000) + if ("SEND OK" in r) or ("OK" in r) or ("+MIPSEND" in r): return True - time.sleep_ms(100) + + time.sleep_ms(50) return False @@ -684,10 +690,7 @@ def detect_circle(frame): circularity = 4 * np.pi * area / (perimeter ** 2) if circularity > 0.75 and len(cnt) >= 5: center, axes, angle = cv2.fitEllipse(cnt) - radius = axes[0] - if axes[1] < radius: - radius = axes[1] - radius /= 2 + radius = (axes[0] + axes[1]) / 4 best_center = (int(center[0]), int(center[1])) best_radius = int(radius) best_radius1 = best_radius @@ -790,7 +793,13 @@ def tcp_main(): time.sleep_ms(20) continue - data = uart4g.read() + if not uart4g_lock.try_acquire(): + time.sleep_ms(1) + continue + try: + data = uart4g.read() + finally: + uart4g_lock.release() if data: rx_buf += data # 解析 +MIPURC 消息 @@ -1351,7 +1360,7 @@ def cmd_str(): # 初始化硬件 init_ina226() - # load_laser_point() + load_laser_point() disp = display.Display() cam = camera.Camera(640, 480)