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

相关推荐
workflower21 小时前
AI system product quality model
大数据·人工智能·机器学习·云计算·无人机
workflower1 天前
世界模型的内涵与发展
人工智能·机器人·云计算·无人机
YOLO数据集集合1 天前
玉米雄穗目标检测数据集 | 玉米雄穗 作物表型 智慧农业 无人机巡检 小样本检测9142期
人工智能·目标检测·无人机·玉米·玉米雄蕊·玉米雄穗
workflower1 天前
从生成式模型开始的技术延伸
人工智能·机器学习·机器人·云计算·无人机
Zootopia6262 天前
多架 eVTOL集群排班调度方案思考
人工智能·算法·数学建模·matlab·动态规划·无人机·evtol
feasibility.2 天前
把城市压缩成 2067 个盒子:CosFly 无人机跟踪数据管线论文解剖
人工智能·大模型·嵌入式·无人机·vla
YOLO数据集集合3 天前
无人机视角行人与车辆检测数据集 | 无人机航拍 行人检测 车辆检测 智慧城市 公共安全9140期
人工智能·目标检测·无人机·智慧城市·车辆识别·无人机视角
workflower3 天前
世界模型向产业上游发掘的热点
人工智能·机器学习·机器人·云计算·无人机
YOLO_DATA3 天前
YOLO27防震锤缺陷检测数据集 防震锤数据集 1000张 防震锤 带标注 voc yolo 2 类 目标检测
人工智能·深度学习·yolo·目标检测·计算机视觉·数据集·无人机
高升说3 天前
无人机避障方案取舍:单目、双目与激光雷达的工程对比
人工智能·无人机