无人机云台跟踪目标实现
文章目录
前言
项目上需要实现云台跟随目标的移动而移动,即始终保持目标在画面中心。现简单记录下实现过程。
效果展示
整体架构流程
基于目标检测框的云台跟踪控制,通过计算目标中心与画面中心的偏移量,生成云台转动指令。
技术细节
偏移量计算
获取当前图像尺寸。
计算目标中心点坐标:
- target_x = x + width / 2
- target_y = y + height / 2
计算与图像中心的偏移量:
- dx = target_x - image_w / 2 (水平方向)
- dy = image_h / 2 - target_y (垂直方向,注意Y轴向上为正)
注意点:图像坐标系以左上角为原点,水平向右为x正,垂直向下为y正。而云台控制中垂直方向(dy)计算时取反,使其符合“向上为正”的常规。
死区设置与比例控制
水平方向:
- 若|dx| < 100像素,则yaw = 0(不转动);
- 否则,yaw = dx * 0.2(比例系数)。
垂直方向:
- 若|dy| < 100像素,则pitch = 0(不转动);
- 否则,pitch = dy * 0.2(比例系数)。
具体参数设置,需要实际测试调节;
部分代码展示
def target_tracking(self, que): import json TOPIC = \"/fly/test00/test01/ctrl\" print(\"\",TOPIC) key = \"demo123456\" try: while True: target_bbox = que.get() # 安全终止机制 if target_bbox is None: print(\"收到终止信号,关闭连接\") break image_h, image_w = self.im0s.shape[:2] # 1. 计算目标中心与图像中心的偏移 target_x = target_bbox[0] + target_bbox[2] / 2 target_y = target_bbox[1] + target_bbox[3] / 2 dx = target_x - image_w / 2 dy = image_h / 2 - target_y # 注意Y轴方向取反 # 2. 设置死区(偏移小于50像素时不动作) if abs(dx) < 100: yaw = 0 else: yaw = dx * 0.2 # 比例系数0.2(与原逻辑一致) if abs(dy) < 100: pitch = 0 else: pitch = dy * 0.2 # 比例系数0.2(与原逻辑一致) # 3. 四舍五入取整(符合接口int类型要求) pitch = int(round(pitch)) yaw = int(round(yaw)) print(f\"Sent command: pitch={pitch}, yaw={yaw}\") if pitch == 0 and yaw == 0: continue elif pitch == 0: message = { \"gimbal\": { \"yaw\": yaw } } elif yaw == 0: message = { \"gimbal\": { \"pitch\": pitch } } else: message = { \"gimbal\": { \"pitch\": pitch, \"yaw\": yaw } } # 发布消息 plaintext_json = json.dumps(message, ensure_ascii=False, separators=(\',\', \':\')) cipher = aes_encrypt_ecb(plaintext_json, key) print(\"Encrypted:\", cipher) publish_result = self.mqtt_client_payload.on_publish(TOPIC, cipher) except Exception as e: print(f\"处理异常: {str(e)}\")
小结
简单实现基于目标检测的云台自动跟踪系统,实时计算目标与画面中心的偏移量,通过比例控制生成云台转动指令。设置±100像素死区避免抖动,单轴指令优化减少数据传输,采用AES加密保障通信安全。适用于无人机追踪、安防监控等需要实时目标跟随的系统。
参考文档:
https://developer.dji.com/cn/onboard-sdk/documentation/sample-doc/advanced-sensing-target-tracking.html
如果阅读本文对你有用,欢迎一键三连呀!!!
2025年7月24日15:15:03