Skip to Main Content Skip to Search
Product Documentation

Get Familiar with the Example Model and Testing Environment

About This Example

Learning Objectives

Prerequisites

Required Files

Before you use each example model file, place a copy in a writable location and add it to your MATLAB path.

Get Familiar with the Functional Design of the Model

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.

View the Top Model

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:

SubsystemsPI_ctrl_1
PI_ctrl_2
Define_Throt_Param
Pos_Command_Arbitration
Top-level inputpos_rqst
fbk_1
fbk_2
Top-level outputpos_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:

You can apply this style to all types of models.

View the Subsystems

Explore two of the subsystems in the top model.

  1. 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.

  2. 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.

  3. 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.

  4. Close throttlecntrl.mdl.

Get Familiar with the Simulation Testing Environment

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:

The test harness model for this example implements a common simulation testing environment consisting of the following parts:

Explore the simulation testing environment.

  1. 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.

  2. Set up your throttlecntrl model as the control algorithm of the test harness.

    1. Open the Unit_Under_Test block and view the control algorithm.

    2. 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.

    3. Change the value of Model name to throttlecntrl.

    4. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. Save and close throttlecntrl_testharness.mdl..

Check the Model for Adverse Conditions and Simulation Settings

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.

  1. Open your copy of the throttle controller model, throttlecntrl.mdl.

  2. Start the Model Advisor by selecting Tools > Model Advisor. A dialog box opens showing the model system hierarchy.

  3. Click throttlecntrl and then click OK. The Model Advisor window opens.

  4. Expand By Product and Simulink. By default, all checks that do not trigger an Update Diagram are selected.

  5. In the left pane, enable the unselected checks and select Simulink.

  6. In the right pane, select Show report after run and click Run Selected Checks. The report shows a Run Summary that flags seven warnings.

  7. Follow the report instructions to correct the warning conditions and rerun the checks.

Run Simulation Tests

  1. In the MATLAB Command Window, enter mex -setup to set up your C compiler. Specify a valid, installed compiler.

  2. 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.

  3. Open your copy of the test harness model, throttlecntrl_testharness.

  4. 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'
  5. Enable inline parameters for the throttle control model, as instructed in the error message.

    1. Open throttlecntrl.mdl.

    2. Select Simulation > Configuration Parameters > Optimization > Signals and Parameters > Inline parameters.

    3. Click OK to save the change and close the dialog box.

  6. 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.

  7. Save and close throttle controller and test harness models.

Key Points

Learn More

  


Related Products & Applications

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