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 调用是仍为进程阻断,无法实现连续移动

相关推荐
IT猿手1 天前
三维动态避障路径规划:基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB代码
算法·matlab·动态规划·无人机·路径规划·动态路径规划
Sanse_2 天前
(二)ubuntu18+ros melodic配置XT-Drone/gazebo无人机仿真平台,无人机添加mid360模型和仿真运行
ubuntu·无人机
EasyDSS2 天前
解析RTMP视频推流平台EasyDSS如何实现无人机推流直播
音视频·无人机
Evand J2 天前
【2026课题介绍】无人机集群时间与角度约束下的协同攻击算法,附代码例程的运行示例
matlab·无人机·协同·协同攻击
Deepoch2 天前
硬件赋能智能:Deepoc开发板如何成为无人机自主飞行的核心引擎
人工智能·无人机·具身模型·deepoc
Coovally AI模型快速验证3 天前
深度学习驱动的视频异常检测(VAD),AI如何让监控更智能?
人工智能·深度学习·目标检测·机器学习·自动驾驶·无人机
云卓SKYDROID3 天前
无人机舵机驱动模块技术解析
无人机·驱动·知识科普·高科技·云卓科技
EasyDSS3 天前
视频推流平台EasyDSS无人机推流直播技术在智慧消防场景中的应用
音视频·无人机
长沙京卓3 天前
低空经济赋能基层治理 望城区探索秸秆露天焚烧无人机智能管控新路径
无人机·源代码管理
renhongxia13 天前
基于多智能体深度强化学习的高炮反无人机算法
图像处理·人工智能·深度学习·无人机