| Contents | Index |
Identify methods available for testing generated code.
Test generated code in the Simulink environment.
Test generated code outside of the Simulink environment.
Access to installed versions of the Eclipse Integrated Development Environment (IDE) and the Cygwin Debugger. However, required integration tasks demonstrated in the tutorial are common to all integration environments. For information on how to install the Eclipse IDE and Cygwin Debugger, see Installing and Using an IDE for the Integration and Testing Tutorials
Able to read C code.
Familiarity with debugging tools and capabilities.
rtwdemo_throttlecntrl_testcode.mdl
rtwdemo_throttlecntrl_testharnessSIL.mdl
Files in matlabroot/toolbox/rtw/rtwdemos/EmbeddedCoderOverview/testcode_files, where matlabroot is your MATLAB installation folder
Simulink software supports multiple methods for verifying the behavior of code generated for a system.
| Test Method | What the Method Does | Advantages | Disadvantages |
|---|---|---|---|
| MicrosoftWindows run-time executable | Generates 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) testing | Uses 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) testing | Downloads 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 prototyping | Runs 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 |
When a unit under test is in the Simulink environment, you can reuse test data inside and outside of the Simulink environment.
Save the Simulink data into a file.
Format the data in a way that is accessible to the system code.
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.

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.
Configure the model you want to test.
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.
Open the Configuration Parameters dialog box. Set Hardware Implementation > Device vendor to Generic and Hardware Implementation > Device type to 32-bit x86 compatible.
Click OK to apply the changes. Close the dialog box.
Make sure that you can build an executable for throttlecntrl_testcode.
Configure the test harness model.
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.
Right-click the throttlecntrl_testcode Model block and select Model Reference Parameters.
Set Model name to throttlecntrl_testcode.
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:

Update the test harness model diagram (Edit > Update Diagram).
Run the test harness. As the following plot shows, the results from running the generated code are the same as the simulation results.

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:
Initialize data (one time)
while < endTime
Read simulated hardware input
PI_cnrl_1
PI_ctrl_2
Pos_Command_Arbitration
Write simulated hardware output
end while
The example_main.c uses two functions, plant and hardwareInputs.
| File Name | Function Signature | Comments |
|---|---|---|
| Plant.c | void Plant(void) | Code generated from the plant section of the test harness. Simulates the throttle body response to throttle commands. |
| HardwareInputs.c | void 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.
This tutorial uses the Eclipse Integrated Development Environment (IDE) debugger to build an embedded system.
Open your copy of the throttle controller model, throttlecntrl_testcode.mdl.
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\"
Make sure the model configuration parameter Code Generation > SIL and PIL Verification > Create block is set to None.
Build the model.
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.
Unzip the throttlecntrl_testcode.zip file, in your working folder, into the build folder that you just created.
Delete the ert_main.c and throttlecntrl_testcode.c files. Then, add example_main.c that you examined in Setting Up Main Function.
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.
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.
Close throttlecntrl_testcode.
For information, instructions, and demos, see SIL and PIL Simulation in the Embedded Coder.
Methods for verifying code generated for an embedded system include running a MicrosoftWindows run-time executable, SIL testing, PIL testing, and on-target rapid prototyping.
You can reuse test data by importing and exporting test vectors.
![]() | Integrating Generated Code into External Environment | Evaluating Generated 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 |