MATLAB: How to run a part of the model at a slower rate on a Simulink Real-Time target

multitaskingSimulink Real-Timeslrtspeedgoat

My Simulink Real-Time application is running at a rate of 1e-5 seconds. A subsystem in it is run based on a trigger signal. However, when the trigger arrives, the time it takes to run the subsystem is probably more than 1e-5 seconds, and so I get a TET error, and an overrun.
However, this subsystem is called once in a long time, and I want to get a "multitasking" option of it – that is, to get it to run in the background, and update when it's finished. When I change the sample time of the subsystem to a slower rate of 1e-4, I get the following error:
ERROR: The enabled subsystem 'example/Subsystem1' executes in multiple tasks.
This can cause corrupted data or non-deterministic behavior in a
real-time system. Consider using a single-rate enabled subsystem
instead, or set the "Tasking mode for periodic sample times" option
in the Solver page of the Configuration Parameters Dialog to "SingleTasking".
Alternatively, you can disable this diagnostic by setting 'Multitask
conditionally executed subsystem' diagnostic to 'none' in the Sample
Time Diagnostics pane of the Configuration Parameters dialog box.

Best Answer

This error occurs because the model is missing a Rate Transition block. This results in a problem as the trigger signal is provided at a rate of 1e-5. But since the conditional subsystem is set to a sample time of 1e-4, a triggering signal at say time 3*1e-5 would not make sense to the subsystem which runs only at 1*1e-4 and then 2*1e-4. To fix this, please use a Rate Transition block.
Also, select the model ‘Allow tasks to execute concurrently’ check box in the model configuration parameters. This parameter check box is visible only if you convert an existing configuration set to one for concurrent execution. To enable this parameter, in the Model Explorer hierarchy pane, right-click and select Configuration > Show Concurrent Execution options. The Dialog pane is displayed with the Allow tasks to execute concurrently on target check box and a Configure Tasks button.
With respect to running different subsystems, which perform different tasks on the Simulink Real-Time target at different sample time, one can refer to the suggested workflow in the following article:
Notes regarding running multirate models on a Do-It-Yourself xPC target (R2017b and earlier):
The multirate feature takes advantage of the multicore processor feature to generate xPC Target code for multirate models. The target PC has to have multiple cores. Also, select the target PC Multicore CPU support check box in the xPC Target Explorer