フィルターのクリア

Variable timestep as input to generated embedded C code

10 ビュー (過去 30 日間)
Timothy Parry
Timothy Parry 2023 年 4 月 19 日
回答済み: Vandit 2023 年 5 月 5 日
Hello,
Is there a way to generate C code from the embedded coder app where I can have the timestep as an input? I want to tell the C code the timestep, where I can change it dynamically for each iteration. My first thought was using the rsim.tlc system target because it is compatible with a variable timestep.
Here is context:
I've connected a controller and rigid body dynamics model that was generated by embedded coder to a CFD fluid simulation. The CFD sim gives the controller the fluid forces and torques acting on a solid body, and the controller then gives the fluid simulation the resulting motion (position, velocity, and acceleration) of the body in the fluid. This all works in an iterative loop.
I've demonstrated this works, but the problem I'm running into is that the C code generated by matlab expects to be called every 0.001s for example. The CFD code uses a variable timestep, where it's timestep is sometimes much smaller than 0.001s. The CFD code assumes constant position, vel, accel for multiple timesteps until the Matlab function is called again, this creates a jump in the position values and the CFD code can't handle this instantaneous change. I would like to be able to feed the matlab code the timestep of the CFD code for each step, so that both codes are working with the same timestep.

回答 (1 件)

Vandit
Vandit 2023 年 5 月 5 日
Hi,
To feed the timestep of the CFD code to the MATLAB code you can perform the following steps as shown below:
  1. Add a MATLAB Function block that takes the timestep of the CGD code as an input in your Simulink model.
  2. Define a local variable in the MATLAB Function block and assigning the input argument to it.
  3. In the "Model Configuration Parameters" dialog box, go to the "Code Generation" tab and under the "System Target File" section, select "rsim.tlc" as the system target file.
  4. select "Variable-step" as the solver type and set the maximum step size to a value that is appropriate for your system.
  5. Under the "Code Generation" section, select "Custom Code" tab and add a custom parameter called "timestep" and set its value to "0.001" (or whatever default value you want to use).
  6. Call the "set_param" function in MATLAB and pass it the name of the Simulink model, the name of the solver, and the value of the timestep parameter.
  7. Generate the C code for your Simulink model using the Embedded Coder app.
When you run the generated C code, you can pass the timestep value from the CFD code to the MATLAB Function block as an input.
You can learn more about the “Code Generation” from embedded coder using the link given below:

カテゴリ

Help Center および File ExchangeComputational Fluid Dynamics (CFD) についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by