| Contents | Index |
| On this page… |
|---|
Get Familiar with the Functional Design of the Model Get Familiar with the Simulation Testing Environment Check the Model for Adverse Conditions and Simulation Settings |
Get familiar with the functional behavior of the example model.
Get familiar with the role of the example test harness and its components.
Apply model checking tools to discover conditions and configuration settings resulting in inaccurate or inefficient simulation.
Run simulation tests on a model.
Ability to open and modify Simulink models and subsystems.
Understand subsystems and how to view subsystem details.
Understand referenced models and how to view referenced model details.
Ability to set model configuration parameters.
Ability to use the Simulink Model Advisor.
Before you use each example model file, place a copy in a writable location and add it to your MATLAB path.
rtwdemo_throttlecntrl.mdl
rtwdemo_throttlecntrl_testharness.mdl
This example uses a simple, but functionally complete, example model of a throttle controller. The model features redundant control algorithms. The model highlights a standard model structure and a set of basic blocks in algorithm design.
Open rtwdemo_throttlecntrl.mdl and save a copy as throttlecntrl.mdl in a writable location on your MATLAB path.

The top level of the model consists of the following elements:
| Subsystems | PI_ctrl_1 PI_ctrl_2 Define_Throt_Param Pos_Command_Arbitration |
| Top-level input | pos_rqst fbk_1 fbk_2 |
| Top-level output | pos_cmd_one pos_cmd_two ThrotComm1 |
| Signal routing | |
| No blocks that change the value of a signal, such as Sum and Integrator |
The layout uses a basic architectural style for models:
Separation of calculations from signal routing (lines and buses)
Partitioning into subsystems
You can apply this style to all types of models.
Explore two of the subsystems in the top model.
If not already open, open throttlecntrl.mdl.
Two subsystems in the top model represent proportional-integral (PI) controllers, PI_ctrl_1 and PI_ctrl_2. At this stage, these identical subsystems, use identical data. If you have an Embedded Coder license, you can use these subsystems in a example that shows how to create reusable functions.
Open the PI_ctrl_1 subsystem.

The PI controllers in the model are from a library, a group of related blocks or models for reuse. Libraries provide one of two methods for including and reusing models. The second method, model referencing, is described in Get Familiar with the Simulation Testing Environment. You cannot edit a block that you add to a model from a library. You must edit the block in the library so that instances of the block in different models remain consistent.
Open the Pos_Command_Arbitration subsystem. This Stateflow chart performs basic error checking on the two command signals. If the command signals are too far apart, the Stateflow diagram sets the output to a fail_safe position.

Close throttlecntrl.mdl.
To test the throttle controller algorithm, you incorporate it into a test harness. A test harness is a model that evaluates the control algorithm and offers the following benefits:
Separates test data from the control algorithm.
Separates the plant or feedback model from the control algorithm.
Provides a reusable environment for multiple versions of the control algorithm.
The test harness model for this example implements a common simulation testing environment consisting of the following parts:
Unit under test
Test vector source
Evaluation and logging
Plant or feedback system
Input and output scaling
Explore the simulation testing environment.
Open the test harness model rtwdemo_throttlecntrl_testharness.mdl and save a copy as throttlecntrl_testharness.mdl in a writable location on your MATLAB path.

Set up your throttlecntrl model as the control algorithm of the test harness.
Open the Unit_Under_Test block and view the control algorithm.
View the model reference parameters by right-clicking the Unit_Under_Test block and selecting Model Reference Parameters.

rtwdemo_PCG_Eval_P1 appears as the name of the referenced model.
Change the value of Model name to throttlecntrl.
Update the test harness model diagram by clicking Edit > Update Diagram.
The control algorithm is the unit under test, as indicated by the name of the Model block, Unit_Under_Test.
The Model block provides a method for reusing components. From the top model, it allows you to reference other models (directly or indirectly) as compiled functions. By default, Simulink software recompiles the model when the referenced models change. Compiled functions have the following advantages over libraries:
Simulation time is faster for large models.
You can directly simulate compiled functions.
Simulation requires less memory. Only one copy of the compiled model is in memory, even when the model is referenced multiple times.
Open the test vector source, implemented in this test harness as the Test_Vectors subsystem.

The subsystem uses a Signal Builder block for the test vector source. The block has data that drives the simulation (PosRequest) and provides the expected results used by the Verification subsystem. This example test harness uses only one set of test data. Typically, you create a test suite that fully exercises the system.
Open the evaluation and logging subsystem, implemented in this test harness as subsystem Verification.

A test harness compares control algorithm simulation results against golden data — test results that exhibit the desired behavior for the control algorithm as certified by an expert. In the Verification subsystem, an Assertion block compares the simulated throttle value position from the plant against the golden value from the test harness. If the difference between the two signals is greater than 5%, the test fails and the Assertion block stops the simulation.
Alternatively, you can evaluate the simulation data after the simulation completes execution. You can use either MATLAB scripts or third-party tools to perform the evaluation. Post-execution evaluation provides greater flexibility in the analysis of data. However, it requires waiting until execution is complete. Combining the two methods can provide a highly flexible and efficient test environment.
Open the plant or feedback system, implemented in this test harness as the Plant subsystem.

The Plant subsystem models the throttle dynamics with a transfer function in canonical form. You can create plant models to any level of fidelity. It is common to use different plant models at different stages of testing.
Open the input and output scaling subsystems, implemented in this test harness as Input_Signal_Scaling and Output_Signal_Scaling.


The subsystems that scale input and output perform the following primary functions:
Select input signals to route to the unit under test and output signals to route to the plant.
Rescale signals between engineering units and units that are writable for the unit under test.
Handle rate transitions between the plant and the unit under test.
Save and close throttlecntrl_testharness.mdl..
Before simulating a model and running simulation tests, use the Simulink Model Advisor to check the model for conditions and configuration settings resulting in inaccurate simulation of the system that the model represents.
Open your copy of the throttle controller model, throttlecntrl.mdl.
Start the Model Advisor by selecting Tools > Model Advisor. A dialog box opens showing the model system hierarchy.
Click throttlecntrl and then click OK. The Model Advisor window opens.
Expand By Product and Simulink. By default, all checks that do not trigger an Update Diagram are selected.
In the left pane, enable the unselected checks and select Simulink.
In the right pane, select Show report after run and click Run Selected Checks. The report shows a Run Summary that flags seven warnings.

Follow the report instructions to correct the warning conditions and rerun the checks.
In the MATLAB Command Window, enter mex -setup to set up your C compiler. Specify a valid, installed compiler.
Check that your working folder is set to a writable folder, such as the folder into which you placed copies of the example model files.
Open your copy of the test harness model, throttlecntrl_testharness.
Start a test harness model simulation.
The first time that the test harness runs, the code generation software compiles the referenced model. You can monitor the compilation progress in the MATLAB Command Window.
During the compilation, an error dialog box appears reporting the following error:
Model 'throttlecntrl' is referenced in Normal Mode and does not have 'Inline parameters' enabled. Go to the Optimization > Signals and Parameters pane of the Configuration Parameters dialog for model 'throttlecntrl' and enable 'Inline parameters'
Enable inline parameters for the throttle control model, as instructed in the error message.
Open throttlecntrl.mdl.
Select Simulation > Configuration Parameters > Optimization > Signals and Parameters > Inline parameters.
Click OK to save the change and close the dialog box.
Restart the simulation. When the simulation is complete, Simulink software displays the results.

The lower-right hand plot shows the difference between the expected (golden) throttle position and the throttle position that the plant calculates. If the difference between the two values is greater than ±0.05, the simulation stops.
Save and close throttle controller and test harness models.
A basic model architecture separates calculations from signal routing and partitions the model into subsystems
Two options for model reuse include block libraries and model referencing.
If you represent your control algorithm in a test harness as a Model block, be sure that you specify the name of the control algorithm model correctly in the Model Reference Parameters dialog box.
A test harness is a model that evaluates a control algorithm and typically consists of a unit under test, a test vector source, evaluation and logging, a plant or feedback system, and input and output scaling components.
The unit under test is the control algorithm being tested.
The test vector source provides the data that drives the simulation which generates results used for verification.
During verification, the test harness compares control algorithm simulation results against golden data and logs the results.
The plant or feedback component of a test harness models the environment that is being controlled.
When developing a test harness,
Scale input and output components.
Select input signals to route to the unit under test.
Select output signals to route to the plant.
Rescale signals between engineering units and units that are writable for the unit under test.
Handle rate transitions between the plant and the unit under test.
Before running simulation or completing verification, consider checking a model with the Model Advisor.
Support Model Referencing in the Simulink Coder documentation
Program Building, Interaction, and Debugging in the Simulink Coder documentation
Configuration Parameters in the Embedded Coder documentation
Working with Signal Groups in the Simulink documentation
![]() | About the Examples | Configure the Model and Generating Code | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |