【Window 10】AirSim基礎配置 (v1.3.1 + Visual Studio 2019 + UE 4.24)

【Window 10】AirSim基礎配置 (v1.3.1 + UE 4.24

1.引言

  AirSim是一個主要有微軟公司團隊開發的一個仿真框架,主要針對無人機和無人車進行仿真,特別是對於深度強化學習來說,是一個很好的仿真環境。在之前的博客中,曾對上一版本的AirSim在Windows 10中的配置進行了介紹,也有該版本在Ubuntu 18.04中的配置,大家可以參考本專欄的其他文章,本文主要對Windows 10中AirSim的配置進行介紹, 其版本爲1.3.1, 虛幻引擎版本爲4.24。

2.虛幻引擎

  AirSim中採用的仿真環境爲虛幻引擎,版本爲4.24,如果在Ubuntu 18.04中,我們也可以通過ROS(Robot Operating System, 機器人操作系統)來使用Gazebo來進行仿真,這裏我們就不贅述了。
1 下載Epic Game Launcher
  我們可以前往虛幻引擎官網進行下載,可能會需要註冊賬號等,下載地址:

https://www.unrealengine.com/zh-CN/download/ue_non_games

2 打開虛幻引擎,點擊庫,在這裏可以下載各種版本的虛幻引擎版本,這裏我們選擇的是4.24.3,由於虛幻引擎較大,可能需要下載較長時間。
在這裏插入圖片描述
在這裏插入圖片描述

3.配置AirSim

1.安裝Visual Studio 2019,並確保安裝Desktop Development with C++Windows 10 SDK 10.0.18362
2. 啓動Developer Command Prompt for VS 2019,將路徑修改到自己期望存放AirSim的位置,然後執行下面命令來cloneAirSim到本地:

git clone https://github.com/Microsoft/AirSim.git

可能會遇到問題:

C:\>git clone https://github.com/Microsoft/AirSim.git
'git' 不是內部或外部命令,也不是可運行的程序
或批處理文件。

這時,我們可以使用Visual Studio的安裝器,將Git相關的組件安裝上,之後就可以使用命令行來clone了。
在這裏插入圖片描述
3.修改路徑,並進行編譯

cd AirSim
build.cmd

此時會生成虛幻引擎的插件位置在 Unreal\Plugins,可以放在自己使用的虛幻引擎項目中。
4.準備虛幻引擎的仿真場景

您可以自己創建虛幻引擎中的項目來進行仿真,也可以使用AirSim內置的仿真場景,這裏們使用AirSim內置的仿真場景Blocks進行測試,如果您希望構建自己獨特的仿真環境,也可以參考我的其他博客。

啓動Developer Command Prompt for VS 2019,執行(路徑根據自己存放AirSim的位置進行更改)

cd C:\AirSim\Unreal\Environments\Blocks
update_from_git.bat

在資源管理器中打開該文件夾,雙擊Blocks.sln,使用Visual Studio 2019打開。
設置Blocks爲啓動項目,編譯設置DebugGame_EditorWin64,按F5開始運行。
當編譯完成後,即可看到
在這裏插入圖片描述
點擊播放(Play)即可看到一架無人機出現在仿真環境中:
在這裏插入圖片描述

4.代碼控制無人機

重新啓動虛幻引擎
啓動Developer Command Prompt for VS 2019,打開Blocks仿真環境,點擊Play運行,
而後,執行AirSim\PythonClient\multirotor路徑下的hello_drone.py文件,我用的是conda的命令行,如下:

cd C:\AirSim\PythonClient\multirotor
python hello_drone.py

代碼執行效果如下圖:
在這裏插入圖片描述
AirSim提供的hello_drone.py腳本的代碼爲:

import setup_path 
import airsim

import numpy as np
import os
import tempfile
import pprint
import cv2

# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)

state = client.getMultirotorState()
s = pprint.pformat(state)
print("state: %s" % s)

imu_data = client.getImuData()
s = pprint.pformat(imu_data)
print("imu_data: %s" % s)

barometer_data = client.getBarometerData()
s = pprint.pformat(barometer_data)
print("barometer_data: %s" % s)

magnetometer_data = client.getMagnetometerData()
s = pprint.pformat(magnetometer_data)
print("magnetometer_data: %s" % s)

gps_data = client.getGpsData()
s = pprint.pformat(gps_data)
print("gps_data: %s" % s)

airsim.wait_key('Press any key to takeoff')
client.takeoffAsync().join()

state = client.getMultirotorState()
print("state: %s" % pprint.pformat(state))

airsim.wait_key('Press any key to move vehicle to (-10, 10, -10) at 5 m/s')
client.moveToPositionAsync(-10, 10, -10, 5).join()

client.hoverAsync().join()

state = client.getMultirotorState()
print("state: %s" % pprint.pformat(state))

airsim.wait_key('Press any key to take images')
# get camera images from the car
responses = client.simGetImages([
    airsim.ImageRequest("0", airsim.ImageType.DepthVis),  #depth visualization image
    airsim.ImageRequest("1", airsim.ImageType.DepthPerspective, True), #depth in perspective projection
    airsim.ImageRequest("1", airsim.ImageType.Scene), #scene vision image in png format
    airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)])  #scene vision image in uncompressed RGBA array
print('Retrieved images: %d' % len(responses))

tmp_dir = os.path.join(tempfile.gettempdir(), "airsim_drone")
print ("Saving images to %s" % tmp_dir)
try:
    os.makedirs(tmp_dir)
except OSError:
    if not os.path.isdir(tmp_dir):
        raise

for idx, response in enumerate(responses):

    filename = os.path.join(tmp_dir, str(idx))

    if response.pixels_as_float:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
        airsim.write_pfm(os.path.normpath(filename + '.pfm'), airsim.get_pfm_array(response))
    elif response.compress: #png format
        print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
        airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
    else: #uncompressed array
        print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
        img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) # get numpy array
        img_rgb = img1d.reshape(response.height, response.width, 3) # reshape array to 4 channel image array H X W X 3
        cv2.imwrite(os.path.normpath(filename + '.png'), img_rgb) # write to png

airsim.wait_key('Press any key to reset to original state')

client.armDisarm(False)
client.reset()

# that's enough fun for now. let's quit cleanly
client.enableApiControl(False)

至此,已基本完成在Windows 10上的AirSim配置,恭喜你!


以下是廣告部分,本人不對內容及真實性負責,如果感興趣的話,大家可以自行查看

深度學習與PyTorch實戰

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章