From a4c3f3510b72697fba811b32b84a8e29bade1d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E5=96=B5?= Date: Wed, 17 Jun 2026 15:48:44 +0800 Subject: [PATCH] =?UTF-8?q?2026.6.17=E5=B0=8F=E6=9B=B4=E6=96=B0qgc?= =?UTF-8?q?=E7=94=9F=E6=88=90=E8=99=9A=E6=8B=9F=E6=97=A0=E4=BA=BA=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qgc虚拟无人机.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 qgc虚拟无人机.py diff --git a/qgc虚拟无人机.py b/qgc虚拟无人机.py new file mode 100644 index 0000000..ec6275b --- /dev/null +++ b/qgc虚拟无人机.py @@ -0,0 +1,44 @@ +import time +from pymavlink import mavutil + +# 1. 连接到本地的 QGC +# QGC 启动后会自动监听 UDP 14550 端口,这里该端口发送数据 +# source_system=1 表示伪装成系统 ID 为 1 的飞机 +gc_connection = mavutil.mavlink_connection('udpout:localhost:14550', source_system=1, source_component=1) + +# 2. 定义你想在地图上呈现的点的坐标(这里以北京天安门为例) +latitude = 39.9042 # 纬度 +longitude = 116.4074 # 经度 +altitude = 50.0 # 高度 (米) + +print(f"虚拟无人机已启动,正在向 QGC 发送坐标: {latitude}, {longitude}") +print("请打开 QGC 地图查看...") + +try: + while True: + # 3. 发送心跳包(必须发送,否则 QGC 不会理会后续数据) + # MAV_TYPE_QUADROTOR 表示伪装成一架四轴飞行器 + # MAV_AUTOPILOT_ARDUPILOT 表示伪装成 ArduPilot 飞控 + gc_connection.mav.heartbeat_send( + 2, + 3, + 0, 0, 0 + ) + + # 4. 发送 GPS 原始数据(GLOBAL_POSITION_INT) + # MAVLink 协议要求经纬度乘以 1e7(10000000)转换为整数 + gc_connection.mav.global_position_int_send( + time_boot_ms=int(time.time() * 1000) & 0xFFFFFFFF, + lat=int(latitude * 1e7), + lon=int(longitude * 1e7), + alt=int(altitude * 1000), # 毫米 + relative_alt=int(altitude * 1000), # 相对高度 + vx=0, vy=0, vz=0, # 速度为 0 + hdg=0 # 朝向正北 + ) + + # 每秒发送一次 + time.sleep(1) + +except KeyboardInterrupt: + print("\n已停止发送。") \ No newline at end of file