Variable timestep as input to generated embedded C code

4 views (last 30 days)
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.

Answers (1)

Vandit
Vandit on 5 May 2023
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:

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!