MATLAB Answers

How do I use a timer to run 2 functions at different speeds?

1 ビュー (過去 30 日間)
Floortje Lycklama à Nijeholt
Floortje Lycklama à Nijeholt 2019 年 10 月 18 日
Commented: Floortje Lycklama à Nijeholt 2019 年 10 月 21 日
Hi guys,
I'm quite new at matlab, so I'm probably using a way to difficult solution for my problem.
I'm trying to have my code do two things at the same time. Currently it is increasing 1 value 1 till 400 and another till 2000. And I want to do this in one second. So value2 has to increase faster than value1.
However value 2 never reaches 2000. And the code is not fast enough.
Is it feasible to do what I want to do? And so yes, what am I doing wrong?
Thank in advance
The code I'm using for this is:
clc;
clear;
previousMillis1 = clock;
previousMillis2 =clock;
Rate1 = 400;
interval1 = 1/Rate1;
Rate2 = 2000;
interval2 = 1/Rate2;
value1 = 0;
value2 = 0;
while value2<Rate2
currentMillis = clock;
if ((currentMillis(6)- previousMillis1(6)) >= interval1)
value1 = value1 + 1
previousMillis1(6) = currentMillis(6);
end
if ((currentMillis(6) - previousMillis2(6)) >= interval2)
value2 = value2 + 1
previousMillis2(6) = currentMillis(6);
end
end

  2 件のコメント

Ganesh Regoti
Ganesh Regoti 2019 年 10 月 21 日
Hi Floortje,
I have a question. Do you specifically want that task to be done on MATLAB or using Simulink , StateFlow is also fine?
Floortje Lycklama à Nijeholt
Floortje Lycklama à Nijeholt 2019 年 10 月 21 日
I actually haven't use StateFlow before, but I think it would be fine as well.
In the parts where the values increase I actually want to read some values using labView. Do you think that would be possible with StateFlow as well?

サインイン to comment.

件の回答 (1)

Ganesh Regoti
Ganesh Regoti 2019 年 10 月 21 日
Hi Floortje,
According to my understanding on problem, you want to execute certain statements at particular time step and State Flow works perfect for such cases because when you execute in code format there might be some latency between execution and I/O operation which might give you wrong results.
Simulink/StateFlow executes the blocks/states at the step-time given explicitly and also implicitly. You can design your own states that would serve your purpose.
You can set fixed time step explicitly for the execution of states which would be GCD of two intervals in your case.
Model Configuration Parameters -> Solver Selection (Fixed-step) -> Solver details -> Fixed Step Size
Refer the following links for more understanding

  1 件のコメント

Floortje Lycklama à Nijeholt
Floortje Lycklama à Nijeholt 2019 年 10 月 21 日
Thanks!
I'll go check it :D

サインイン to comment.

サインイン してこの質問に回答します。


Translated by