首页 > 汽车技术 > 正文

MATLAB R2020a+RoadRunner+Unreal Engine4.23 联合仿真详细流程

2020-12-09 22:47:31·  来源:自动驾驶仿真  
 
RoadRuner是MathWorks新收购的自动驾驶场景构建工具,Unreal Engine是商业游戏引擎。RoadRunner创建驾驶场景,导入到Unreal Engine,与Simulink联合仿真。这种方
RoadRuner是MathWorks新收购的自动驾驶场景构建工具,Unreal Engine是商业游戏引擎。RoadRunner创建驾驶场景,导入到Unreal Engine,与Simulink联合仿真。这种方法兼具RoadRunner创建场景之便捷性和Unreal Engine渲染的高保真度。本文将对这个工作流程进行详细介绍。
1.  电脑软硬件要求
软件要求:
Windows® 64-bitplatform
Unreal Editor 4.23
Visual Studio® 2017or higher
Microsoft® DirectX® —— If this software is not already installedon your machine and you try to simulate in the 3D environment, Vehicle DynamicsBlockset prompts you to install it. once you install the software, you mustrestart the simulation.
Automated DrivingToolbox Interface for Unreal Engine 4 Projects,详见3.1
最低硬件要求:
Graphics card (GPU)— Virtual reality-ready GPU with 8 GB of onboard RAM
Processor (CPU) —2.60 GHz
Memory (RAM) — 12 GB
如果要获得流畅的仿真效果,建议配置高性能显卡。以上信息,引自以下链接中的Support Package for Customizing Scenes(无需关注此链接中其他内容)。 https://www.mathworks.com/help/releases/R2020a/vdynblks/ug/support-package-for-customizing-scenes.html
2.  安装基础软件
MATLAB 和 VS 的安装顺序没有特别要求。 VS 需在 Ureal Engine 之前安装,否则有可能 Unreal 的 .uproject 文件没法找到 VS 进行编译。
2.1 安装Visual Studio2017
参照以下链接。
https://docs.unrealengine.com/zh-CN/ProductionPipelines/DevelopmentSetup/VisualStudioSetup/index.html
请确保在工作量(Workloads)下选择“用C++开发游戏(Game developmentwith C++)”。在右侧的摘要(Summary)工具栏中,展开用C++开发游戏(Game development with C++),并勾选可选(Optional)下的虚幻引擎安装程序(Unreal Engine installer)。注意,不要勾选Windows 10 SDK (10.0.17763.0)。



              
另外,如果用户需要使用Simulink Real-Time工具箱,需要同时参照以下链接。
https://ww2.mathworks.cn/matlabcentral/answers/348269-how-do-i-set-up-microsoft-visual-studio-2017-for-slrt
2.2 安装MATLAB R2020a
必备工具箱是Automated Driving Toolbox和Vehicle Dynamics Blockset。并使用mex -setup命令将MATLAB的编译器设置成VS2017。
2.3 安装Unreal Engine4.23
进入 Unreal Engine 官网,点击右上角的下载,注册账号,下载 Epic Games Launcher 并安装。



登陆 Epic Games Launcher ,在“库”界面中选择 Unreal Engine 4.23 并安装。



安装完成后,可在上图界面启动 Unreal Editor 4.23 。
2.4 安装RoadRunner和RoadRunner Asset Library
用正版或者申请试用版,点击以下链接可申请试用版。
https://www.mathworks.com/campaigns/products/trials.html?prodcode=RD
点击以下链接,查看RoadRunner和RoadRunnerAsset Library的下载安装指南。
https://www.mathworks.com/help/roadrunner/ug/install-and-activate-roadrunner.html
我们以Windows平台下的individual license为例,加以说明。在MathWorks官网个人账户的my account中,查看License。



点击上图中RoadRunner的license,在以下界面Download Products。



点击Manage Users→Add User。



Add User中设置邮箱和姓名等信息。接着Intall and Activate→Activate。



在下图界面中填写即将安装RoadRunner的电脑的一些信息。



Host ID——MAC地址或C盘序列号
如使用C盘序列号,在Windows command prompt中执行vol c:获得信息。



Computer Login Name
在Windows command prompt中执行set username获得信息。
Activation Label——自行定义
提交上述信息后,网站会生成一个包含上述信息的RoadRunner license文件,将它下载到电脑中。最后安装之前下载的程序,当提示输入license文件时,选择上一步下载的license文件。此时,已完成安装和激活工作。
3.  安装插件
注意:完成上述步骤后,再装插件。
3.1 安装MATLAB的Unreal支持包和Unreal的MathWorks插件
参见:
https://www.mathworks.com/help/releases/R2020a/driving/ug/install-and-configure-support-package-for-customizing-scenes.html
打开MATLAB的Add On/附加功能界面。



在界面中搜索 unreal 。



点开上图红框链接(Automated Driving ToolboxInterface for Unreal Engine 4 Projects),选择安装。



安装完成后,需要在MATLAB中执行以下代码。
supportPackageFolder= fullfile( ...
   matlabshared.supportpkg.getSupportPackageRoot, ...
    "toolbox","shared","sim3dprojects","driving");
localFolder = "C:\Local";
projectFolderName= "AutoVrtlEnv";
projectSupportPackageFolder= fullfile(supportPackageFolder,projectFolderName);
projectLocalFolder= fullfile(localFolder,projectFolderName);
if~exist(projectLocalFolder,"dir")
   copyfile(projectSupportPackageFolder,projectLocalFolder);
end
ueInstallFolder= "C:\Program Files\Epic Games\UE_4.23";
supportPackageFolder= fullfile( ...
   matlabshared.supportpkg.getSupportPackageRoot, ...
    "toolbox","shared","sim3dprojects","driving");
mwPluginName = "MathWorksSimulation.uplugin";
mwPluginFolder =fullfile(supportPackageFolder,"PluginResources","UE423");
uePluginFolder =fullfile(ueInstallFolder,"Engine","Plugins");
uePluginDestination= fullfile(uePluginFolder,"Marketplace","MathWorks");
cd(uePluginFolder)
foundPlugins =dir("**/" + mwPluginName);
if~isempty(foundPlugins)
   numPlugins = size(foundPlugins,1);
   msg2 = cell(1,numPlugins);
   pluginCell = struct2cell(foundPlugins);
   msg1 = "Plugin(s)already exist here:" + newline + newline;
    for n = 1:numPlugins
       msg2{n} = "   " + pluginCell{2,n}+ newline;
    end
   msg3 = newline + "Please removeplugin folder(s) and try again.";
   msg = msg1 + msg2 + msg3;
   warning(msg);
else
   copyfile(mwPluginFolder,uePluginDestination);
   disp("Successfullycopied MathWorksSimulation plugin to UE4 engine plugins!")
end
以上代码引自以下链接的Set Up Scene Customization UsingSupport Package。
https://www.mathworks.com/help/releases/R2020a/driving/ug/install-and-configure-support-package-for-customizing-scenes.html
值得注意的是:
localFolder ="C:\Local",该路径可自行设定,上述代码会把支持包自带的Unreal工程拷贝到该文件夹。
ueInstFolder ="C:\Program Files\Epic Games\UE_4.23",此处是指明Unreal Engine 4.23的安装路径。如果您的安装路径不是C:\ProgramFiles\Epic Games\UE_4.23,需要调整成实际路径。
执行结束后,会有两个现象:
①   localFolder中出现支持包自带的Unreal工程。



②   Unreal Editor中出现MathWorks插件。在Unreal Editor中点击“编辑”→“插件”可进行查看。



3.2 新建Unreal工程,安装Unreal的RoadRunner插件
引自:
https://www.mathworks.com/help/roadrunner/ug/Exporting-to-Unreal.html
打开Unreal,设置项目文件夹和项目名称后,创建一个项目,如下图。



创建项目后,项目文件夹如下图。注意,路径中不要出现空格。



在该路径下新建一个Plugins文件夹。



接着,点击以下链接,下载Unreal的RoadRunner插件。
https://www.mathworks.com/help/roadrunner/ug/Downloading-Plugins.html
将插件文件夹中的RoadRunnerimporter和RoadRunnerMaterials文件夹(如下图)都复制到上图的Plugins文件夹中。每个unreal工程都得拷贝一次,否则roadrunner导出的信息没法加载到Unreal。



然后,右键刚才新建的unreal 工程demo1.uproject,点击Generate Visual Studio project files。之后,双击打开demo1.uproject,在弹出的窗口中选择“是”。(引自https://www.mathworks.com/help/roadrunner/ug/Exporting-to-Unreal.html)



在弹出Unreal Editor中,可以在“编辑”→“插件”中查看到Unreal已经加载了RoadRunner的插件。



4.  RoadRunner的场景导出到Unreal
参见:
https://www.mathworks.com/help/roadrunner/ug/Exporting-to-Unreal.html
我们以RoadRunner自带的FourWaySignal场景为例,将它导出到Unreal。
4.1 将RoadRunner场景导出成文件
打开RoadRunner,新建场景/New Scene。在RoadRunner界面中,“File”→“Open Scene”选择打开自带的场景文件FourWaySignal.rrscene(如下图)。



选择“File”→“Export”→“Unreal (.fbx + .xml)”。



设定导出的目标文件夹和文件名,如下图。



导出完成后,目标文件夹中就包含了导出的文件。



4.2 将导出文件加载到Unreal
打开Unreal Editor,在内容浏览器中点击“导入”。



选择4.1中Exports文件夹中的fbx文件。



接着,导入的设置。设置界面如下图。



具体如何设置,参考以下链接。导入完成后,注意保存,保存时设置MAP名称。
https://www.mathworks.com/help/roadrunner/ug/Exporting-to-Unreal.html
5.  MATLAB与UnrealEditor联合仿真联合仿真
Unreal的场景能以两种形式与MATLAB联合仿真:
①   Unreal Editor直接运行场景,联合仿真时需要打开Unreal Editor
②   Unreal场景导出成可执行文件(exe),联合仿真时无需打开Unreal Editor
此节介绍第一种方式。
5.1 从MATLAB打开Unreal Editor
引自:
https://www.mathworks.com/help/releases/R2020a/driving/ug/customize-scenes-using-simulink-and-unreal-editor.html
如果直接在Unreal Editor中打开项目文件,则MATLAB和Simulink不会与Unreal Editor建立连接,因此也将无法联合仿真。需要从MATLAB打开Unreal Editor。
双击Simulink模型的Simulation 3D SceneConfiguration模块,将Scene source参数设置为Unreal Editor。在Project参数中,浏览到包含场景的项目文件。



单击“Open Unreal Editor”,Unreal Editor将打开并从您的项目中加载场景。 第一次从 MATLAB 打开 Unreal Editor 时,可能会要求您重新编译 UE4Editor DLL 文件,点击 “Yes” 以重新编译。
当 UnrealEditor 打开时,您可以忽略有关名称“ _BuiltData ”加载失败的文件的所有警告消息。
如果您收到“the lighting needs to be rebuilt”的警告,请从Unreal Editor窗口上方的工具栏中,选择“Build”→“Build Lighting Only”。首次打开场景或向场景中添加新元素时,Unreal Editor会发出此警告。
另外,如果是自定义的场景,单击“Open Unreal Editor”后,在出现的Unreal Editor中需要重设蓝图父项,具体操作如下。
If this is your first time opening a customproject in Unreal Editor 4.23, you may need to associate, or reparent, thisproject with the Sim3dLevelscriptActor level blueprint used in Vehicle DynamicsBlockset. The level blueprint controls how objects interact once placed withinin the Unreal environment. If you do not reparent the project to this levelblueprint, the simulation returns an error.
Toreparent the level blueprint:
In theUnreal Editor toolbar above the editor window, select Blueprints > OpenLevel Blueprint.
In theLevel Blueprint window, select File > Reparent Blueprint.
Click theSim3dLevelscriptActor blueprint and close the Level Blueprint window.
If you do not see this blueprint, make surethat you have the MathWorksSimulation plugin installed and enabled.
Navigate back to the editor window. In thetoolbar above this window, select Settings > Plugins.
In the Plugins window, verify that theMathWorks Interface plugin appears. This plugin refers to theMathWorksSimulation.uplugin file that you copied into your local Unreal Editorinstallation from the Vehicle Dynamics Blockset Support Package for UnrealEngine 4 Projects.
If you editor installation includes theplugin, then when you open a project in the editor for the first time, you areprompted to enable this plugin. If you do not see the MathWorks Interfaceplugin in this window, repeat the steps under and reopen the editor fromMATLAB.
Select the Enabled check box.
Close the editor, reopen it from MATLAB, andrepeat the steps to reparent the blueprint.
实际操作步骤如下:



如果没找到名为Sim3dLevelscriptActor的蓝图,那要先确认Unreal是否启用了MathWorks插件。查看插件的方式,3.1有说明,正常的状态如下图。



5.2 联合仿真
在UnrealEditor中可以通过数字键切换视角,如下图。



对于自定义的场景,如果也要实现这个功能,需要把DefaultInput.ini从如下路径
C:\ProgramData\MATLAB\SupportPackages\\toolbox\shared\sim3dprojects\driving\AutoVrtlEnv\Config
拷贝到下面的路径
分享到:
 
反对 0 举报 0 收藏 0 评论 0
沪ICP备11026620号