Local pointer rtM for embedded coder in ert_main.c

13 views (last 30 days)
Hi,
Setup:
  • Simulink Model
  • Generate C-Code for real-time target by embedded coder
  • Generate example main
In the example main the RT_MODEL rtM_ is defined with a file-wide scope as well as a pointer to rtM_ (rtMPtr).
static RT_MODEL rtM_;
static RT_MODEL *const rtMPtr = &rtM_; /* Real-time model */
static DW rtDW; /* Observable states */
static ExtU rtU; /* External inputs */
static ExtY rtY; /* External outputs */
In the main function of the example main, there is the definition of the pointer rtM=rtMPtr at function scope.
int_T main(int_T argc, const char *argv[])
{
RT_MODEL *const rtM = rtMPtr;
/* Unused arguments */
(void)(argc);
(void)(argv);
/* Pack model data into RTM */
rtM->dwork = &rtDW;
rtM->inputs = &rtU;
rtM->outputs = &rtY;
/* Initialize model */
codgen_initialize(rtM);
What is the reasoning behind the usage of the pointer rtM instead of using rtMPtr directly?
I'd also appreciate some guidance on best practices to use the generated code in a non-trivial codebase (e.g. not an empty main). As soon as the codebase is not just one file (e.g. rt_OneStep is in a different c file) I have to pass arround pointers to variables that are defined on a file scope in different files which seems like a code smell to me. Since there is so much documentation regarding the embedded coder it seems like I am missing some useful pages.

Answers (1)

Tobias Schindler
Tobias Schindler on 14 Jan 2021
This "issue" is even more pronounced if the coding interface "Pass rooot-level I/O" to "Structure reference" instead of "Part of model data structure".
The example main then uses the following structure:
void rt_OneStep(RT_MODEL *const rtM){
uz_codegen0_step(rtM, &rtU, &rtY);
}
The function OneStep is called in main and passed the local pointer rtM (which is set to the global pointer rtMPtr) and then the function OneStep uses the global varibles rtU and rtY directly.
This even more confuses me regarding the proper usage of the generated code structure and the reasoning behind the pointer rtM since the StepFunction uses two globals anyway. What is the benefit of the function scoped variable in this case?

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!