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
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 analysis profile editor, enable analysis for the languages
IEC 61131-3 ST
andSimulink and Stateflow
by selecting them fromLanguage & Tool Enablement
. IEC is required to get findings/coverage to be mapped. Simulink is required to have something to map the findings/coverage to.As a final step in the analysis profile editor, make sure to enable the option
Enable Simulink tracing
underAdvanced Options
.In the
Included file names
option in the project settings, make sure to include your IEC files (e.g.,**.st
) and your Simulink files (e.g.,**.slx
).In the
Analysis report mapping
option in the project settings (advanced settings), add the entry**/traceInfo.mat -> SIMULINK_MAT
.The generated Structured Text code and the corresponding
traceInfo.mat
file must be committed to the analyzed code repository or to another content source configured in the Teamscale project (e.g., Artifactory/S3). Since code links might change over time, thetraceInfo.mat
file should be regenerated and committed each time the code is regenerated. When code andtraceInfo.mat
file are committed they should ideally match the Simulink models stored in the current repository commit.
Limitations
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.mat
file per project and branch. This means that at any time only onetraceInfo.mat
file 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.