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

相关推荐
哈泽尔都12 小时前
运动控制教学——5分钟学会PRM算法!
人工智能·单片机·算法·数学建模·贪心算法·机器人·无人机
ARM+FPGA+AI工业主板定制专家4 天前
基于ZYNQ FPGA+AI+ARM 的卷积神经网络加速器设计
人工智能·fpga开发·cnn·无人机·rk3588
时空自由民.5 天前
无人机系统耗电,低功耗管理问题解决方法(chatgpt)
单片机·嵌入式硬件·无人机
时空自由民.5 天前
无人机系统耗电,低功耗管理问题解决方法(腾讯元宝)
单片机·嵌入式硬件·无人机
mtouch3335 天前
GIS+VR地理信息虚拟现实XR MR AR
大数据·人工智能·ar·无人机·xr·vr·mr
FlyingMindIndex05 天前
无人机_鲁棒性
无人机
timmy-uav5 天前
PX4-Autopilot代码解析(2)-系统架构
系统架构·自动驾驶·无人机·飞控·px4
夜听莺儿鸣6 天前
801-203_各无人机厂家对RemoteID支持情况汇总
无人机
云卓SKYDROID6 天前
无人机定点派送技术要点与运行方式
人工智能·无人机·航电系统·高科技·云卓科技
大毛无人机6 天前
无人机找工作去哪找?无人机接单app
无人机