AirSim键盘飞行控制

钰见梵星 / 2024-01-31 / 原文

鼠标事件检测

import sys
import pygame

pygame.init()										# 初始化所有导入的pygame模块
screen = pygame.display.set_mode((640, 480))		# 创建一个大小为 640x480 像素的窗口
pygame.display.set_caption('mouse ctrl')			# 设置窗口的标题
screen.fill((0, 0, 0))								# 设置窗口的背景颜色为黑色(RGB: (0, 0, 0))

while True:
    for event in pygame.event.get():				# 无限循环用于不断地检查和处理事件
        if event.type == pygame.QUIT:
            sys.exit()					# 点击窗口的关闭按钮时,触发pygame.QUIT,代码调用sys.exit()来退出程序

        # >------>>>  处理鼠标事件   <<<------< #
        if event.type == pygame.MOUSEBUTTONDOWN:	# 按下鼠标
            print("Mouse Down: ", event)
        if event.type == pygame.MOUSEBUTTONUP:		# 松开鼠标
            print("Mouse Up", event)
        if event.type == pygame.MOUSEMOTION:		# 移动鼠标
            print("Mouse is moving now: ", event)

        # >------>>>  处理键盘事件   <<<------< #
        if event.type == pygame.KEYDOWN:			# 按下键盘上的回车
            if event.key == pygame.K_RETURN:
                print("keyboard event: ", event)

键盘按键检测

import sys
import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('keyboard ctrl')
screen.fill((0, 0, 0))

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    scan_wrapper = pygame.key.get_pressed()		# 函数返回一个包含所有键盘按键状态的元组
    print("pressed keys is ", scan_wrapper)

    # press 'Esc' to quit
    if scan_wrapper[pygame.K_ESCAPE]:			# Esc退出
        pygame.quit()
        sys.exit()

字母及方向按键检测

import sys
import pygame

pygame.init()

screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('keyboard ctrl')
screen.fill((0, 0, 0))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    keys = pygame.key.get_pressed()
    for i in range(26):
        if keys[pygame.K_a + i]:  # 检测从 A 到 Z 的按键
            print(chr(pygame.K_a + i))		# a+ACSII码,在强制类型转换为字符

    # 检测上下左右键
    if keys[pygame.K_UP]:
        print("Up arrow")
    if keys[pygame.K_DOWN]:
        print("Down arrow")
    if keys[pygame.K_LEFT]:
        print("Left arrow")
    if keys[pygame.K_RIGHT]:
        print("Right arrow")

    # 按下 'Esc' 退出程序
    if keys[pygame.K_ESCAPE]:
        pygame.quit()
        sys.exit()

键盘飞控

import sys
import time
import airsim
import pygame
import CV2
import numpy as np

# >------>>>  pygame settings   <<<------< #
pygame.init()
screen = pygame.display.set_mode((320, 240))
pygame.display.set_caption('keyboard ctrl')
screen.fill((0, 0, 0))

# >------>>>  AirSim settings   <<<------< #
# 这里改为你要控制的无人机名称(settings文件里面设置的)
# vehicle_name = "Drone"
AirSim_client = airsim.MultirotorClient()
AirSim_client.confirmConnection()
AirSim_client.enableApiControl(True)
AirSim_client.armDisarm(True)
AirSim_client.takeoffAsync().join()

# 基础的控制速度(m/s)
vehicle_velocity = 2.0
# 设置临时加速比例
speedup_ratio = 10.0
# 用来设置临时加速
speedup_flag = False

# 基础的偏航速率
vehicle_yaw_rate = 5.0

while True:
    yaw_rate = 0.0
    velocity_x = 0.0
    velocity_y = 0.0
    velocity_z = 0.0

    time.sleep(0.02)									# 检测时间间隔为0.02s

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    scan_wrapper = pygame.key.get_pressed()

    # 按下空格键加速10倍
    if scan_wrapper[pygame.K_SPACE]:
        scale_ratio = speedup_ratio						# 加速倍率,若按空格则为10倍,否则是1倍
    else:
        scale_ratio = speedup_ratio / speedup_ratio

    # 根据 'A' 和 'D' 按键来设置偏航速率变量
    if scan_wrapper[pygame.K_a] or scan_wrapper[pygame.K_d]:
        yaw_rate = (scan_wrapper[pygame.K_d] - scan_wrapper[pygame.K_a]) * scale_ratio * vehicle_yaw_rate		# d-a为1顺时针偏航,否则逆时针

    # 根据 'UP' 和 'DOWN' 按键来设置pitch轴速度变量(NED坐标系,x为机头向前)	同时也是前进后退
    if scan_wrapper[pygame.K_UP] or scan_wrapper[pygame.K_DOWN]:
        velocity_x = (scan_wrapper[pygame.K_UP] - scan_wrapper[pygame.K_DOWN]) * scale_ratio

    # 根据 'LEFT' 和 'RIGHT' 按键来设置roll轴速度变量(NED坐标系,y为正右方)	 同时也是左右飞行
    if scan_wrapper[pygame.K_LEFT] or scan_wrapper[pygame.K_RIGHT]:
        velocity_y = -(scan_wrapper[pygame.K_LEFT] - scan_wrapper[pygame.K_RIGHT]) * scale_ratio

    # 根据 'W' 和 'S' 按键来设置z轴速度变量(NED坐标系,z轴向上为负)			同时也是上升下降
    if scan_wrapper[pygame.K_w] or scan_wrapper[pygame.K_s]:
        velocity_z = -(scan_wrapper[pygame.K_w] - scan_wrapper[pygame.K_s]) * scale_ratio

    # print(f": Expectation gesture: {velocity_x}, {velocity_y}, {velocity_z}, {yaw_rate}")

    # 设置速度控制以及设置偏航控制
    AirSim_client.moveByVelocityBodyFrameAsync(vx=velocity_x, vy=velocity_y, vz=velocity_z, duration=0.02,yaw_mode=airsim.YawMode(True, yaw_or_rate=yaw_rate))

    if scan_wrapper[pygame.K_ESCAPE]:
        pygame.quit()
        sys.exit()

卡顿现象:编辑——编辑器偏好设置——搜索CPU——取消勾选处于背景中时占用较少CPU

或者通过一台电脑运行airsim,一台电脑运行python的通信方式来运行