Main Content

Create Tests Using Simulink Model Simulation as a Test Bench

This example shows how to develop tests that use Simulink model simulation as a test bench with the Simulink® Test™ Support Package for ASAM® XIL Standard. The example configures a Simulink model that simulates in normal mode as a test bench and creates a test that simulates the model, logs data, and then compares the logged data to the expected data to confirm that the results are within acceptable limits.

To run this example, you must install the Simulink Test Support Package for ASAM XIL Standard. For more information, see Install and Set Up the Simulink Test Support Package for ASAM XIL Standard. Then, you can load the file into the Test Manager using Open > Open MATLAB-Based Simulink Test (.m) and run the test. Alternatively, you can run the example files at the MATLAB command line, but you cannot push data to the Test Manager.

The ASAMXILSimulinkSimExampleTest.m file defines the SimulinkModelTestPoint function, which configures the model ASAMXILSimulinkSim as the test bench. The function creates a Framework object and then generates a model access port configuration file using sltest.xil.testbench.createMAPortConfigFile. Then, the function adds a model access port that specifies the generated configuration file and maps the test variables to test bench variables. For more information on how to configure test benches and map variables, see sltest.xil.framework.FrameworkConfiguration.

The testbody function initializes the framework and creates instances of the mapped variables, sets up the test to log the Sine, Cosine, and Result variables, enables logging, and simulates the model. Then, the function stops data acquisition, compares the simulation results to the expected results, and pushes the test case and results to the Test Manager. For more information signal data logging, see sltest.xil.framework.Acquisition.

classdef ASAMXILSimulinkSimExampleTest < sltest.TestCase

    methods (Test)

        function SimulinkModelTestPoint(testCase)
            import sltest.xil.framework.*;

            portConfigFileName = fullfile(pwd, ...
                'ASAMXILSimulinkSimExampleTestConfiguration.xml');
            sltest.xil.testbench.createMAPortConfigFile(portConfigFileName, ...
                "ASAMXILSimulinkSim")

            frm = Framework;
            
            frm.Configuration.addModelAccessPort(...
            'aPort', ...
            'asamxil.v2_1', ...
            'VendorName','MathWorks',...
            'ProductName','Simulation Server XIL API',...
            'ProductVersion','1.0.0.0',...
            'PortConfigFile',portConfigFileName);
              
            frm.Configuration.addTestVariableMapping('Sine', ...
                'aPort','ASAMXILSimulinkSim/Sine wave:1');
            frm.Configuration.addTestVariableMapping('Cosine',['' ...
                'aPort'],'ASAMXILSimulinkSim/Cosine wave:1');
            frm.Configuration.addTestVariableMapping('Result', ...
                'aPort','ASAMXILSimulinkSim/Product:1');
            frm.Configuration.addTestVariableMapping('SineFreq', ...
                'aPort','SineFrequency');
            frm.Configuration.addTestVariableMapping('CosineFreq', ...
                'aPort','CosineFrequency');
            
            
            % 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)
                                     
            frm.init;
             
            Sine = frm.createVariable('Sine');
            Cosine = frm.createVariable('Cosine');
            Result = frm.createVariable('Result');
            SineFreq = frm.createVariable('SineFreq');
            CosineFreq = frm.createVariable('CosineFreq');
              
            sineFrequency = sqrt(8);
            cosineFrequency = sqrt(6);
            SineFreq.write(sineFrequency);
            CosineFreq.write(cosineFrequency);
             
            frm.Acquisition.setupWithVariables([Sine,Cosine,Result]);
            frm.Acquisition.start;
         
            frm.start;
            frm.waitForSimulationComplete;
            frm.stop;

            fetchedResult = frm.Acquisition.fetch;

            vals = fetchedResult.find('Result').Values;
            time = vals.Time;
            data = vals.Data;
            expectedData = sin(time*sineFrequency).*cos(time*cosineFrequency);

            import matlab.unittest.constraints.IsEqualTo
            import matlab.unittest.constraints.AbsoluteTolerance
            testCase.verifyThat(data,IsEqualTo(expectedData, ...
                'Within', AbsoluteTolerance(1.0e-12)))

            frm.pushDataToSimulinkTestManager(testCase,fetchedResult);
        end
    end
end

See Also

| | | | | |

Related Topics

External Websites