DJITelloPy 控制无人机飞行

python 复制代码
from pynput import keyboard
from djitellopy import Tello
import cv2, math, time


def on_press(key):
    char = key.char
    if char == 'w':
        print(123)
        tello.move_forward(20)
    if char == 'a':
        tello.move_left(20)
    if char == 's':
        tello.move_back(20)
    if char == 'd':
        tello.move_right(20)

    if char == 'q':
        tello.rotate_clockwise(-10)
    if char == 'e':
        tello.rotate_clockwise(10)
    if char == 'r':
        tello.move_up(20)
    if char == 'f':
        tello.move_down(20)
def on_release(key):
    print('释放了:{}'.format(key) )
    if key == keyboard.Key.esc:
        tello.end()
        return False
listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release,)


tello = Tello()
try:
    tello.connect()
    tello.takeoff()
    print("无人机起飞完成")

    # 开启一个键盘监听
    listener.start()
    time.sleep(50)
except Exception as e:
    # 如果出现异常,打印错误信息
    print(f"An error occurred: {e}")

finally:
    # 不论是否发生异常,最后都断开连接
    tello.end()

改进 将监听之后对无人机的操作指令 和 键盘监听分为两个进程,不在同一个进程里执行,避免键盘监听进程阻塞

python 复制代码
from pynput import keyboard
from djitellopy import Tello
import cv2, math, time
from keyboardListener import KeyboardListener

tello = Tello()
try:
    tello.connect()
    tello.takeoff()
    print("无人机起飞完成")

    # 开启一个键盘监听
    listener = KeyboardListener()
    listener.start_listener()
    while True:
        pressSet = listener.get_pressKeySet()
        if pressSet == {'w'}:
            tello.move_forward(20)
        if pressSet == {'a'}:
            tello.move_left(20)
        if pressSet == {'s'}:
            tello.move_back(20)
        if pressSet == {'d'}:
            tello.move_right(20)

        if pressSet == {'q'}:
            tello.rotate_clockwise(-10)
        if pressSet == {'e'}:
            tello.rotate_clockwise(10)
        if pressSet == {'r'}:
            tello.move_up(20)
        if pressSet == {'f'}:
            tello.move_down(20)
        
        if pressSet == {keyboard.Key.esc}:
            listener.stop_listener()
            tello.land()
            break
except Exception as e:
    # 如果出现异常,打印错误信息
    print(f"An error occurred: {e}")

finally:
    # 不论是否发生异常,最后都断开连接
    tello.end()

仍然存在问题 tello.move api 调用是仍为进程阻断,无法实现连续移动

相关推荐
Deepoch22 分钟前
Deepoc具身模型外拓板:无人机从“会飞的相机”到“会思考的搭档”的跨越
科技·无人机·开发板·未来·具身模型·deepoc
xqqxqxxq26 分钟前
《智能仿真无人机平台(多线程V3.0)技术笔记》
笔记·无人机·cocos2d
大势智慧1 小时前
低空大师2025飞行报告
无人机·生态·低空经济·技术突破·场景应用·年度回顾·创新升级
延凡科技9 小时前
无人机低空智能巡飞巡检平台:全域感知与智能决策的低空作业中枢
大数据·人工智能·科技·安全·无人机·能源
Together_CZ12 小时前
无人机助力道路智能养护,基于最新端到端范式YOLO26全系列【n/s/m/l】参数模型开发构建无人机航拍道路交通场景下水泥路面缺陷智能检测识别系统
无人机·无人机助力道路智能养护·无人机航拍道路交通场景·yolo26·最新端到端范式·水泥路面缺陷智能检测识别·道路巡检养护
sxgzzn12 小时前
低空经济新实践:无人机如何革新光伏电站巡检
无人机
FL162386312912 小时前
无人机视角河道巡检治理垃圾淤泥植被排水沟障碍物识别分割数据集labelme格式2777张12类别
无人机
芒果de香蕉皮20 小时前
开源飞控APM备降点源码分析
无人机·二次开发·apm·开源飞控·备降点
Coovally AI模型快速验证21 小时前
YOLO26技术详解:原生NMS-Free架构设计与实现原理
人工智能·计算机视觉·开源·音视频·无人机
星辰亦晓1 天前
px4+ubuntu22.04+ros2开发记录
机器人·无人机