Using C functions in Simulink with variable step solvers
2 ビュー (過去 30 日間)
古いコメントを表示
We're using Simulink to simulate real world physics to test control algorithms to be run on embedded hardware. The controllers are written in C so I'm using coder.ceval in a Matlab Function block to call them. As the controllers use static variables to store information from previous steps there's a problem with the Simulink variable step solvers taking negative timesteps to improve accuracy. Is there a way to avoid the negative time steps reaching the external C functions or what's the best way to solve the issue?
As developing the controllers in Matlab and then porting them to C has caused us problems before, I want to use the actual control functions in C with the simulations.
3 件のコメント
Lucas Lebert
2016 年 12 月 6 日
Now the more detailed explanation: Simulink can add Minor time steps to improve the accuracy of the simulation. These minor time steps occur only with continous solvers and not with discrete solvers. Most importantly, these minor time steps are not displayed in the scopes nor can the be logged easily to the MATLAB workspace. You can find more information about minor time steps on the following link:
https://de.mathworks.com/help/simulink/ug/types-of-sample-time.html
If you want to visualize the minor time steps, there is a block for that on the file exchange:
https://de.mathworks.com/matlabcentral/fileexchange/22695-minor-time-step-logging
With that being said, let me picture an example: Let's say you have a c-function with a static variable, which is incremented every time the function gets called. If you include this function with coder.ceval in a MATLAB function block, which gets driven by a continous input signal (which is not fixed in minor step), the function gets called both in the minor steps and the major steps. If you log the output to the Workspace you will notice, that the value gets incremented multiple times during one major time step. If you want to avoid, that the c-function is called multiple times during one major time step, you have to make sure, that the MATLAB function block is only executed every major time step. This can be achieved by the workaround I described above. Actually this is also the reason, why I prefer using C-Mex S-Functions over the coder.ceval approach.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で String についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!