Main Content

本页采用了机器翻译。点击此处可查看英文原文。

使用 Simulink Test 支持包(针对 ASAM XIL 标准)创建测试

这些示例展示了如何使用 Simulink® Test™ ASAM® XIL 标准支持包开发测试。一个示例创建了一个配置一个测试平台并使用数据采集触发的测试。另一项测试则有两个不同的测试点,各自配置不同的测试平台,但使用相同的共用测试体。

这些示例继承自 sltest.TestCase,因此您可以使用 打开 > 打开基于 MATLAB 的 Simulink 测试( m) 将它们加载到测试管理器中。然后,像在测试管理器中运行其他测试用例一样运行它们。或者,您可以在 MATLAB 命令行运行示例文件,但您不能从命令行将数据推送到测试管理器。

具有数据采集触发功能的 ASAM XIL 测试

此示例展示了使用触发器控制数据采集的示例 MATLAB® 代码文件。ABCCoTestPoint 函数中的配置用于支持数据采集触发的测试平台。

classdef xilexample_trigger < sltest.TestCase

   methods (Test)
      function ABCCoTestPoint(testCase)
         import sltest.xil.framework.*;
            
         frm = Framework;
            
         frm.Configuration.addModelAccessPort(...
            'MAPort1', ...
            'asamxil.v2_1', ...
            'VendorName','ABC Co.', ...
            'ProductName','ABC Test Bench', ...
            'ProductVersion','1.7', ...
            'PortConfigFile',fullfile(pwd,'myConfigureFile.xml'));
            
         frm.Configuration.addTestVariableMapping(...
            'rpm','MAPort1',...
            ['Targets/Controller/Simulation Models/'...
               'Models/simpleXIL/Outports/Out3']);
         frm.Configuration.addTestVariableMapping(...
            'temperature','MAPort1',...
            ['Targets/Controller/Simulation Models/'...
               'Models/simpleXIL/Outports/Out4']);
         frm.Configuration.addTestVariableMapping(...
             'target_rpm','MAPort1',...
             ['Targets/Controller/Simulation Models/'...
                'Models/simpleXIL/Parameters/K']);
         frm.Configuration.addTestVariableMapping(...
             'input1','MAPort1',
             ['Targets/Controller/Simulation Models/'...
                'Models/simpleXIL/Inports/Inport']);

         % Beyond this point the configuration is done and 
         % the test is generic with no test bench specifics
            
         testBody(testCase,frm);
      end
   end

   methods (Access = 'private')
      function testBody(testCase,frm)
         import sltest.xil.framework.*;
                                 
         frm.init;
            
         rpm = frm.createVariable('rpm');
         temperature = frm.createVariable('temperature');
         target_rpm = frm.createVariable('target_rpm');
         input1 = frm.createVariable('input1');
            
         target_rpm.write(50);
            
         % Start acquisition when rpm reaches more than 10 and
         % stop after 15 seconds.
         frm.Acquisition.setupWithVariables([rpm,temperature], ...
            'triggerVariables',rpm, ...
            'startTriggerType','condition', ...
            'startTriggerVal','rpm > 10', ...
            'stopTriggerType','duration', ...
            'stopTriggerVal',15);
         frm.Acquisition.start;
            
         % Set up a stimulation (external input) for the model. 
         % Waveform defined here lasts 5 seconds and LoopCount 
         % of 2 doubles its duration to 10 seconds.
         tseries = timeseries(cos(2*pi*(0:1000)/200)*10,(0:1000)/200);
         frm.Stimulation.setupWithVariablesAndData(...
            {{input1,tseries}},'LoopCount',2);
         frm.Stimulation.start;
            
         frm.start;
         disp(temperature.read);
            
         frm.Acquisition.wait;
         frm.stop;
            
         result = frm.Acquisition.fetch;
         frm.pushDataToSimulinkTestManager(testCase,result);
         testCase.verifySignalsMatch(result,'baseline1.mat');
      end
   end
end

ABCCoTestPoint 函数:

  • 创建一个 Framework 对象

  • 添加模型访问端口

  • 将测试变量名称映射到测试平台变量名称

  • 调用 testBody 函数

testBody 函数:

  • 初始化框架

  • 使用 createVariable 方法实例化变量

  • 通过将值写入 target_rpm 变量来调整参数

  • 设置采集和触发

  • 设置时间序列和刺激

  • 开始仿真并读取和显示变量

  • 等待采集完成并停止仿真

  • 获取结果数据,将其推送至测试管理器,并将其与基线数据进行比较

无需数据采集触发的 ASAM XIL 测试

此示例显示了不使用数据采集触发的示例 MATLAB 代码文件。while 等待 temperature 变量小于或等于 50,然后完成测试。配置功能指定了两个测试平台,Simulink Real-Time™ 和 ABC Co 测试平台。运行测试时使用的测试平台取决于您调用的是 ABCCoTestPoint 还是 SimulinkRealTimeTestPoint 函数。

ABCCoTestPointSimulinkRealTimeTestPoint 函数与 具有数据采集触发功能的 ASAM XIL 测试 中描述的相同,但 SimulinkRealTimeTestPoint 函数设置了三个端口,这是 Simulink Real-Time 所必需的。

classdef xilexample_polling < sltest.TestCase

   methods (Test)

      function ABCCoTestPoint(testCase)
         import sltest.xil.framework.*;
            
         frm = Framework;
            
         % Add the ports
         frm.Configuration.addModelAccessPort(...
            'MAPort1', ...
            'asamxil.v2_1', ...
            'VendorName','ABC Co.', ...
            'ProductName','ABC Test Bench', ...
            'ProductVersion','1.7', ...
            'PortConfigFile',fullfile(pwd,'myConfigureFile.xml'));
            
         % Create the mapping from test variables to 
         % test bench port variables
         frm.Configuration.addTestVariableMapping(...
            'rpm','MAPort1',...
            ['Targets/Controller/Simulation Models/'...
               'Models/simpleXIL/Outports/Out3']);
         frm.Configuration.addTestVariableMapping(...
            'temperature','MAPort1',
            ['Targets/Controller/Simulation Models/'...
               'Models/simpleXIL/Outports/Out4']);
         frm.Configuration.addTestVariableMapping(...
            'target_rpm','MAPort1',...
            ['Targets/Controller/Simulation Models/'...
               'Models/simpleXIL/Parameters/K']);
         frm.Configuration.addTestVariableMapping(...
            'input1','MAPort1',...
            ['Targets/Controller/Simulation Models/'...
               'Models/simpleXIL/Inports/Inport']);

         % Beyond this point the configuration is done and 
         % the test is generic with no test bench specifics
            
         % Call the generic test body
         testBody(testCase, frm);
      end
          
      function SimulinkRealTimeTestPoint(testCase)
         import sltest.xil.framework.*;
            
         frm = Framework;
            
         frm.Configuration.addModelAccessPort(...
            'MAPort', ...
            'asamxil.v2_1', ...
            'VendorName','MathWorks',...
            'ProductName','XIL API',...
            'ProductVersion','1.0',...
            'PortConfigFile',fullfile(pwd,'myConfigureFile.xml'));
         frm.Configuration.addECUCalibrationPort(...
            'ECUCPort', ...
            'asamxil.v2_1', ...
            'VendorName', 'MathWorks',...
            'ProductName', 'XIL API',...
            'ProductVersion', '1.0',...
            'PortConfigFile', fullfile(pwd,'myConfigureFile.xml'),...
            'TargetState', 'started');
         frm.Configuration.addECUMeasurementPort(...
            'ECUMPort', ...
            'asamxil.v2_1', ...
            'VendorName','MathWorks', ...
            'ProductName','XIL API', ...
            'ProductVersion','1.0', ...
            'PortConfigFile',fullfile(pwd,'myConfigureFile.xml'));
          
         frm.Configuration.addTestVariableMapping(...
            'rpm','ECUMPort','simpleXIL/Gain:1','TaskName','SubRate1');
         frm.Configuration.addTestVariableMapping('temperature',...
            'ECUMPort','simpleXIL/Gain2:1','TaskName','SubRate2');
         frm.Configuration.addTestVariableMapping('target_rpm',...
            'ECUCPort','simpleXIL/Gain/Gain');
         frm.Configuration.addTestVariableMapping('input1',...
            'MAPort','simpleXIL/Inport:1');

         % Beyond this point the configuration is done and 
         % the test is generic with no test bench specifics
            
         % Call the generic test body
         testBody(testCase,frm);
      end
   end

   methods (Access = 'private')
      function testBody(testCase, frm)
         import sltest.xil.framework.*;
                                 
         frm.init;
         
         rpm = frm.createVariable('rpm');
         temperature = frm.createVariable('temperature');
         target_rpm = frm.createVariable('target_rpm');
         input1 = frm.createVariable('input1');
          
         target_rpm.write(100);
            
         frm.Acquisition.setupWithVariables([rpm, temperature]);
         frm.Acquisition.start;
            
         % Set up a stimulation (external input) for the model. 
         % Waveform defined here lasts 5 seconds and LoopCount 
         % of 2 doubles its duration to 10 seconds.
         tseries = timeseries(cos(2*pi*(0:1000)/200)*10,(0:1000)/200);
         frm.Stimulation.setupWithVariablesAndData(...
            {{input1,tseries}},'LoopCount',2);
         frm.Stimulation.start;
            
         frm.start;
         while(temperature.read > 50)
            pause(1);
         end

         testCase.verifyTrue(rpm.read >= 100);            

         frm.stop;
         result = frm.Acquisition.fetch;
         frm.pushDataToSimulinkTestManager(testCase,result);
      end
   end
end

testBody 函数:

  • 初始化框架

  • 使用 createVariable 方法实例化变量

  • 通过将值写入 target_rpm 变量来调整参数

  • 设置收购

  • 设置时间序列和刺激

  • 开始刺激

  • 启动仿真并通过轮询等待温度高于 50

  • 验证 rpm 值是否大于或等于 100

  • 停止仿真,获取结果数据并将其推送到测试管理器,

另请参阅

| | | | |

相关主题

外部网站