How to start and stop parallel Timer Delays (on / off delays) in Matlab
5 views (last 30 days)
Show older comments
Hi everyone. I am trying to write a code in matlab for the on delay and off delay for several parallel events.
Each event on its own should be represented as shown in the picture attached:
The code sould be written in matalb and should also be supported for C/C++ Code Generation.
The idea is to write a function which can be called by several events in parallel. I am not using any additional toolboxs for the moment to create this solution.
So far I have written this function which is not working as expected:
Thanks in advance for your help.
function Q=TimeDelay(IN,PT)
t = timer('TimerFcn', 'Q=true','StartDelay',PT);
switch IN
case 1
Q=false;
start(t)
while(Q==false)
disp(Q)
pause(1)
end
stop(t)
%delete timer objects after using
delete(t)
case 0
stop(t)
%delete timer objects after using
delete(t)
Q=false;
end
end
0 Comments
Answers (1)
Walter Roberson
on 17 Dec 2019
The code sould be written in matalb and should also be supported for C/C++ Code Generation.
The idea is to write a function which can be called by several events in parallel.
You have no hope of achieving your aims with MATLAB. Timers are not supported for code generation, and Parallel Computing Toolbox is not supported for code generation.
18 Comments
Walter Roberson
on 24 Dec 2019
Refer back to the example I gave:
count = 0;
for K = 1 : 10
fprintf('first round #%d\n', K);
count = count + 1;
pause(1);
end
for K = 1 : 5
fprintf('second round #%d\n', K);
count = count + 1;
pause(2);
end
fprintf('final count: %d\n', count);
the 10 that is the upper bound of the first loop becomes the limit of 10 for majorstate 1, and the 5 that is the upper bound for the second loop becomes the limit of 5 for majorstate 2.
The 10 was pulled out from your originally posted code that looped displaying values and pausing in-between. The 5 was pulled out "from thin air" in order for me to illustrate that the state machine approach does not need to use the same limits for each loop.
When you write state machines, it would be possible to number all of the states consecutively, and use only one state counter, but if you do that then it becomes a nuisance to edit the code (you end up renumbering states a lot.) It is usually easier to give a "major" state number to each outer block of code, and then use increasingly many states to designate further nested blocks. In the above example, there is an initialization phase (which I put into state 0), then the 1:10 block is the first important block so I assigned it a major state of 1 and the second important block a major state of 2; those two were each for loops, which need a state variable corresponding to the current value of the loop control variable. If there had been a nested loop, a third state would have been necessary.
That said, you really only need a state variable for any information you need to preserve while you give up control; if you were to take the same code as above but remove the pause(2) so that there was no waiting needed there, then you could just run that entire block and the "final count" final block all together as soon as the code detected that it was finished with the first for loop.
You need as many state entries as the maximum length of temporary information that you need to preserve while you give up control. However, as temporary variables go into and out of existence, keeping track of the minimum length needed right now and marshalling the values into and out of that minimum length, gets to be a nuisance. What is usually easier instead is to reserve a state entry for each variable that needs to be preserved at some point, without worrying too much about whether you could get away without perserving it this time. (Though at the same time, a practice of re-using variable names helps keep the number of state entries lower.)
See Also
Categories
Find more on Logical 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!