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

相关推荐
猿大师播放器8 小时前
网页Web端无人机直播RTSP视频流,无需服务器转码,延迟300毫秒
无人机·h.265·rtsp
调了个寂寞1 天前
无人机俯视风光摄影Lr调色预设,手机滤镜PS+Lightroom预设下载!
无人机·lr调色·人像预设·摄影后期·lr预设·文艺预设
UAV_ckesc1 天前
无人机动力系统全解析:核心组件、工作原理与实用指南
无人机·多旋翼无人机·无人机电调·无人机动力·南昌长空科技·无人机配件
风暴智能1 天前
问题处理——在ROS2(humble)+Gazebo+rqt下,无法显示仿真无人机的相机图像
linux·无人机
云卓SKYDROID2 天前
无人机数据处理与特征提取技术分析!
人工智能·科技·无人机·科普·云卓科技
合新通信 | 让光不负所托2 天前
【合新通信】无人机天线拉远RFOF(射频光纤传输)解决方案
无人机
视觉语言导航2 天前
昆士兰科技大学无人机自主导航探索新框架!UAVNav:GNSS拒止与视觉受限环境中的无人机导航与目标检测
人工智能·无人机·具身智能
视觉语言导航2 天前
武汉大学无人机视角下的多目标指代理解新基准!RefDrone:无人机场景指代表达理解数据集
人工智能·深度学习·无人机·具身智能
视觉语言导航2 天前
AAAI-2025 | 中科院无人机导航新突破!FELA:基于细粒度对齐的无人机视觉对话导航
人工智能·深度学习·机器人·无人机·具身智能
孚为智能科技2 天前
无人机箱号识别系统结合5G技术的应用实践
图像处理·人工智能·5g·目标检测·计算机视觉·视觉检测·无人机