From 34b93b2fef51ca23697fb278f83e1b3c107aff2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E5=96=B5?= Date: Wed, 10 Jun 2026 17:41:43 +0800 Subject: [PATCH] =?UTF-8?q?2036.6.10=E5=B0=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c_test.py | 17 +++++++++++++++++ config.json | 2 +- control.py | 2 +- drone_controller/base_control.py | 5 ++--- 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 c_test.py diff --git a/c_test.py b/c_test.py new file mode 100644 index 0000000..fd55888 --- /dev/null +++ b/c_test.py @@ -0,0 +1,17 @@ +import time +from pymavlink import mavutil + +# 根据你的实际 UDP 配置修改 +# 如果飞控是作为 UDP 服务端(监听),则 Python 应作为客户端连接: +# connection_string = "udpout:飞控IP:14550" +# 如果飞控是作为 UDP 客户端(主动连接 Python),则 Python 应作为服务端监听: +connection_string = "udpin:127.0.0.1:14550" + +master = mavutil.mavlink_connection(connection_string) + +print("等待飞控心跳包...") +try: + master.wait_heartbeat(timeout=5) + print(f"收到心跳,系统 ID: {master.target_system}, 组件 ID: {master.target_component}") +except: + print("超时:未收到飞控心跳包,请检查 IP 和端口配置以及飞控端是否在发送心跳") \ No newline at end of file diff --git a/config.json b/config.json index 00b3676..3d1364d 100644 --- a/config.json +++ b/config.json @@ -13,7 +13,7 @@ "udp_port": 9999 }, "flight_control": { - "connection_string": "udpout:127.0.0.1:14550", + "connection_string": "AMA1", "baud_rate": 57600 }, "fc_info":{ diff --git a/control.py b/control.py index 3ff4d43..eb60ffc 100644 --- a/control.py +++ b/control.py @@ -45,7 +45,7 @@ class PID: class UAVController: - def __init__(self, port='/dev/ttyUSB0', baud=57600): + def __init__(self, port='/dev/ttyAMA1', baud=57600): """ 初始化与飞控的 MAVLink 串口连接 port:串口地址 diff --git a/drone_controller/base_control.py b/drone_controller/base_control.py index 35acfcb..dbc1b1a 100644 --- a/drone_controller/base_control.py +++ b/drone_controller/base_control.py @@ -5,7 +5,7 @@ from pymavlink import mavutil class DroneController: - def __init__(self, connection_string='udpout:127.0.0.1:14550', baud=115200): + def __init__(self, connection_string='/dev/ttyAMA1', baud=115200): self.connection_string = connection_string self.baud = baud self.master = None @@ -57,8 +57,7 @@ class DroneController: """初始化 MAVLink 串口连接,并等待首个心跳包""" try: print(f"正在尝试连接飞控 ({self.connection_string})...") - # 当使用udp时网络不再重要 - self.master = mavutil.mavlink_connection(self.connection_string) + self.master = mavutil.mavlink_connection(self.connection_string, baud=self.baud) self.master.wait_heartbeat(timeout=5) self.is_connected = True self.last_heartbeat_time = time.time()