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

相关推荐
无人机长了一个脑袋3 小时前
无人机序列号
无人机
airuike1238 小时前
高性能MEMS IMU:重构无人机飞行控制核心
人工智能·算法·重构·无人机
洛阳吕工8 小时前
【Python 教程】无人机 MAVLink 通信完整实战:连接飞控、接收数据与发送指令
开发语言·python·无人机
GIS数据转换器1 天前
延凡智慧水务系统:引领行业变革的智能引擎
大数据·人工智能·无人机·智慧城市
洛阳吕工1 天前
无人机新手入门指南——从选型到首飞完整流程
无人机
foundbug9991 天前
无人机离散系统模型预测控制(MPC)MATLAB实现
开发语言·matlab·无人机
KOYUELEC光与电子努力加油3 天前
JAE日本航空端子推出支持自走式机器人的自主充电功能浮动式连接器“DW15系列“方案与应用
服务器·人工智能·机器人·无人机
REDcker3 天前
无人机 Remote ID(RID)广播与技术标准概览
无人机
LittroInno4 天前
边缘AI目标检测与跟踪——模块化技术架构对比分析
人工智能·目标检测·目标跟踪·架构·无人机
Deepoch4 天前
Deepoc具身模型开发板:无人机集群去中心化协同的VLA中枢
人工智能·无人机·具身模型·deepoc