Matlab代码生成之SIL/PIL测试

一、SIL与PIL测试流程

1.SIL(Software in the Loop),使用SIL可以在PC端进行测试生成的代码。   PIL(Processor in the Loop),使用PIL可以在实际目标硬件或指令集模拟器   上运行源代码进行测试。

  测试标准:将PIL(SIL)结果与正常模型测试结果进行对比,可以得到生代码的执行效果。

2.需要配置与PC端的直接连接参数

二、Top Model的SIL/PIL仿真

特点:

1.可以直接使用独立的代码接口进行测试
2.可以配置模型从Workspace中加载测试向量和激励输入
3.便捷地在Top model、Normal、SIL、PIL之间进行切换

官方链接:官方链接1

在matlab command window中输入以下命令:

model='rtwdemo_sil_topmodel';
close_system(model,0)    //关闭不保存
open_system(model)		//打开model

之后在Simulink中打开如下模型:

在这里插入图片描述

在matlab command window中输入以下命令:

set_param(gcs, 'RecordCoverage','off');                            /*关闭模型覆盖率记录*/
coverageSettings = get_param(model, 'CodeCoverageSettings');      
coverageSettings.CoverageTool='None';							   /*关闭代码覆盖率记录*/
set_param(model, 'CodeCoverageSettings',coverageSettings);
set_param(model, 'CodeExecutionProfiling','off');				  /*关闭执行时间分析*/
[ticks_to_count, reset, counter_mode, count_enable] = ...
    rtwdemo_sil_topmodel_data(T);                                   /*设置输入参数*/
    
set_param(model, 'LoadExternalInput','on');
set_param(model, 'ExternalInput','ticks_to_count, reset, counter_mode, count_enable');
set_param(model, 'SignalLogging', 'on');
set_param(model, 'SignalLoggingName', 'logsOut');				   /*设置相关参数*/

set_param(model,'SimulationMode','normal')
[~, ~, yout_normal] = sim(model,10);                               /*运行normal 模型(结果存储在yout_normal)*/

set_param(model,'SimulationMode','Software-in-the-Loop (SIL)') 
[~, ~, yout_sil] = sim(model,10);                                  /*运行SIL模型(结果存储在yout_sil)*/


fig1 = figure;
subplot(3,1,1), plot(yout_normal), title('Counter Output for Normal Simulation')
subplot(3,1,2), plot(yout_sil), title('Counter Output for SIL Simulation')
subplot(3,1,3), plot(yout_normal-yout_sil), ...    
    title('Difference Between Normal and SIL');                       /*绘制图像,观看两者差异*/

模型中有关的设置:

在这里插入图片描述

  整个模型的执行就是,从外部输入控制参量然后进行执行,然后对比SIL与normal模型下的运行结果进行分析。

三、基于模型的SIL(PIL)测试

model='rtwdemo_sil_modelblock';
open_system(model);                    /*打开模型*/

coverageSettings = get_param(model, 'CodeCoverageSettings');
coverageSettings.CoverageTool='None';
set_param(model, 'CodeCoverageSettings',coverageSettings);
open_system('rtwdemo_sil_modelblock')
set_param('rtwdemo_sil_modelblock', 'CodeExecutionProfiling','off');
open_system('rtwdemo_sil_counter')
set_param('rtwdemo_sil_counter', 'CodeExecutionProfiling','off');
currentFolder=pwd;
save_system('rtwdemo_sil_counter', fullfile(currentFolder,'rtwdemo_sil_counter.slx')) 
/*关闭代码覆盖率、执行时间分析*/

set_param([model '/CounterA'], 'CodeInterface', 'Top model');     /*设置模型的运行方式*/
out = sim(model,20);                /*开始运行*/

在这里插入图片描述

yout = find(out,'logsOut');
yout_sil = yout.get('counterA').Values.Data;
yout_normal = yout.get('counterB').Values.Data;
fig1 = figure;
subplot(3,1,1), plot(yout_normal), title('Counter Output for Normal Simulation')
subplot(3,1,2), ...
    plot(yout_sil), title('Counter Output for Model Block SIL (Top-Model) Simulation')
subplot(3,1,3), plot(yout_normal-yout_sil), ...
    title('Difference Between Normal and SIL');                   /*绘制图表进行分析*/

  这种模式下是normal和SIL(PIL)各自封装成一个测试模块同时执行(如图中的CounterA与CounterB),对比执行后的结果。

四、基于SIL(PIL)控制模块测试

model='rtwdemo_sil_block';
close_system(model,0)
open_system(model)                  /*打开模型*/

out = sim(model,10);
yout_normal = find(out,'yout');
clear out                          /*启动模型*/  这里是normal模型的仿真

set_param(model,'CreateSILPILBlock','SIL');
close_system('untitled',0);
rtwbuild([model '/Controller'])

controllerBlock = [model '/Controller'];
blockPosition = get_param(controllerBlock,'Position');
delete_block(controllerBlock);
add_block('untitled/Controller',[controllerBlock '(SIL)'],...
          'Position', blockPosition);
close_system('untitled',0);
clear controllerBlock blockPosition

之后出现一个新的model构建框:

在这里插入图片描述

controllerBlock = [model '/Controller'];
blockPosition = get_param(controllerBlock,'Position');
delete_block(controllerBlock);                                          //删除原来模型中的模块

add_block('untitled/Controller',[controllerBlock '(SIL)'],...           //将生成的Model替代原来模型中的模块
          'Position', blockPosition);
close_system('untitled',0);
clear controllerBlock blockPosition

out = sim(model,10);                                                    //启动仿真

machine_precision = eps(single(yout_normal));
tolerance = 4 * machine_precision;                                      //设置仿真容许误差

yout_sil = find(out,'yout');
tout = find(out,'tout');
fig1 = figure;
subplot(3,1,1), plot(yout_normal), title('Controller Output for Normal Simulation')
subplot(3,1,2), plot(yout_sil), title('Controller Output for SIL Simulation')
subplot(3,1,3), plot(tout,abs(yout_normal-yout_sil),'g-', tout,tolerance,'r-'), ...
    title('Normal and SIL Difference and Error Tolerance');                //绘制图像
    
    
close_system(model,0);
if ishandle(fig1), close(fig1), end
clear fig1
simResults={'out','yout_sil','yout_normal','tout','machine_precision'};
save([model '_results'],simResults{:});
clear(simResults{:},'simResults')                                          //关闭图像

五、总结

PIL测试方面,可以验证测试生成代码。C2000的测试与以上相仿,具体说明链接如下:官方链接

  • 4
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quikk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值