# Simulink Models and Code Generation
Simulink is often used to model controllers for complex physical systems. Since the modelled logic will be used in embedded controllers, there are code-generation tools that generate low-level code from the simulink models.
This page describes how Teamscale supports connecting the generated code to the Simulink-Model elements from which the code was generated.
# Problem Description
In a typical use case, the code generated from a simulink model will not be maintained directly. If there are problems, they will be addressed in the Simulink model and the next generator run will overwrite the generated code. The generated code is not directly maintained and therefore, code-readability findings on the generated code are not interesting.
However, findings concerning correctness or performance in the generated code are interesting and should be addressed. Even more interesting would be code-coverage of the generated code. How much of the original Simulink model is actually covered by testing of the generated code?
# Linking generated code to Simulink Models
Teamscale supports linking "back" from generated code to the Simulink model for the code generator
PLCcoder (opens new window) from Mathworks.
PLCcoder generates Structured Text code (IEC 61131-3).
The code contains hints that allow us to determine the Simulink Blocks from which the code was generated.
To enable this, Teamscale requires a special meta-information
traceInfo.mat file that PLCcoder generates when executed with the
Generate traceability option.
If the file is provided, Teamscale will map code regions in the generated code to Simulink blocks. For any mapped code region:
- Findings on the code will be copied and attached to the Simulink blocks.
- Coverage on the code will be mapped to the corresponding Simulink model.
The coverage per model will appear in the new metric
Model Line Coverage.
- The code view in the Teamscale UI will provide a link from the comment (e.g.,
'<S1>/Weight') to the Simulink block. Use CTRL + left click to follow the link.
# Setup of the generated-code Tracing
To enable generated-code tracing, several options need to be enabled in the project configuration and analysis profile.
In the analyis profile, enable analysis of the languages
IEC 61131-3 STand
Simulink and Stateflow. IEC is required to get findings/coverage to be mapped. Simulink is required to have something to map the findings/coverage to.
As last step in the analysis-profile editor, enable the option
Enable Simulink tracing
Included file namesoption in the project settings, make sure to include your IEC files (e.g.,
**.st) and your Simulink files (e.g.,
Analysis report mappingoption in the project settings (advanced settings), add the entry
**/traceInfo.mat -> SIMULINK_MAT.
The generated Structured Text code and the corresponding
traceInfo.matfile must be committed to the analyzed code repository or to another content source configured in the Teamscale project (e.g., Artifactory/S3, documentation). Since code links might change over time, the
traceInfo.matfile should be regenerated and committed each time the code is regenerated. When code and
traceInfo.matfile are committed they should ideally match the Simulink models stored in the current repository commit.
Due to the rather complex relations between Simulink models, generated code, and the
traceInfo.mat file, we need to make some assumptions on the usage scenarios.
These assumptions lead to the following limitations of the code-tracing feature:
- We can consume only one
traceInfo.matfile per project and branch. This means that at any time only one
traceInfo.matfile will be used by Teamscale. If a new commit introduces a new file, its information will overwrite the information from any previous file (even if they have different file paths and the "old" file still exists in the repository).
- If the simulink models change without new code generation, then Teamscale can't maintain correct tracing. Teamscale will provide the old links as far as possible, but over time more and more links will break until new generated code is committed.