How to create simulink model for simulate tasks running with different frequency, priority

6 views (last 30 days)
Hi all,
I have 3 tasks with: task 1 frequency 10s, task 2 frequency 15s, task 3 frequency 30s.
About priority: task 3 > task 1 > task 2
Please help me for create simulink model with real running time.
Thank you so much

Answers (1)

Sandeep Mishra
Sandeep Mishra on 7 Oct 2024
Hi Galaxy,
I recognize that you are working on creating a Simulink model with three tasks running at different frequencies and priority levels.
To run tasks at different frequencies, you can use the Simulink "Clock" block to obtain the current simulation time. Based on this time, create a MATLAB function within a “MATLAB function” block, using the "Clock" output to generate “enable” signals for each task.
Here’s an example code snippet that enables tasks at intervals of 2, 3, and 5 seconds:
function [task1, task2, task3] = fcn(clk)
% Initializing enable values for different tasks
task1 = false;
task2 = false;
task3 = false;
% Enabling task1 variable after every 2 seconds
if mod(clk, 2)==0
task1 = true;
end
% Enabling task2 variable after every 3 seconds
if mod(clk, 3)==0
task2 = true;
end
% Enabling task3 variable after every 5 seconds
if mod(clk, 5)==0
task3 = true;
end
end
You can use the “Enabled Subsystem” block for each task by passing the “enable” signal generated by the MATLAB function block to the "Enable" input of each respective “Enabled Subsystem” block.
Furthermore, to set the priority of each task, you can refer to the following steps:
  • Right-click the “Enabled Subsystem” block and open the “Properties” section.
  • In the “General” section, adjust the “Priority” field to set the desired level.
  • Set the “Execution Order” is set to “Based on priority” within the same section.
For more information, refer to the following MathWorks Documentation:
  1. ‘Clock’ block: https://www.mathworks.com/help/releases/R2024b/simulink/slref/clock.html
  2. ‘Using Enabled Subsystems’: https://www.mathworks.com/help/releases/R2024b/simulink/ug/enabled-subsystems.html
  3. Set Priority in Execution Order of Blocks: https://www.mathworks.com/help/releases/R2024b/simulink/slref/set-priority-in-execution-order-of-blocks.html
I hope this helps you!

Products

Community Treasure Hunt

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

Start Hunting!