How to start and stop parallel Timer Delays (on / off delays) in Matlab

5 views (last 30 days)
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

Answers (1)

Walter Roberson
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
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.)
Bab
Bab on 25 Dec 2019
Dear Walter,
Merry christmas first of all :) Hope you enjoyed!
Unfortunatley with the code I showed you obove I am not able to move over the first round (first round #1). The original idea was to have two events one with 10 sec delay another with 5 and run them in paralle. Unfortunaley the sleep function would have stopped the other tasks in the whole application from running so it was not possible to use this.
But now I think I still miss the right way of calling or initiating events and I guess the key is to write this call function right:
event1 = {now, @PendingEvents_AnonymusFunction, nan(1,3)};
eventqueue = {event1};
current_event = eventqueue{1};
eventqueue(1) = []; %dequeue it
[newstatevector, nexttime] = current_event{2}(current_event{3});
current_event{1} = nexttime;
current_event{3} = newstatevector;
if ~isempty(nexttime)
eventqueue = queue_event(eventqueue, current_event);
end
With this call function I am not able to go over first round. There is no continuation so that I know it the delay has be elapsed or not and I can also not see how to set the second event to run in parallel.
For me it would help a lot to see in code how to run the simple two events in parallel.
Perhaps a comment for each line, what each line should do would help.
What do you think?
Thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!