第一次打开虚幻引擎时,那个闪烁着蓝色光芒的启动画面总让人有种即将开启新世界的兴奋感——直到你发现需要配置的依赖项列表比超市购物单还长。作为微软研究院开源的无人机仿真平台,AirSim确实为算法研究提供了绝佳的试验场,但它的环境搭建过程却像在玩真人版"扫雷"游戏。本文将带你用最稳妥的方式穿越这片雷区。
在安装任何软件之前,请先检查你的Windows系统版本。AirSim对Windows 10/11的专业版和企业版支持最好,家庭版可能会遇到Hyper-V相关的问题。建议确保系统已更新至最新版本,并预留至少100GB的可用磁盘空间。
必备组件清单:
注意:Python 3.10+目前与AirSim存在兼容性问题,这也是我们选择3.9版本的原因
安装Visual Studio时,务必勾选以下工作负载:
powershell复制# 验证安装的VS工具集版本
Get-ChildItem "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC" | Sort-Object Name -Descending | Select-Object -First 1
Epic Games启动器默认安装的是最新版UE,我们需要特别指定4.27版本:
安装完成后,建议创建一个专用的项目目录,例如:
code复制D:\AirSim_Projects\
常见问题排查:
C:\Users\[用户名]\AppData\Local\EpicGamesLauncher\Saved\Config\Windows下的所有文件现在来到最具挑战性的环节——编译AirSim插件。我们将采用最稳妥的"两步编译法":
bash复制# 第一步:克隆仓库并初始化子模块
git clone https://github.com/Microsoft/AirSim.git
cd AirSim
git checkout v1.8.1 # 确保版本匹配
git submodule update --init --recursive
接下来需要修改两处关键配置:
AirSim\Unreal\Plugins\AirSim\Source\AirSim\AirSim.Build.cs:csharp复制// 约第25行,添加以下定义
PublicDefinitions.Add("_CRT_SECURE_NO_WARNINGS");
PublicDefinitions.Add("_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS");
AirSim\Unreal\Environments\AirSimNH\Plugins\AirSim\Source\AirLib\deps\rpclib\include\rpc\msgpack\v1\object_fwd.hpp文件,内容为:cpp复制#pragma once
#include <msgpack/object_fwd.hpp>
现在可以开始编译了:
powershell复制# 在AirSim目录下执行
build.cmd
提示:编译过程可能需要30-60分钟,期间CPU温度可能较高,建议关闭其他大型程序
我们使用Miniconda创建独立的Python环境:
bash复制conda create -n airsim python=3.9.13
conda activate airsim
pip install msgpack-rpc-python==0.4.1
pip install airsim
验证安装是否成功:
python复制import airsim
client = airsim.MultirotorClient()
client.confirmConnection()
print(client.getServerVersion())
典型错误解决方案:
| 错误现象 | 可能原因 | 解决方案 |
|---|---|---|
| ImportError: DLL load failed | VC++运行时缺失 | 安装VC_redist.x64.exe |
| TypeError: unsupported operand type(s) | msgpack版本冲突 | pip install msgpack-rpc-python==0.4.1 |
| Connection refused | AirSim未启动 | 先运行UE4项目 |
官方提供了多个预制场景,我们以"城市环境"为例:
Neighborhood.zipNeighborhood\WindowsNoEditor\Neighborhood.exeC:\Users\[用户名]\Documents\AirSim\settings.json:json复制{
"SettingsVersion": 1.2,
"SimMode": "Multirotor",
"Vehicles": {
"Drone1": {
"VehicleType": "SimpleFlight",
"AutoCreate": true,
"Cameras": {
"front_center": {
"CaptureSettings": [
{
"ImageType": 0,
"Width": 640,
"Height": 480
}
]
}
}
}
}
}
现在可以运行这个基础控制脚本:
python复制import airsim
import time
client = airsim.MultirotorClient()
client.enableApiControl(True)
client.armDisarm(True)
# 起飞到5米高度
client.takeoffAsync().join()
client.moveToZAsync(-5, 1).join()
# 简单矩形飞行路径
client.moveToPositionAsync(10, 0, -5, 1).join()
client.moveToPositionAsync(10, 10, -5, 1).join()
client.moveToPositionAsync(0, 10, -5, 1).join()
client.moveToPositionAsync(0, 0, -5, 1).join()
# 降落并断开连接
client.landAsync().join()
client.armDisarm(False)
client.enableApiControl(False)
要获取高质量的深度图,需要先修改相机设置:
json复制"Cameras": {
"bottom": {
"CaptureSettings": [
{
"ImageType": 3,
"Width": 256,
"Height": 144
}
],
"X": 0, "Y": 0, "Z": 0,
"Pitch": -90, "Roll": 0, "Yaw": 0
}
}
这个Python脚本可以保存三种深度图并生成点云:
python复制import airsim
import numpy as np
import cv2
import os
def depth_to_pointcloud(depth_map, fov=90):
height, width = depth_map.shape
fx = fy = width / (2 * np.tan(fov * np.pi / 360))
cx = width / 2
cy = height / 2
rows, cols = np.indices(depth_map.shape)
z = depth_map.astype(float)
x = (cols - cx) * z / fx
y = (rows - cy) * z / fy
return np.dstack((x, y, z))
client = airsim.MultirotorClient()
responses = client.simGetImages([
airsim.ImageRequest("bottom", airsim.ImageType.DepthPlanner, False, False),
airsim.ImageRequest("bottom", airsim.ImageType.DepthVis, False, False),
airsim.ImageRequest("bottom", airsim.ImageType.DepthPerspective, False, False)
])
for idx, response in enumerate(responses):
img1d = np.frombuffer(response.image_data_uint8, dtype=np.uint8)
img_rgb = img1d.reshape(response.height, response.width, 3)
cv2.imwrite(f"depth_{idx}.png", img_rgb)
if idx == 0: # DepthPlanner
depth_map = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
points = depth_to_pointcloud(depth_map)
with open("pointcloud.asc", "w") as f:
for pt in points.reshape(-1, 3):
if not np.isnan(pt).any():
f.write(f"{pt[0]} {pt[1]} {pt[2]}\n")
要让仿真运行更流畅,可以调整这些UE4控制台命令(在仿真运行时按~键打开控制台):
ini复制r.ScreenPercentage 70
r.VSync 0
r.MotionBlurQuality 0
r.PostProcessAAQuality 2
r.TonemapperQuality 0
r.LightFunctionQuality 0
r.ShadowQuality 1
r.Shadow.MaxResolution 512
对于强化学习应用,建议修改settings.json添加这些配置:
json复制"PhysicsEngineName": "FastPhysics",
"ClockSpeed": 2.0,
"Recording": {
"RecordOnMove": false,
"RecordInterval": 0
}
在项目目录下创建Engine.ini文件(路径:项目名称/Config/WindowsNoEditor/Engine.ini)添加:
ini复制[/Script/Engine.RendererSettings]
r.DefaultFeature.MotionBlur=0
r.DefaultFeature.AmbientOcclusion=0
r.DefaultFeature.AmbientOcclusionStaticFraction=0
r.DefaultFeature.AutoExposure=0