Skip to Main Content Skip to Search
Product Documentation

Verifying Generated Code

About this Tutorial

Learning Objectives

Prerequisites

Required Files

Methods for Verifying Generated Code

Simulink software supports multiple methods for verifying the behavior of code generated for a system.

Test MethodWhat the Method DoesAdvantagesDisadvantages
MicrosoftWindows run-time executableGenerates a Windows executable and runs the executable from the command prompt

Easy to create

Can use C debugger to evaluate code

Emulates only part of the target hardware
Software-in-the-loop (SIL) testingUses an S-function wrapper to include the generated code in a Simulink model

Easy to create

Allows you to reuse the Simulink test environment

Can use C debugger to evaluate code

Emulates only part of the target hardware
Processor-in-the-loop (PIL) testingDownloads code to a target processor and communicates with it from Simulink; see How SIL and PIL Simulations Work

Allows you to reuse the Simulink test environment

Can use C debugger with the simulation

Uses actual processor

Requires additional steps to set up test environment

On-target rapid prototypingRuns generated code on the target processor as part of the full system

Can determine actual hardware constraints

Allows testing of component within the full system

Processor runs in real time

Requires hardware

Requires additional steps to set up test environment

Reusing Test Data By Importing and Exporting Test Vectors

When a unit under test is in the Simulink environment, you can reuse test data inside and outside of the Simulink environment.

  1. Save the Simulink data into a file.

  2. Format the data in a way that is accessible to the system code.

  3. Read the data file as part of the system code procedures.

You can also reuse data from an external environment, such as an interactive development environment (IDE) for a specific target, in the Simulink test environment. To do so, you must save the data in a format that MATLAB software can read.

Verifying Behavior of Model with Software-in-the-Loop Testing

You can automatically generate code from a Model block, wrap the code in an S-Function, and bring the S-Function into another model for software-in-the-loop testing.

  1. Configure the model you want to test.

    1. Open rtwdemo_throttlecntrl_testcode.mdl. Save a copy to throttlecntrl_testcode.mdl in a writable location on your MATLAB path. Proceed through the tutorial from this location.

    2. Open the Configuration Parameters dialog box. Set Hardware Implementation > Device vendor to Generic and Hardware Implementation > Device type to 32-bit x86 compatible.

    3. Click OK to apply the changes. Close the dialog box.

  2. Make sure that you can build an executable for throttlecntrl_testcode.

  3. Configure the test harness model.

    1. Open rtwdemo_throttlecntrl_testharnessSIL.mdl. Save a copy to throttlecntrl_testharnessSIL.mdl in a location on your MATLAB path. The test harness uses a Model block to access the model to verify with software-in-the-loop (SIL) testing.

    2. Right-click the throttlecntrl_testcode Model block and select Model Reference Parameters.

    3. Set Model name to throttlecntrl_testcode.

    4. Set Simulation mode to Software-in-the-loop (SIL) and click OK.

      After you configure the Model block for SIL verification, the block includes a (SIL) tag, as the following figure shows:

    5. Update the test harness model diagram (Edit > Update Diagram).

  4. Run the test harness. As the following plot shows, the results from running the generated code are the same as the simulation results.

Verifying System Behavior By Importing and Exporting Test Vectors

About the example_main.c

This example extends the example in Integrating Generated Code into External Environment. In this case, example_main.c simulates hardware I/O.

Open matlabroot/toolbox/rtw/rtwdemos/EmbeddedCoderOverview/verification_files/example_main.c.

This version of example_main.c has the following order of execution:

The example_main.c uses two functions, plant and hardwareInputs.

File NameFunction SignatureComments
Plant.cvoid Plant(void)Code generated from the plant section of the test harness. Simulates the throttle body response to throttle commands.
HardwareInputs.cvoid hardwareInputs(void)Provides the pos_req signal and adds noise from the Input_Signal_Scaling subsystems into the plant feedback signal.

A handwritten function, WriteDataForEval.c, logs data. When the test is complete, the function executes and writes test data to the file, ThrottleCntrl_ExternSimData.m. You can load this file into the MATLAB environment and compare the data to simulation data.

Importing and Exporting Test Vectors Using the Eclipse Environment

This tutorial uses the Eclipse Integrated Development Environment (IDE) debugger to build an embedded system.

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

  2. Add the additional files required to build an executable. In the Configuration Parameter dialog box, add the following paths for Code Generation > Custom Code > Include list of additional > Include directories:

    "$matlabroot$\toolbox\rtw\rtwdemos\EmbeddedCoderOverview\"
    "$matlabroot$\toolbox\rtw\rtwdemos\EmbeddedCoderOverview\verification_files\"
  3. Make sure the model configuration parameter Code Generation > SIL and PIL Verification > Create block is set to None.

  4. Build the model.

  5. Create a build folder on your C drive. Name the folder such that the path contains no spaces (for example, EclipseProjects/throttlecntrl/testcode). For this tutorial, you use the Cygwin Debugger, which requires that your build folder be on your C drive and that the folder path not include spaces.

  6. Unzip the throttlecntrl_testcode.zip file, in your working folder, into the build folder that you just created.

  7. Delete the ert_main.c and throttlecntrl_testcode.c files. Then, add example_main.c that you examined in Setting Up Main Function.

  8. Run the control code in Eclipse to generate the eclipseData.m file. This is the file that writeDataForEval.c generates.

    Consult Integrating and Testing Code with the Eclipse IDE or Eclipse IDE help.

  9. Plot the results that you get from the Eclipse environment. Compare the data from the Eclipse run with the data results from the test harness.

  10. Close throttlecntrl_testcode.

Testing with Processor-in-the-Loop Testing

For information, instructions, and demos, see SIL and PIL Simulation in the Embedded Coder.

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