필터 지우기
필터 지우기

Using C functions in Simulink with variable step solvers

조회 수: 2 (최근 30일)
Johan
Johan 2016년 12월 5일
편집: Johan 2017년 1월 11일
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
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.
Johan
Johan 2017년 1월 11일
편집: Johan 2017년 1월 11일
Hi Lucas and thanks for your suggestion. Unfortunately I didn't have time to deal with this problem until now.
Your signal spec "workaround" solution works. I just figured that I could probably run the Matlab Function block at a fixed interval as well as the software loop will be running at a fixed repetition interval. Thanks for your help! Unfortunately, as you entered your suggestion as a comment, I can't mark it as an accepted answer.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by