MATLAB Examples

Test Real-Time Application

This example shows how to perform a frequency-response test of the model ex_slrt_slt_osc (matlab:open_system(docpath(fullfile(docroot, 'toolbox', 'xpc', 'examples', 'ex_slrt_slt_osc')))).

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.

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.

Contents

Test Harness Constants

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

vfreq                % Frequency at each iteration.
vw_open              % Window opens once in each half-period.
vw_close             % Window closes once in each half-period.

Step 1. Set Model Configuration Parameters

  1. Open ex_slrt_slt_osc in a writable folder.
  2. Open the Configuration Parameters dialog box.
  3. Open the Model Referencing node, and then set Total number of instances allowed per top model to One.
  4. Open the Data Import/Export node and make the following settings:
  • Set Format to Structure with time.
  • Set the Time and Output check boxes.
  • Clear the States, Final states, Signal logging, Data stores, and Log Dataset data to file check boxes.

Step 2. Create Test Harness

  1. In Analysis, select Test Harness > Create for Model. The software creates a test harness with the default name ex_slrt_slt_osc_Harness1.
  2. In the Basic Properties pane, select the Save Test Harnesses Externally check box.
  3. 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 panes.

8. Click OK.

The test harness looks like this figure.

The example model ex_slrt_slt_osc stores the test harness within the model. To access the test harness from the example model:

  1. In Simulink Editor, click Analysis > Test Harness > Manage Test Harnesses.
  2. Click ex_slrt_slt_osc_Harness1.
  3. To return to the example model, select it in the perspectives view in the lower right corner of the test harness.

Step 3. Set Test Harness Configuration Parameters

  1. Open ex_slrt_slt_osc_Harness1.
  2. Open the Configuration Parameters dialog box.
  3. Open the Model Referencing node, and then set Total number of instances allowed per top model to One.
  4. Open the Data Import/Export node.
  5. Set Format to Structure with time.
  6. Set the Time and Output check boxes.
  7. Clear the States, Final states, Signal logging, Data stores, and Log Dataset data to file check boxes.

Step 4. Configure Test Harness

  1. Open the Test Assessment block.
  2. To simplify the test assessment configuration, in the Input symbol list, replace input Outport with inputs Int1 and SigGen.
  3. In ex_slrt_slt_osc_Harness1, connect a Demux block to ex_slrt_slt_osc/Outport.
  4. In the Demux block dialog box, set Number of outputs to 2.
  5. To make the Demux outputs visible to the Test Assessment block, connect unitary Gain blocks to each of the Demux block outputs.
  6. Connect the top Demux block output to Test Assessment/Int1 and the bottom output to Test Assessment/SigGen.

Step 5. Configure Simulink Parameters

  1. On the toolbar, click the Model Explorer button.
  2. Click node ex_slrt_slt_osc_Harness1 > Model Workspace.
  3. In the toolbar, click the Add Simulink Parameter button.
  4. Add the following data 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 ex_slrt_slt_osc_Harness1 model workspace as model parameters, you access them by name directly, without model hierarchy.

6. Save the model.

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.

Step 7. Initialize Test Suite

  1. Click on the ex_slrt_slt_osc subsystem.
  2. Open Analysis > Test Manager.
  3. Select New > Test File.
  4. Name the test file realtimetest.
  5. Right-click the test file and select New > Real-Time Test.
  6. In the new real-time test dialog box, enter Simulation in the Test Type field.
  7. Click Create.
  8. Rename the new test suite to realtimesuite.
  9. Rename the new test case to frequencysweep.

Step 8. Initialize System Under Test

  1. In Test Manager, select node frequencysweep.
  2. Select tab System Under Test.
  3. Set Load Application From to Model.
  4. Set Model to ex_slrt_slt_osc.
  5. Set Target Computer to TargetPC1.
  6. In tab Test Harness, set Harness to ex_slrt_slt_osc_Harness1.
  7. In tab Simulation Settings Overrides, select the Stop Time check box.
  8. 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 ex_slrt_slt_osc_Harness1 and ex_slrt_slt_osc.
  3. Open Parameter Set 1 and select the Criterion, Frequency, w_close, and w_open check boxes. Leave the other check boxes cleared.

Step 10. Create Scripted Iterations

  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', ...
       'ex_slrt_slt_osc/Signal Generator','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. Build and download ex_slrt_slt_osc to the target computer.
  2. In Test Manager, on the toolbar, click the Run button.
  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 (ex_slrt_slt_osc).

Step 12. Display Results

  1. In the Simulation Data Inspector pane, select the Layout button.
  2. Select two horizontal displays.
  3. In the top Simulation Data Inspector 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.