Main Content

Test Real-Time Application in Simulink Test

This example shows how to perform a frequency-response test of the model slrt_ex_osc_sltest.

Using this information, in the design phase, you can modify the internal parameters of the model to meet your frequency requirements. In the production phase, you can bin manufactured parts based on frequency response.

Open the Model

To open the model, in the MATLAB® Command Window, type:

open_system('slrt_ex_osc_sltest');

The figure shows representative output from a real-time application running on a target computer. At low frequencies, the output of the Integrator1 block settles to the same value as the output of the Signal Generator block. At high frequencies, the output of the Integrator1 block is still ringing at the end of each pulse.

The test determines the highest frequency at which the output values of the Integrator and Signal Generator blocks are within a specified criterion of each other. The test uses the model itself as a signal source and uses a Test Harness to compare the outputs of the Integrator and Signal Generator blocks.

Step 1. Create Frequency Parameter

Create the parameter Frequency that is tuned at the end of this example.

  1. Open model explorer.

  2. Create a Simulink® parameter Frequency in model workspace for model slrt_ex_osc_sltest.

  3. Mark the parameter as a model argument.

  4. Enter a default Value of 1. Change the Storage Class to be Model Default.

  5. Open the Signal Generator block parameters and change the Frequency parameter to be the new Simulink parameter Frequency.

Step 2. Create Test Harness

  1. On the Simulink Apps tab, click Simulink Test.

  2. On the Test tab, click Add Test Harness. The software creates a Test Harness with the default name slrt_ex_osc_sltest_Harness1.

  3. In the Basic Properties tab, for the Input to Component under Test, select None.

  4. For the Output from Component under Test, select Outport.

  5. Select the Add separate assessment block check box.

  6. Select the Open harness after creation check box.

  7. Take the defaults in the remaining tabs.

8. Click OK.

The example model slrt_ex_osc_sltest stores the Test Harness within the model. To access the Test Harness from the example model:

  1. In Simulink Editor, on the Test tab, click Manage Test Harnesses.

  2. Click slrt_ex_osc_sltest_Harness1.

Step 3. Configure Test Harness

  1. Open the Test Assessment block.

  2. To simplify the Test Assessment configuration, in the Input symbol list, delete the existing input Outport and add inputs Int1 and SigGen.

  3. Open the Block Parameters for the Test Assessment block and change the Show port labels option to FromPortIcon. This will show the input symbol names next to the port icon of the block.

  4. In slrt_ex_osc_sltest_Harness1, connect a Demux block to the signal named Out.

  5. In the Demux block dialog box, set Number of outputs to 2.

  6. Connect the top Demux block output to Test Assessment/Int1 and the bottom output to Test Assessment/SigGen.

Step 4. Configure Simulink Parameters

  1. Open the Model Explorer. On the Modeling tab, pull down the Design section and click Model Explorer.

  2. Click node slrt_ex_osc_sltest_Harness1 > Model Workspace.

  3. In the toolbar, click the Add Simulink Parameter button.

  4. Add the following Simulink.Parameter object:

  • Name — Criterion

  • Value — 0

  • DataType — double

  • Storage Class — ExportedGlobal

5. In a similar manner, add Simulink parameters w_open and w_close. Because these parameters are in the slrt_ex_osc_sltest_Harness1 model workspace as model parameters, you access them by name directly, without model hierarchy.

6. Save the model.

Step 5. Setup Frequency Argument

  1. Right click on slrt_ex_osc_sltest_Harness1/slrt_ex_osc_sltest.

  2. Select Block Parameter(Model reference).

  3. Select Instance parameters on pop out window.

  4. Mark Frequency as an argument.

Step 6. Prepare Test Assessment Steps

1. Open the Test Assessment block

2. Add these parameters to the Parameter symbol list:

  • Criterion

  • w_open

  • w_close

3. To add a step, in the Step column, move the cursor to the top row, click Add step after, and type:

CheckSetting

4. Right-click step CheckSetting and set the When decomposition check box.

5. To add a substep to CheckSetting, click Add sub-step, and type:

   Hi when (SigGen > 0)

The when expression selects one half of the waveform.

6. Right-click substep Hi when and set the When decomposition check box.

7. To substep Hi when, add substep:

        HiCheck when ((et >= w_open) && (et <= w_close))
        verify((abs(Int1) >= abs(SigGen) * (1.0 - Criterion)) && ...
           (abs(Int1) <= abs(SigGen) * (1.0 + Criterion)));

The when expression selects the time window for testing the acceptance criterion. The verify command tests the acceptance criterion.

8. In a similar manner, to step CheckSetting, add substep:

   Lo when (SigGen < 0)

9. To substep Lo when, add substep:

        LoCheck when ((et >= w_open) && (et <= w_close))
        verify((abs(Int1) >= abs(SigGen) * (1.0 - Criterion)) && ...
           (abs(Int1) <= abs(SigGen) * (1.0 + Criterion)));

10. Right-click substep Lo when and set the When decomposition check box.

11. To satisfy the requirements of When decomposition, remove the default Run step and insert DefaultStep substeps after steps CheckSetting, Hi when, and Lo when. When decomposition requires at least two steps at each level of nesting, and one nondecomposed step at the end of each list of steps.

11. Close the Test Sequence Editor and save the model.

Step 7. Initialize Test Suite

  1. Open the slrt_ex_osc_sltest_Harness1 Test Harness if not already open. Then, click on the slrt_ex_osc_sltest subsystem.

  2. On the Harness tab, click Simulink Test Manager.

  3. Select New > Test File.

  4. Name the Test File realtimetest.

  5. Right-click on the existing Test Suite and click Delete to remove the example Test File contents.

  6. Right-click the Test File and select New > Real-Time Test.

  7. In the new real-time test dialog box, enter Simulation in the Test Type field.

  8. Click Create.

  9. Rename the new Test Case to frequencysweep.

  10. Rename the new Test Suite to realtimesuite.

Step 8. Initialize System Under Test

  1. In the Simulink Test Manager, select Test Case frequencysweep.

  2. Select tab System Under Test.

  3. Set Model to slrt_ex_osc_sltest.

  4. Expand the Test Harness tab, set Harness to slrt_ex_osc_sltest_Harness1.

  5. Expand the Simulation Settings Overrides tab, select the Stop Time check box.

  6. Take the defaults for the other fields.

Step 9. Initialize Parameter Overrides

1. In Test Manager, select tab Parameter Overrides.

2. Click the Add button. A dialog box opens containing a list of parameters. If parameters are not visible, click the Refresh line at the top of the dialog box.

The refresh builds the model and uploads the model and block parameters from slrt_ex_osc_sltest_Harness1 and slrt_ex_osc_sltest.

3. Open Parameter Set 1 and select the Criterion, Frequency, w_close, and w_open check boxes.

Step 10. Create Scripted Iterations

To configure and control iterated runs of the Test Harness, a number of constants and variables provide input.

Test Harness constants include:

  • cStartFreq = 15.0 Start frequency of parameter sweep.

  • cStopFreq = 25.0 End frequency of parameter sweep.

  • cFreqIncr = 1.0 Frequency increment.

  • cWOpen = 0.90 Start of time window for evaluating criterion.

  • cWClose = 0.99 End of time window for evaluating criterion.

  • cCriterion = 0.025 Maximum normalized amplitude difference between Signal Generator and Integrator1 within the time window.

Test Harness variables include:

  • vfreq Frequency at each iteration.

  • vw_open Window opens once in each half-period.

  • vw_close Window closes once in each half-period.

  1. In Test Manager, select tab Iterations > Scripted Iterations.

  2. In the text box, enter the following code. To resize the Scripted Iterations text box, click and drag the lower-right corner of the box.

% Initialize constants
cStartFreq = 15.0;
cStopFreq = 25.0;
cFreqIncr = 1.0;
cWOpen = 0.90;
cWClose = 0.99;
cCriterion = 0.025;
% Loop through test frequencies
for vfreq = cStartFreq:cFreqIncr:cStopFreq
  % Create a new iteration
  testItr = sltest.testmanager.TestIteration();
  % Calculate the time window
  half_period = 0.5 * (1.0/vfreq);
  vw_open = half_period * cWOpen;
  vw_close = half_period * cWClose;
  % Set the parameters for the iteration
  testItr.setVariable('Name','Frequency','Source', ...
       'slrt_ex_osc_sltest','Value',vfreq);
  testItr.setVariable('Name','w_open','Source', ...
       '','Value', vw_open);
  testItr.setVariable('Name','w_close','Source', ...
       '','Value', vw_close);
  testItr.setVariable('Name','Criterion','Source', ...
       '','Value', cCriterion);
  % Name and add the iteration to the testcase
  str = sprintf('%.0f Hz', vfreq);
  addIteration(sltest_testCase, testItr, str);
end

Step 11. Run Test

  1. Connect to your target computer. Build and download real-time application slrt_ex_osc_sltest to the target computer.

  2. In the Simulink Test Manager, click the Run button with the Test Case selected.

  3. To view test results, in the left column, click Results and Artifacts. In this case, the test failed at iteration 23 Hz.

  4. To view the failing results, open nodes 23 Hz > Verify Statements and 23 Hz > Sim Output (slrt_ex_osc_sltest).

Step 12. Display Results

  1. In the Data Inspector tab, select the Subplots button.

  2. Select a 2x1 grid configuration.

  3. In the Simulation Data Inspector top display, select the two Out check boxes and the top Test Assessment check box. This assessment corresponds to the HiCheck substep.

  4. In the bottom display, select the two Out check boxes and the bottom Test Assessment check box. This assessment corresponds to the LoCheck substep.

  5. Click the Zoom in Time button and select the range 4.00-4.1.

In the top display, the vertical red line near 4.04 followed by a horizontal green line shows that the HiCheck test failed briefly before succeeding. In the bottom display, the vertical red spike near 4.02 followed by a horizontal green line shows that the LoCheck test failed briefly before succeeding.

See Also

(Simulink Test) | (Simulink Test)

Related Topics