AirSim自己操控?zé)o人機(jī)——pygame環(huán)境檢測(cè)+具體代碼實(shí)現(xiàn)
一、Pygame鼠標(biāo)事件簡(jiǎn)單檢測(cè)
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('mouse ctrl')
screen.fill((0, 0, 0))
while True:
? ?for event in pygame.event.get():
? ? ? ?if event.type == pygame.QUIT:
? ? ? ? ? ?sys.exit()
? ? ? ?# >------>>> ?處理鼠標(biāo)事件 ? <<<------< #
? ? ? ?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)
二、Pygame鍵盤全按鍵簡(jiǎn)單檢測(cè)
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]:
? ? ? ?pygame.quit()
? ? ? ?sys.exit()
三、Pygame核心26個(gè)字母及上下左右按鍵檢測(cè)
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]: ?# 檢測(cè)從 A 到 Z 的按鍵
? ? ? ? ? ?print(chr(pygame.K_a + i))
? ?# 檢測(cè)上下左右鍵
? ?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()
四、AirSim使用Pygame在UE4中實(shí)現(xiàn)自己用鍵盤控制無人機(jī)飛行
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 ? <<<------< #
# 這里改為你要控制的無人機(jī)名稱(settings文件里面設(shè)置的)
vehicle_name = "Drone"
AirSim_client = airsim.MultirotorClient()
AirSim_client.confirmConnection()
AirSim_client.enableApiControl(True, vehicle_name=vehicle_name)
AirSim_client.armDisarm(True, vehicle_name=vehicle_name)
AirSim_client.takeoffAsync(vehicle_name=vehicle_name).join()
# 基礎(chǔ)的控制速度(m/s)
vehicle_velocity = 2.0
# 設(shè)置臨時(shí)加速比例
speedup_ratio = 10.0
# 用來設(shè)置臨時(shí)加速
speedup_flag = False
# 基礎(chǔ)的偏航速率
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)
? ?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
? ?else:
? ? ? ?scale_ratio = speedup_ratio / speedup_ratio
? ?# 根據(jù) 'A' 和 'D' 按鍵來設(shè)置偏航速率變量
? ?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
? ?# 根據(jù) 'UP' 和 'DOWN' 按鍵來設(shè)置pitch軸速度變量(NED坐標(biāo)系,x為機(jī)頭向前)
? ?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
? ?# 根據(jù) 'LEFT' 和 'RIGHT' 按鍵來設(shè)置roll軸速度變量(NED坐標(biāo)系,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
? ?# 根據(jù) 'W' 和 'S' 按鍵來設(shè)置z軸速度變量(NED坐標(biāo)系,z軸向上為負(fù))
? ?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}")
? ?# 設(shè)置速度控制以及設(shè)置偏航控制
? ?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), vehicle_name=vehicle_name)
? ?if scan_wrapper[pygame.K_ESCAPE]:
? ? ? ?pygame.quit()
? ? ? ?sys.exit()