Simulink has a mechanism to generate tasks at different rates within your model, this example shows a way to create three tasks running at 1ms, 10ms and 100ms each, this may be helpful to someone looking to implement this type of systems without having to reinvent the wheel.
Mathworks has some articles with more detail on Modeling for Multitasking Execution, you can visit their website for more details. This example is intended to be a simple tutorial on how to create a simple multitasking model and toggle three digital outputs at different rates as defined in each task.
This example uses
Starting from a blank model then you first must define the step size, go to Model Configuration Parameters on the menu as shown below, or Ctrl + E
Make sure “Solver” is selected on the left menu, and set the settings as shown below:
You can change the settings on the solver to suit your specific project, as always Mathworks help is a great resource.
Next I will create three subsystems and name each one with the rate at which they will be running.
Then, right click on each Subsystem block and select the "Block Parameters (Subsystem)", on the window change the following:
At the end you'll have your three atomic subsystems with the sampling rate defined for each task.
Inside each task subsystem, place a GPO Write to toggle an output. I used the Color LED so I could have the LED changing accordingly.
For the 1ms task (Red LED):
For the 10ms task (Blue LED):
For the 100ms task (Green LED):
Once the system is built, Simulink uses a color coded scheme to differentiate the blocks running at a different rate as you can observe below:
If you take a look under the hood into the generated code you can see that Simulink generates some functions multitasking_step0, multitasking_step1 & multitasking_step2, each function is called according the Rate Monotonic Scheduler implemented by the Simulink coder.
Below is the oscilloscope capture from the PTD15, PTD0 & PTD16 outputs:
I set up some measurement boxes to measure the positive pulse duration on each signal, as you can see the pin is toggling at the expected rates of 1ms, 10ms and 100ms respectively.
Hopefully this example is helpful to someone in this forum.
Regards,
Hello @gramirezv @mariuslucianand
The above Explanation for the topic is very clear and too useful.
I have done few trails using the same method and have observed various result would like to share you my observation and understand the reason in broader level. I am using S32k144.
Here is the description of test and there observations. I have even attached file containing all trails and their results.
S.No | Subsytem_1 Sample Time | Subsytem_2 Sample Time | Description | Image Name | Observation on Scope | |
PTD15 | PTD0 | PTD15 | PTD0 | |||
1 | 0.001 | 0.002 | Data Stores(A B C) Placed outside subsystem 2.Data been Processed in MATLAB function but some Constant are transmitted on CAN | MultiTasking_01 | +ve Width shows two values 0.002 and 0.003 -ve width is 0.001 | +ve and -ve width both has 0.004 |
2 | 0.001 | 0.002 | Data Stores(A B C) Placed in subsystem 2.Data been Processed in MATLAB function and then transmitted on CAN | MultiTasking_02 | +ve Width shows two values 0.002 and 0.003 -ve width is 0.001 | +ve and -ve width both has 0.004 |
3 | 0.001 | 0.002 | Data Stores(A B C) Placed in subsystem 2.Data been Processed in MATLAB function but some Constant are transmitted on CAN | MultiTasking_03 | +ve and -ve width both has 0.001 | +ve and -ve width both has 0.002 |
I am unable to figure the difference in LED behavior with change in position of Data Stores.(Observed in CASE 1 and CASE 3).
Similarly, Change in LED when data store and Constant is transmitted Via CAN (Observed in CASE 2 and CASE 3).
Hope for you Help!!
Regards
Gopal Rathi
hi @gramirezv and @constantinrazva
I have a complex model (BMS model) for S32K144EVB.
When I try 1s sample time, everything is calculating correctly, but when I changed sample time 50ms some blocks of model (stateflow) is lagging for couple of timesteps (basically showing same values for couple of of steps which are increased 50ms when I look at freemaster). My model solver is discrete and has one sample time. So at this point should I use multitasking mode and change some blocks sample time? By the way, blocks are using inputs/outputs each other. Is using different sample times a problem for data exchange for that blocks?
Or maybe it is not possible to run all model at that sample time, how should I know the sample time that I can run without any problem on thi MCU?
Thanks
Hello @bcelikte,
From your description it looks like you would need to profile your software. You need to verify what parts of your model are taking more time.
Without having more details on your model. Your problem could be from different sources, but here are some suggestions you could take a look at:
One is the refresh rate from freemaster, I haven't played in deep with it's settings but what you're seeing could be freemaster not updating fast enough, hence you're seeing the same data for more than one timestamp.
Also could be that when you have your time step to 1s your code has enough time to complete, and when you crank it down it takes more than 1 step to complete. The best thing to do is to profile your software and see blocks take more time to execute. I haven't worked a lot with the profiling blocks on the S32K144 but i'm sure you can find some examples in the toolbox.
Obviously all depends on your application and the way you architected your model, you need to analyze and verify what blocks can work at different rates. The key is that it's a wild broad topic to be summarized without specific details.
Regards,
Thank you gramirezv for this insightful article ! It's a great addition for our featured content. Very detailed explanation with a hands-on example as well (+oscilloscope screenshot bonus!).
Thank you for having the initiative to write such an article and share knowledge with all the members of our community! Hopefully you've also inspired others to share their insights, too.
Best regards,
Razvan.
I'm glad you find this helpful for the community, I always believe that if I had troubles figuring out something others may have faced something similar.
Regards!