Test Generated Code
Validate generated code by using system-level S-functions or by running the code in an external environment.
Learn about:
Different methods for testing the generated code
How to test generated code in Simulink®
How to test generated code outside of Simulink
For information about the example model and other examples in this series, see Prepare a Control Algorithm Model for C Code Generation.
Validation Methods
Simulink supports multiple system testing methods for validating the behavior of generated code.
Windows® run-time executable: Generate a Microsoft® Windows executable and run the executable from the command prompt.
Benefit: Easy to create and enables use of a C debugger to evaluate code
Drawback: Partial emulation of target hardware
Software-in-the-loop (SIL) testing: Use an S-function to include the generated code in the Simulink model.
Benefit: Easy to create, enables reuse of the Simulink test environment, and enables use of a C debugger to evaluate code
Drawback: Partial emulation of target hardware
Processor in the loop (PIL) testing: Run a non-real-time cosimulation. Simulink executes a portion of the model (typically a plant model) while the target processor runs a different portion of the model (typically a controller). After you download code to the target processor, the processor-in-the-loop technology handles the communication of signals between Simulink and the target during the cosimulation.
Benefit: Enables reuse of the Simulink test environment, enables use of a C debugger with the simulation, and involves the target processor
Drawback: Requires additional steps to set up the test environment and the processor does not run in real time
On-target rapid prototyping: Run the generated code on the target processor as part of the full system.
Benefit: You can determine actual hardware constraints and test the component within the full system. The processor runs in real time
Drawback: Requires hardware and requires additional steps to set up the test environment
External Mode: Run the generated code on the target processor as part of the full system.
Benefit: You can determine actual hardware constraints and test the component within the full system
Drawback: Requires hardware and requires additional steps to set up the test environment
Reuse Test Data Outside of Simulink
In other examples in this series, you test the system through simulation in Simulink. The test input data come from a Signal Editor block in the test harness model.
To test the system outside of Simulink, you can reuse the test input data.
Save the Simulink test data into a file.
Format the data in a way that the system code can access.
Read the data file as part of the system code procedures.
To use Simulink to perform verification, you can save the test output data from the external environment in a format that MATLAB® can read.
In this example, the file hardwareInputs.c
contains the data from the Signal Editor block in the test harness model.
Test via Software-in-the-Loop (Model Block SIL)
Create and Configure Model Block for SIL
Simulink can generate code from a Model block, wrap the code into an S-Function, and add the S-Function back into the model for software-in-the-loop testing.
Open the example model, rtwdemo_PCG_Eval_P6
.
The test harness uses a Model block to access the example model. In this example, you run a software-in-the-loop test on the example model.
Right-click the Model block and select Block Parameters (ModelReference).
Set Model name to the name of the model that you want to test.
Set Simulation mode to
Software-in-the-loop (SIL)
.
The Model block now shows a (SIL)
tag.
Configure Models for SIL
Configure the example model, rtwdemo_PCG_Eval_P6
, and the test harness model, rtwdemo_PCGEvalHarnessHTGTSIL
, for SIL simulation by adjusting the hardware implementation parameters.
For both models, in the Configuration Parameters dialog box, select the Support long long check box.
Save a copy of the example model, rtwdemo_PCG_Eval_P6
, in your current folder.
Run Model Block SIL
In this example, the test harness model contains a Model block that is configured for SIL simulation.
The generated code and the simulation yield the same results.
Configure System for Testing via Test Vector Import and Export
This example extends the integration example in Build Integrated Code Outside the Simulink Environment. In this case, example_main.c
uses simulated hardware I/O.
The augmented example_main.c
file now uses this execution order:
1. Initialize data (one time)
while < endTime
2. Read the simulated hardware inputs
3. PI_cnrl_1
4. PI_ctrl_2
5. Pos_Command_Arbitration
6. Write the simulated hardware outputs
end while
Two functions, plant
and hardwareInputs
, supply the input test data.
Plant.c
: This function, which was generated from the plant portion of the test harness, simulates the throttle body response to throttle commands.
void Plant(void)
HardwareInputs.c
: This function provides the pos_req
signal and adds noise from the Input_Signal_Scaling
subsystems into the plant feedback signal.
void hardwareInputs(void)
The handwritten function in WriteDataForEval.c
logs output data from the test. After the test is finished, the function executes and writes the data to the file PCG_Eval_ExternSimData.m
. You can run this script file in MATLAB to access the data. You can then compare the data to the simulated data from Simulink.
To enable these additional files, identify them by using Configuration Parameters > Code Generation > Custom Code > Additional build information.
Test via Test Vector Import and Export (Eclipse Environment)
Before building an executable in the Eclipse™ environment, regenerate the code without the S-function interface.
To learn how to install and use Eclipse and GCC, see Install and Use Cygwin and Eclipse.
To install the files for this example automatically, click the hyperlink.
Automatically set up the build folder.
Alternatively, to manually install the files:
Create a build folder (
Eclipse_Build_P6
).Unzip the file
rtwdemo_PCG_Eval_P6.zip
into the build folder.Delete the files
rtwdemo_PCG_Eval_P6.c
,ert_main.c
, andrt_logging.c
, which are replaced byexample_main.c
.
Run the control code in Eclipse. The function in writeDataForEval.c
creates the file eclipseData.m
, which contains the output test data. In MATLAB, load this Eclipse data by running the script file. Then, compare this data with the data from the Simulink test harness model by using plot
.
For the next example in this series, see Evaluate Performance of the Generated Code.