Clear Filters
Clear Filters

Using C functions in Simulink with variable step solvers

2 views (last 30 days)
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 Comments
Lucas Lebert
Lucas Lebert on 6 Dec 2016
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 on 11 Jan 2017
Edited: Johan on 11 Jan 2017
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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!