フィルターのクリア

Discrepancy in Time Step Behavior Between Simulink Model and Generated C++ Code

17 ビュー (過去 30 日間)
Ghassen
Ghassen 2024 年 6 月 28 日 12:39
回答済み: Umar 2024 年 6 月 30 日 15:22
I have developed a Simulink model where the simulation time progresses in fixed steps of 0.2 using a Clock. However, when I generated and inspected the corresponding C++ code, the behavior of the time step (`rtb_switch`) differs. Here is an example comparison:
In Simulink (expected behavior):
0
0.2
0.4
0.6
0.8
1.0
In the generated C++ code:
rtb_switch = 0.0
rtb_switch = 0.1
rtb_switch = 0.15
rtb_switch = 0.2
rtb_switch = 0.3
rtb_switch = 0.35
rtb_switch = 0.4
rtb_switch = 0.5
Details:
- I have set the Simulink solver to use a fixed-step solver.
- Upon inspecting the generated C++ code, I observed the use of major and minor time step handling mechanisms (`rtmIsMajorTimeStep` and `rtmIsMinorTimeStep`) which I'm guessing where the issue is actually from.
- Here is a snippet of the generated C++ code where the time step logic is implemented:
```cpp
// Snippet from generated C++ code
void controller_module::step()
{
double rtb_Add;
double rtb_Switch;
if (rtmIsMajorTimeStep((&controller_module_M))) {
rtsiSetSolverStopTime(&(&controller_module_M)->solverInfo,
(((&controller_module_M)->Timing.clockTick0 + 1) *
(&controller_module_M)->Timing.stepSize0));
}
if (rtmIsMinorTimeStep((&controller_module_M))) {
(&controller_module_M)->Timing.t[0] = rtsiGetT(&(&controller_module_M)->solverInfo);
}
// Clock: '<Root>/Clock'
rtb_Switch = (&controller_module_M)->Timing.t[0];
std::cout << "rtb_switch = " << rtb_Switch << std::endl;
// Additional calculations and outputs follow...
}
```
- How can I ensure that the generated C++ code adheres strictly to the fixed-step size of 0.2 as defined in my Simulink model?
- Are there specific adjustments or configurations I should apply within Simulink or in the generated code to enforce this fixed step size?

回答 (1 件)

Umar
Umar 2024 年 6 月 30 日 15:22
Hi Ghassen,
To enforce a strict adherence to the fixed-step size of 0.2 in the generated C++ code from your Simulink model, you need to make adjustments in both Simulink and the generated code.
Simulink Configuration:
Ensure that the Simulink solver settings match your desired fixed-step size. Set the solver type to Fixed-step and specify the step size as 0.2 in the Solver Configuration parameters. Generated C++ Code Modifications:
In the generated C++ code snippet you provided, the issue lies in how the time step is handled. To correct this, modify the logic in the controller_module::step() function to explicitly use the fixed step size. Update the time step calculation to increment by 0.2 in each major time step iteration, ensuring consistency with the Simulink model.
Here's an example modification to enforce the fixed step size:
if (rtmIsMajorTimeStep((&controller_module_M))) { rtsiSetSolverStopTime(&(&controller_module_M)->solverInfo, (((&controller_module_M)->Timing.clockTick0 + 1) * 0.2)); // Set fixed step size to 0.2 }
By aligning the C++ code's time step handling with the fixed step size specified in Simulink, you can ensure that the generated code follows the expected behavior accurately
Hope this will help resolve your problem.

カテゴリ

Help Center および File ExchangeSimulink Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by