How can I pass a tunable struct parameter as an input to the generated step function in Embedded Coder for a Simulink model using reusable function packaging?

28 次查看(过去 30 天)
Hello MathWorks team,
I have a Simulink model named experiment_demo.slx that includes a MATLAB Function block with the following code:
function adjusted_setpoint = fcn(setpoint, cfg)
adjusted_setpoint = setpoint * cfg.gain * cfg.offset;
  • setpoint is provided via an Inport.
  • cfg is a tunable parameter defined as a Simulink.Parameter with a Bus type (ModelConfigBus) and configured via the Model Explorer.
Model configuration:
  • Solver: Fixed-step
  • Code Generation:
  • System target file: ert.tlc
  • Code interface packaging: Reusable function
Code is generated using Embedded Coder. In the generated code:
  • The ModelConfigBus struct is defined in experiment_demo_types.h as:
typedef struct {
real_T gain;
real_T offset;
} ModelConfigBus;
My goal is to pass the cfg parameter directly into the generated experiment_demo_step() function, so that it can be configured from an external source (e.g., another module or application). Ideally, I would like the struct to be declared in experiment_demo.h and used as an input argument in experiment_demo.c.
Questions:
  1. What is the recommended way to expose a tunable struct parameter like cfg as an input to the step() function in the generated code?
  2. Is there a configuration or customization (e.g., via code interface settings or TLC customization) that allows this struct to be passed explicitly to the step function?
  3. Would moving the struct definition from experiment_demo_types.h to experiment_demo.h be supported or recommended for this use case?
Thank you!

采纳的回答

Aabha
Aabha 2025-8-22,7:01
Hi @Manoj.
To pass the ‘cfg’ struct as an input to the ‘experiment_demo_step()’ function, rather than using it as a global variable, you may replace the current 'Simulink.Parameter' object with a ‘Bus Inport’ in your Simulink model. The ‘inport’ should use the same bus type, ‘ModelConfigBus’, which defines the structure of the ‘cfg’ data. By making ‘cfg’ an explicit input signal instead of a parameter, Simulink will treat it as an input argument when generating code.
Next, in the Configuration Parameters, under ‘Code Generation’ > ‘Interface’, set the Code interface packaging option to ‘Reusable function’. This setting allows Simulink and Embedded Coder to generate a function interface where model inputs and outputs are passed as arguments, rather than accessed via global variables. Once configured this way, the generated ‘experiment_demo_step()’ function will accept ‘cfg’ as an input pointer.
As for the struct definition itself, Simulink automatically places the ‘ModelConfigBus’ type in the ‘experiment_demo_types.h’ header file. This is the correct and intended location for type definitions. If you need the struct to be accessible through ‘experiment_demo.h’, for integration purposes, ensure that ‘experiment_demo.h’ includes ‘experiment_demo_types.h’. It is not recommended to manually move the struct definition between files, as these are auto generated and subject to regeneration during code updates.
This method avoids the need for TLC customization, as Simulink's standard configuration options fully support passing structured data as function arguments when using bus signals and reusable function interfaces.
Please refer to the following documentation links for more information about the same: https://www.mathworks.com/help/ecoder/ug/control-data-interface-in-the-generated-code.html
I hope this answers your questions.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by