Create Tests Using the Simulink Test Support Package for ASAM XIL Standard
Note
The examples require that you Install and Set Up the Simulink Test Support Package for ASAM XIL Standard.
These examples show how you can develop tests using the Simulink® Test™ Support Package for ASAM® XIL Standard. One example creates a test that configures one test bench and uses data acquisition triggering. The other test has two different test points, each of which configures a different test bench, but uses the same shared test body.
The examples inherit from sltest.TestCase
, so you can load them into the Test Manager using Open > Open MATLAB-Based Simulink Test (.m). Then, run them like you run other test cases in the Test Manager.
Alternatively, you can run the example files at the MATLAB command line, but you cannot
push data to the Test Manager from the command line.
ASAM XIL Test With Data Acquisition Triggering
This example shows a sample MATLAB® code file that uses a trigger to control data acquisition. The
configuration in the ABCCoTestPoint
function is for a test bench
that supports data acquisition triggering.
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
The ABCCoTestPoint
function:
Creates a
Framework
objectAdds a model access port
Maps the test variable names to test bench variable names
Calls the
testBody
function
The testBody
function:
Initializes the framework
Instantiates the variables using the
createVariable
methodTunes a parameter by writing a value to the
target_rpm
variableSets up the acquisition and triggering
Sets up a time series and stimulation
Starts the simulation and reads and displays a variable
Waits for acquisition to complete and stops the simulation
Fetches the result data, pushes it to the Test Manager, and compares it to baseline data
ASAM XIL Test Without Data Acquisition Triggering
This example shows a sample MATLAB code file that does not use data acquisition triggering. A
while
waits for the temperature
variable
to be less than or equal to 50, and then finishes the test. The configuration
functions specify two test benches, Simulink
Real-Time™ and ABC Co Test Bench. The test bench that is used when you run the
test depends on whether you call the ABCCoTestPoint
or the
SimulinkRealTimeTestPoint
function.
The ABCCoTestPoint
and
SimulinkRealTimeTestPoint
functions are the same as described
in ASAM XIL Test With Data Acquisition Triggering, except the
SimulinkRealTimeTestPoint
function sets up three ports, which
is required by 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
The testBody
function:
Initializes the framework
Instantiates the variables using the
createVariable
methodTunes a parameter by writing a value to the
target_rpm
variableSets up the acquisition
Sets up a time series and stimulation
Starts the stimulation
Starts the simulation and waits, using polling, for the temperature to be greater than 50
Verifies that the rpm value is greater than or equal to 100
Stops the simulation, fetches the result data, and pushes it to the Test Manager,
See Also
sltest.xil.framework.Framework
| sltest.xil.framework.FrameworkConfiguration
| sltest.xil.framework.Acquisition
| sltest.xil.framework.Stimulation
| sltest.xil.framework.TestVariable
| sltest.TestCase
Related Topics
- Install and Set Up the Simulink Test Support Package for ASAM XIL Standard
- Real-Time Testing with the Simulink Test Support Package for ASAM XIL Standard