How to create a C/C++ routine from a Simulink block with the definition of output, input, and parameters in the header?

2 views (last 30 days)
Hello everyone,
The bottom line is that I need to create a Fortran/C/C++ routine from a Simulink block with the definition of output, input, and parameters in the header. In more detail, the case is as follows:
I have a Simulink model that implements structural dynamics as a first-order differential linear system. This model should interface with another dynamics, fluid dynamics, by defining a fluid-structure interaction problem (so closed-loop).
The structural model should receive as input the forces and moments from fluid dynamics, which in turn should receive as input the displacement field from structural dynamics (its output).
However, the integration of the two dynamics must take place at the c++ code level, so my goal is to generate a c++ code of the Simulink model describing the structural dynamics that is able to interface externally with the fluid-dynamics code.
Now, although I am clear on the basic process of generating a Simulink model code, I would like to understand how best to configure the generation process.
Specifically, I would like to understand how to generate a c++ code (or even better, a c++ function) that can properly (and iteratively, so at each call) receive the parameters and inputs from the fluid-dynamics code (forces and moments) and return the structural displacements (thus the structural dynamics outputs) in an externally readable form (from the fluid-dynamics code).
Thank you in advance for the support.
Marco
  5 Comments
Marco Pizzoli
Marco Pizzoli on 18 Jan 2024
Hi Benjamin, thank you first of all for your response.
I'll try to clarify a few aspects.
First, please note that my mention of Fortran was misleading, in fact I have now deleted it from the title. I am interested exclusively in C/C++.
Next, I have a Simulink model and external C++ code (which unfortunately I cannot share yet). I need to generate a C++ code from the Simulink model, which I can interface with the external C++ code, as in the following figure (following a closed loop strategy).
So, in generating the code from Simulink I have to make sure that the outputs are readable by the external code (and vice versa, for the outputs of the external code that will have to be read as input by the generated code). This interaction between the two codes should allow for the execution of a simulation over time of the whole in closed loop system (as is done in Simulink). Specifically, the two codes will need to query each other every Time Step. In this regard, another problem is precisely that of managing the Time Step and Maximum Simulation Time in the generation of the code, so that what I have written can be implemented.
I hope I have been clear.
Regards
Marco
Benjamin Thompson
Benjamin Thompson on 20 Jan 2024
You need to have the Simulink Embedded Coder toolbox to generate "readable" code. There are some helpful videos about the product:
If you have further questions about how to use the source code generated by this tool, attach some very simple models that you have tried yourself. Also attach the source code generated by those simple models if you are able to complete that process and ask further questions about any problems using the source code.

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!