Local pointer rtM for embedded coder in ert_main.c

8 ビュー (過去 30 日間)
Tobias Schindler
Tobias Schindler 2021 年 1 月 13 日
回答済み: Tobias Schindler 2021 年 1 月 14 日
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.

回答 (1 件)

Tobias Schindler
Tobias Schindler 2021 年 1 月 14 日
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?

カテゴリ

Help Center および File ExchangeDeployment, Integration, and Supported Hardware についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by