为自定义目标配置处理器在环 (PIL) 仿真
使用目标连接 API 创建一个目标连接配置。使用目标连接配置,您可以在自定义嵌入式硬件上运行处理器在环 (PIL) 仿真。
您将:
调整编译过程以支持 PIL 仿真。
配置用于在目标硬件上下载并执行 PIL 可执行文件的工具。
配置主机和目标之间的通信信道,以支持目标硬件上的 PIL 仿真。
从配置为进行软件在环 (SIL) 仿真的模型开始。此示例将引导您完成创建目标连接配置的过程,以便您以 PIL 模式对模型进行仿真。该示例完全在您的开发计算机上运行。您可以使用相同的步骤为您自己的嵌入式目标硬件创建连接配置。
另请参阅用 SIL 和 PIL 仿真测试生成的代码和为处理器在环 (PIL) 仿真创建目标通信信道。
准备工作
稍后在此示例中,您要将文件夹添加到搜索路径。创建文件夹路径。
sl_customization_path = pwd;
如果此文件夹已在搜索路径上,请将其删除。
if contains(path,sl_customization_path) rmpath(sl_customization_path) end
重置自定义。
sl_refresh_customizations
使用 SIL 仿真测试生成的代码
为针对 SIL 配置的模型进行仿真。通过将 SIL 仿真行为与普通仿真行为进行比较,验证为开发计算机编译的生成代码。
确保示例模型是新打开的。
close_system('SILModelBlock',0); close_system('SILCounter',0) open_system('SILModelBlock')
CounterA Model 模块中显示有文本 (SIL),表示其引用模型是针对 SIL 仿真配置的。
运行系统的仿真。
set_param('SILModelBlock','StopTime','10'); sim('SILModelBlock');
### Searching for referenced models in model 'SILModelBlock'. ### Found 0 model references to update. ### Starting build procedure for: SILCounter ### Successful completion of build procedure for: SILCounter Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================================ SILCounter Code generated and compiled. Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.803s ### Preparing to start SIL simulation ... Building with 'gcc'. MEX completed successfully. ### Updating code generation report with SIL files ... ### Starting SIL simulation for component: SILCounter ### Application stopped ### Stopping SIL simulation for component: SILCounter
目标连接配置
从针对 PIL 的目标连接配置入手。
制作一份目标连接配置类的本地副本。
src_dir = ... fullfile(matlabroot,'toolbox','coder','simulinkcoder','+coder','+mypil'); if exist(fullfile('.','+mypil'),'dir') rmdir('+mypil','s') end mkdir +mypil copyfile(fullfile(src_dir,'Launcher.m'), '+mypil'); copyfile(fullfile(src_dir,'TargetApplicationFramework.m'), '+mypil'); copyfile(fullfile(src_dir,'ConnectivityConfig.m'), '+mypil');
将复制的文件设置为可写。
fileattrib(fullfile('+mypil', '*'),'+w');
更新命名空间以反映文件的新位置。
coder.mypil.Utils.UpdateClassName(... './+mypil/ConnectivityConfig.m',... 'coder.mypil',... 'mypil');
验证现在您的当前文件夹中有文件夹 +mypil,其中包含文件 Launcher.m、TargetApplicationFramework.m 和 ConnectivityConfig.m。
dir './+mypil'
. .. ConnectivityConfig.m Launcher.m TargetApplicationFramework.m
查看代码以启动 PIL 可执行文件
使用 mypil.Launcher 类对用来启动 PIL 可执行文件的工具进行配置。在编辑器中打开此类。
edit(which('mypil.Launcher'))
查看此文件的内容。setArgString 方法为可执行文件提供额外的命令行参数。这些参数可以包括 TCP/IP 端口号。对于嵌入式处理器实现,您可以选择对这些设置进行硬编码。
配置总体目标连接配置
查看 mypil.ConnectivityConfig 类。
edit(which('mypil.ConnectivityConfig'))
查看此文件的内容。您应该能够从中找出以下内容:
rtw.connectivity.RtIOStreamHostCommunicator 实例的创建,用于配置 TCP/IP 通信信道的主机端。
对 Launcher 的 setArgString 方法的调用,用于配置 TCP/IP 通信信道的目标端。
对 setTimer 的调用,用于配置计时器以测量执行时间。
要定义您自己的目标特定计时器以进行执行时间探查,您必须使用代码替换库来指定函数 code_profile_read_timer 的替换项。使用命令行 API 或 crtool 用户界面。
查看目标端通信驱动程序
查看文件 rtiostream_tcpip.c。
rtiostreamtcpip_dir=fullfile(matlabroot,'toolbox','coder','rtiostream','src',... 'rtiostreamtcpip'); edit(fullfile(rtiostreamtcpip_dir,'rtiostream_tcpip.c'))
向下滚动到此文件的末尾。查看以确认此文件包含函数 rtIOStreamOpen、rtIOStreamSend 和 rtIOStreamRecv 的 TCP/IP 实现。这些函数是目标硬件与开发计算机通信所必需的。对于特定于您的目标硬件和通信信道的上述每个函数,您都必须提供一个实现。
将目标端通信驱动程序添加到连接配置
使用 mypil.TargetApplicationFramework 类对要包含在编译版本中的其他文件进行配置。在编辑器中打开此类。
edit(which('mypil.TargetApplicationFramework'))
使用 sl_customization 注册目标连接配置
要使用新目标连接配置,您必须提供 sl_customization 文件。sl_customization 文件用来注册您的新目标连接配置并指定使用它所需的条件。此文件中指定的条件可以包括系统目标文件的名称和硬件实现设置。
您可以查看 sl_customization 文件。对于此示例,您不必对该文件进行更改。
edit(fullfile(sl_customization_path,'sl_customization.m'))
将 sl_customization 文件夹添加到搜索路径并刷新自定义。
addpath(sl_customization_path); sl_refresh_customizations;
使用 PIL 仿真测试生成的代码
运行 PIL 仿真。
close_system('SILModelBlock',0) open_system('SILModelBlock') set_param('SILModelBlock/CounterA','SimulationMode','processor-in-the-loop (pil)'); set_param('SILModelBlock','StopTime','10'); sim('SILModelBlock');
### Searching for referenced models in model 'SILModelBlock'. ### Found 0 model references to update. ### Starting build procedure for: SILCounter ### Generated code for 'SILCounter' is up to date because no structural, parameter or code replacement library changes were found. ### Successful completion of build procedure for: SILCounter Build Summary Top model targets built: Model Action Rebuild Reason ===================================================================== SILCounter Code compiled. Compilation artifacts were out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 2.3594s ### Connectivity configuration for component "SILCounter": My PIL Example ### EXECUTING METHOD SETARGSTRING SETARGSTRING called from line 65 of ConnectivityConfig.m ### Preparing to start PIL simulation ... Building with 'gcc'. MEX completed successfully. ### Updating code generation report with PIL files ... ### Starting application: 'SILCounter_ert_rtw/pil/SILCounter' Starting PIL simulation Started new process, pid = 22347 Stopping PIL simulation Terminated process, pid = 22347
查看前面的消息。确认仿真正常运行而没有错误。您现在已实现了一个 PIL 的目标连接配置。您可以使用相同的 API 来为您自己的嵌入式处理器、下载工具和通信信道组合实现连接配置。
清理
删除此示例的搜索路径。
rmpath(sl_customization_path)
重置自定义。
sl_refresh_customizations
关闭模型。
close_system('SILModelBlock',0) close_system('SILCounter',0)