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

相关推荐
九河云19 小时前
无人机测绘数字化实践:AI 驱动地形建模与数据标注革新
科技·安全·无人机·数字化转型
FL16238631291 天前
无人机视角巡检数据集航拍建筑废物垃圾检测数据集VOC+YOLO格式3382张12类别
yolo·无人机
feiyangqingyun2 天前
Qt实时绘制飞行轨迹/移动轨迹实时显示/带旋转角度/平滑移动/效果一级棒/地面站软件开发/无人机管理平台
qt·无人机·集群地面站
EriccoShaanxi2 天前
加速度计如何助力大型无人机飞得更稳、更准、更智能?
无人机
云卓SKYDROID2 天前
无人机电气隔离与抗干扰技术概述
无人机·智慧安防·高科技·云卓科技·四光吊舱
云望无线图传模块2 天前
无人机远距离无线通信模块:突破空中通信的未来之钥
无人机·无线模块·无人机模块·远距离模块
A_SKYLINE2 天前
低空无人机“一网统飞”深度解构:从技术内核到产业落地,重构低空经济操作系统
人工智能·重构·无人机·产品经理·低空经济
mit6.8244 天前
[无人机sdk] Open Protocol | 协议包构造&验证
游戏引擎·无人机·cocos2d
中渝软通4 天前
无人机技术深度解析:警用、消防、清洗与反制系统的实现与应用
无人机
GIS 数据栈4 天前
【网盘下载】SegGISv2.0.0.1 无人机·遥感影像识别系统
无人机