Search code examples
matlabsimulinks-function

Setting a Level-2 Matlab S-Function Block Sample-Time to specific time intervals


Matlab beginner here and I'm stuck on some problem and neither Matlab documentation nor other posts here could help me. I'm working on a Level-2 Matlab S-Function that receives a sensor signal via USB (Gyroscope, Magnetometer and Accelerator signals) and I have to analyze this data every 200ms for more high level tasks. At first I thought I can just set the simulation time of Simulink model to Start time 0 and Stop time 10 and make set my blocks sample time at 0.2 through block.SampleTimes = [0.2 0]; but then after getting some unexpected behavior I found out that through the documentation that simulation time is not the same as clok time in Simulink:

Simulation time is not the same as clock time. For example, running a simulation for 10 seconds usually does not take 10 seconds. Total simulation time depends on factors such as model complexity, solver step sizes, and computer speed.

I'm really stuck now as I'm required to output the sensor data exactly every 200ms and I don't have any prior experience with Matlab or Simulink at all. I hope my problem is clear enough but I will provide more details if required. Thanks for any help in advance.


Solution

  • Simulink (and MATLAB) run on Windows, Linux, OSX, etc... which are not real-time operating systems. Therefore MATLAB code or Simulink models will never run in real-time. The only way to ensure hard real-time execution of a Simulink model is to generate C code from it with Simulink Coder and Embedded Coder, and run the generated code on a real-time (embedded) platform.

    Having said that, if your Simulink model runs faster than real-time, you can slow it down to run "pseudo" real-time, which will be as close to real-time as you can get on a non real-time operating system. See Slowing Down Simulink for more details.