How do I make a Simulink embedded function run independent of the time step of the model?

3 views (last 30 days)
So to explain on this: Let us say I have a simulink model with multiple blocks that I have to run for 100 sec with a time step of 1 sec. The model runs real time. I have added an embedded function added to the model that should run at time = 20 sec only once. However, the embedded function has lots of computations in it, and thus at time = 20 sec, the model takes some time to run the embedded function and thus does not run real time anymore. The question is: I need the function to run at time = 20 sec, but I dont need it to run in one time step necessarily (i.e. the results of the function do not feed into any other block or anything, it is just a result that I want to look at at the end of the 100sec). Can I make the function run independently of the model, i.e. have the model run in real time, and the function take the time it needs to finish independently (as long as it doesn't change the real time performance of the model)? Hopefully, my question makes sense.

Answers (1)

Venkatachala Sarma
Venkatachala Sarma on 13 Jan 2016
Hi,
It is currently not possible to execute a block as an independent task or a background task directly in Simulink. Simulink just does single tasking and does not support multi tasking at the moment. There are ways to start the block after a particular major step time like using a Clock block from Simulink/Sources and using simulation time feature of a block (get_param). Although you could design your code in the MATLAB function block to indirectly maintain real-time by saving the states and retrieving the states later, it is directly not possible to run a block in background.
One workaround for the case provided here is to save all the data in a '.mat' file and run the logic separately after the execution of the model.
Regards
-Venkatachala Sarma
  2 Comments
Nassim Samad
Nassim Samad on 19 Jan 2016
Yes I was thinking of doing that (running the logic after the execution of the model using a callback function). However, this would not work if I am building my simulink model into an executable to run real time on a hardware. How would I be able to run the logic after the execution of the model? My point is, if there is an input to the hardware that is processed and filtered through the built simulink model, how can I post process the data for it if there is no workspace or anything to save the data to?
Venkatachala Sarma
Venkatachala Sarma on 8 Mar 2016
Please take a look at this example.
Try using triggered subsystem with clock block to track time and implement the embedded function as a pre-emptable task using the example in the link above.
Hope this helps.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!