UE4中使⽤飞控⽆⼈机仿真插件——AirSim
UE4中使⽤飞控仿真插件——AirSim
1. 简述
项⽬中使⽤UE4开发⽆⼈机飞控的仿真,所以了解并学习了部分AirSim的机制,微软开源了基于虚幻引擎的⼀款⽤于模拟⽆⼈机飞⾏的⼯具AirSim。通过AirSim平台可以理解⾃动驾驶仪在真实世界中是如何⾏驶的,同时可以在该平台使⽤深度学习技术来理解这些运输⼯具在各种不同的环境下是如何反应的。可以通过飞⾏控制板或模拟器进⾏硬件模拟输⼊,这⾥我使⽤了纯软件仿真。
2.简单配置
根据官⽅说明或⽹络上其他安装步骤,将项⽬clone并编译,将AirSim应⽤到你⾃⼰的UE4⼯程:具体操作如下:
1. 复制Unreal\Plugins⽂件夹从你在上⾯部分做的构建到你的虚幻项⽬的⽂件夹的根
2. 在虚幻项⽬的.uproject⽂件中,向“Modules”对象添加关键的额外依赖项
"AdditionalDependencies": [ "AirSim" ]
心如止水英文
-Plugins部分
"Plugins": [ { "Name": "AirSim", "Enabled": true } ]
添加完后类似这样:
{
"FileVersion":3,
"EngineAssociation":"4.24",
"Category":"Samples",
"Description":"",
"Modules":[
{
"Name":"LandscapeMountains",
"Type":"Runtime",
"LoadingPha":"Default",
"AdditionalDependencies":[
"AirSim"
]
}
],
"TargetPlatforms":[
"MacNoEditor",
"WindowsNoEditor"
],
"Plugins":[
{
"Name":"AirSim",
"Enabled":true
}
]
}
3.修改tting.json⽂件
设置⽂件默认都在C:\Urs\Admin\Documents\AirSim\ttings.json⽂件下,完整的⽤法参考如下:⼀般不需要全部使⽤,需要的再做添加:
{
"SimMode":"",
"ClockType":"",
"ClockSpeed":1,
"LocalHostIp":"127.0.0.1",
"RecordUIVisible":true,
"EngineSound":true,
"PhysicsEngineName":"",
"SpeedUnitFactor":1.0,
"SpeedUnitLabel":"m/s",
"Recording":{
"RecordOnMove":fal,
"RecordInterval":0.05,
"Cameras":[inxs
{"CameraName":"0","ImageType":0,"PixelsAsFloat":fal,"Compress":true} ]
},
"CameraDefaults":{
"CaptureSettings":[
{
"ImageType":0,
"Width":256,
项目负责人 翻译"Height":144,
"FOV_Degrees":90,
"AutoExposureSpeed":100,
"AutoExposureBias":0,
"AutoExposureMaxBrightness":0.64,
"AutoExposureMinBrightness":0.03,
"MotionBlurAmount":0,
"TargetGamma":1.0,
"ProjectionMode":"",
"OrthoWidth":5.12
}
],
"NoiSettings":[
{
"Enabled":fal,
"ImageType":0,
"RandContrib":0.2,
"RandSpeed":100000.0,
"RandSize":500.0,
"RandDensity":2,
"HorzWaveContrib":0.03,
"HorzWaveStrength":0.08,
"HorzWaveVertSize":1.0,
"HorzWaveScreenSize":1.0,
"HorzNoiLinesContrib":1.0,
"HorzNoiLinesDensityY":0.01,
"HorzNoiLinesDensityXY":0.5,
"HorzDistortionContrib":1.0,
"HorzDistortionStrength":0.002
}
],
"Gimbal":{
"Stabilization":0,suffered
"Pitch": NaN,"Roll": NaN,"Yaw": NaN
气缸套}
"X": NaN,"Y": NaN,"Z": NaN,
"Pitch": NaN,"Roll": NaN,"Yaw": NaN
},
"OriginGeopoint":{//初始⽣成点的相对于世界的经纬度
"Latitude":47.641468,
"Longitude":-122.140165,
"Altitude":122
"StartDateTime":"",
"CelestialClockSpeed":1,
"StartDateTimeDst":fal,
"UpdateIntervalSecs":60
},
"SubWindows":[//需要⼏个⼦窗⼝
{"WindowID":0,"CameraName":"0","ImageType":3,"Visible":fal},
{"WindowID":1,"CameraName":"0","ImageType":5,"Visible":fal},
{"WindowID":2,"CameraName":"0","ImageType":0,"Visible":fal}
],
"SegmentationSettings":{love cat
跨境电商培训班
"InitMethod":"",
"MeshNamingMethod":"",
"OverrideExisting":fal
},
"PawnPaths":{
"BareboneCar":{"PawnBP":"Class'/AirSim/VehicleAdv/Vehicle/VehicleAdvPawn.VehicleAdvPawn_C'"}, "DefaultCar":{"PawnBP":"Class'/AirSim/VehicleAdv/SUV/SuvCarPawn.SuvCarPawn_C'"}, "DefaultQuadrotor":{"PawnBP":"Class'/AirSim/Blueprints/BP_FlyingPawn.BP_FlyingPawn_C'"}, "DefaultComputerVision":{"PawnBP":"Class'/AirSim/Blueprints/BP_ComputerVisionPawn.BP_ComputerVisionPawn_C'"} },
"Vehicles":{//这⾥配置多机的状态
"SimpleFlight":{
"VehicleType":"SimpleFlight",
"DefaultVehicleState":"Armed",
托福报名考试
//Armed螺旋桨处于活跃状态,取消为Inactive
"AutoCreate":true,
"PawnPath":"",
"EnableCollisionPassthrogh":fal,
"EnableCollisions":true,
"AllowAPIAlways":true,//允许api控制,重要
mppt"RC":{//远程控制 switch PX4
"RemoteControlID":0,
"AllowAPIWhenDisconnected":fal
},
"Cameras":{
//相同的元素,作为元素内部的相机参考上⾯
},
"X": NaN,"Y": NaN,"Z": NaN,
"Pitch": NaN,"Roll": NaN,"Yaw": NaN
},
"PhysXCar":{
"VehicleType":"PhysXCar",
"DefaultVehicleState":"",
"AutoCreate":true,
"PawnPath":"",
"EnableCollisionPassthrogh":fal,
"EnableCollisions":true,
"RC":{
"RemoteControlID":-1
},
"Cameras":{
"MyCamera1":{
//相同的元素,作为元素内部的相机参考上⾯
},
"MyCamera2":{
//相同的元素,作为元素内部的相机参考上⾯
},
},
"X": NaN,"Y": NaN,"Z": NaN,
"Pitch": NaN,"Roll": NaN,"Yaw": NaN
}
特别地,
"Lidar1":{
"DrawDebugPoints":true//绘制激光雷达
}
4.开发⽰例
仿照ShellDemo,我们来创建⼀个⾃定义的类,通过键盘输⼊来调⽤它:这⾥我需要实现的⽬的是——多机同时飞⾏,按多航路点,阵型等,所以这⾥⽤到了线程来保证每⼀个飞机及独⽴飞⾏没有延迟,这⾥作为⽰例开两个线程。
class MultiCommand :public DroneCommand {
public:
MultiCommand():DroneCommand("Multi","Test Muti fly?")
{
}
bool execute(const DroneCommandParameters& params)
{
DWORD dwThreadId1;//线程ID
DWORD dwThreadId2;
HANDLE hThread1 =CreateThread(NULL,0, TakeProc1,NULL,0,&dwThreadId1);
HANDLE hThread2 =CreateThread(NULL,0, TakeProc2,NULL,0,&dwThreadId2);
float takeoffTimeout =5;
auto position = t-&MultirotorState("Drone1").getPosition();
float z = position.z();// 当前位置 (NED coordinate system).
const float speed =3.0f;
const float size =10.0f;//以3⽶/秒的速度以10⽶阈值飞⾏
const float duration = size / speed;//持续时间
DrivetrainType driveTrain = DrivetrainType::ForwardOnly;
YawMode yaw_mode(true,0);
std::cout <<"moveByVelocityZ("<< speed <<", 0, "<< z <<","<< duration <<")"<< std::endl;
std::this_thread::sleep_for(std::chrono::duration<double>(duration));
std::cout <<"moveByVelocityZ(0, "<< speed <<","<< z <<","<< duration <<")"<< std::endl;
std::this_thread::sleep_for(std::chrono::duration<double>(duration));
std::this_thread::sleep_for(std::chrono::duration<double>(10));
CloHandle(hThread1);
printf("线程1关闭\n");
CloHandle(hThread2);
printf("线程2关闭\n");
printf("FINISH-DONE\r\n");
return fal;
}
爱单词};
DWORD WINAPI TakeProc2(LPVOID lpParam)
{
MultirotorRpcLibClient client;
client.armDisarm(true,"Drone3");
float takeoffTimeout =5;
client.takeoffAsync(takeoffTimeout,"Drone3")->waitOnLastTask();
auto position = MultirotorState("Drone3").getPosition();
float z = position.z();//当前位置(NED coordinate system).
const float speed =3.0f;
const float size =10.0f;
const float duration = size / speed;
DrivetrainType driveTrain = DrivetrainType::ForwardOnly;
YawMode yaw_mode(true,0);
std::cout <<"Drone3("<< speed <<", 0, "<< z <<","<< duration <<")"<< std::endl;
std::cout <<"Drone3(0, "<< speed <<","<< z <<","<< duration <<")"<< std::endl;
std::cout <<"Drone3("<<-speed <<", 0, "<< z <<","<< duration <<")"<< std::endl; veByVelocit
yZAsync(-speed,0, z, duration, driveTrain, yaw_mode,"Drone3"); std::this_thread::sleep_for(std::chrono::duration<double>(duration));
std::cout <<"Drone3(0, "<<-speed <<","<< z <<","<< duration <<")"<< std::endl; veByVelocityZAsync(0,-speed, z, duration, driveTrain, yaw_mode,"Drone3"); std::this_thread::sleep_for(std::chrono::duration<double>(duration));
client.hoverAsync()->waitOnLastTask();
return0;
}
当然,不要忘记更改ttings.json的内容哦,多机的⽰例配置如下: