How to Make Blocks wait in Simulink? - simulink

I have two Blocks. Block A and Block B. Block A has a sampling time of 1 ms and Block B we do not know. It takes longer for Block B to do computing. I need to run A and then make it wait to run B. Until B is done with computing, A should not be run again. So every time A should run once and wait for B. How can I implement this on Simulink?
Thanks!

Related

GPU MATLAB gives different elapsed time between first and second execution

When i execute my code using Matlab parallel toolbox it gives me two different time execution between first and second time.
In fact the first time is very slow (more than CPU version) however the second time is faster and logical, and subsequent runs are the same as the second time. Why does this happen?
That is correct, and expected.
When you call it for the first time, it needs to initialize the GPU ("turn it on" in some sense), set up the CUDA contexts, etc etc. The second time you run it the GPU is ready to take anything you throw at it.
On top of that, depends on how you wrote the code, maybe the first time it requires to move some data to the GPU, and perhaps in the second time the memory is already there.
Often doing gpuDevice(1) will initialize the context enough, but otherwise just throw a small matrix multiplication to it to initialize.
All this is somehow true for other parallel computing paradigms in MATLAB, e.g. if you want to use parfor you need to initialize the parallel pool or it will take very long the first time.

MATLAB execution runtime limitation

I want to limit execution runtime of a function I am using in my code. Is it possible?
I am using Windows 10 with MATLAB R2015a (unlike in this question asked before: Matlab time limit for function execution), so has there been any changes to make it possible or are there any tweaks to make it possible?
For example:
H = transpose(homograpyMatrix);
t = projective2d(homograpyMatrix);
result = imwarp(img, t); % If execution takes more than X seconds - stop running
If imwarp is taking too long to compute (more than predefined X seconds) I want MATLAB to stop the running process. Is it possible?
Please note that I don't want to use tic-toc within a loop as a stopping condition, because this is not the case.
You have two options:
Parallel Computing Toolbox
With the Parallel Computing Toolbox, you can define a timeout for idle calculations. It is important to remember that this timeout is reset whenever your process enters a parfor loop or uses parfeval.
Hard-coded Timeout
In this instance you would implement a hard-coded timeout variable and check it at a regular interval to assess if you have to stop the running process or not.

How does "pause(n)" work in MATLAB?

I'm using MATLAB to read from a serial port. A colleague of mine is doing the same thing with LabVIEW. He told me that I needed a pause in my code to allow the system time to write the data back. However, I've read that "pause(n)" halts execution for n seconds.
I'm not totally sure what it means by "halts execution." Does is stop the serial port from reading and writing, therefore nullifying my purpose?
Should I use another function, or should pause(n) be okay for my purposes?
pause(n) basically makes your program sleep for n seconds. As such, when you invoke pause, it makes your program wait there for n seconds, then proceeds to the next line of code.
For example:
a = rand(3,3);
pause(2); % // Pause for 2 seconds
b = rand(4,4);
This creates a random 3 x 3 matrix stored in a, then the program waits at the second line for two seconds. The program does nothing and sleeps. After, a 4 x 4 random matrix is created.
To answer your question, this does not stop the serial port. All you're doing is allowing the data enough time to be written to the serial port before you decide to write more to the port. Similarly, you're allowing the serial port enough time to buffer enough data to the port so you can read the right amount of bytes in one read.

Simulink: How to convert event based signal with zero duration values to a time based signal without losing information

I have a matlab function block (which is not relevant) whose input is his previous output (loop). For example, if in a sample period the output is X, his input in the next sample period will be X, and so on.
This image shows a simplification of my simulation. I initialize the input of my function for the first loop.
The problem is that matlab functions recieves an event based signal from de initialization block in the first sample period (zero-duration), which I must convert to a timed based signal (so I can apply the unit delay that avoids an inifite loop, and allows to generate the next input as explained before). So, when I do so, I lose the information contained in the event-based signal (due to the zero-duration values) and the loop does not work.
If there was a way to intialize the loop in the time-based domain (green part of the image) so, in the first sample time, it is not a zero-duration signal, it would avoid the problem.
Is there any way to do so? Or, a different approach for this problem?
Two approaches come to mind
The initial condition can be set in the Unit Delay block, so it's not clear from your simplified example why you need the specific Initialization block.
You could just use a persistent variable inside the MATLAB Function block to maintain the state from one execution of the block to the next (noting that since it is event driven the block may not get called at every time step, only at each event triggger).

Worker interrupting main program in Matlab?

So basically what I want to do is have my main program to do some calculations, unrelated to a parallel program. The parallel program is constantly checking for some event to come true, and when it does, I want the main program to freeze and start another parallel job. Is it possible to do so in Matlab?
You can imagine it as a robot riding (main program) and at the same time checking its sensor data (worker). When it approaches an obstacle, a program to avoid the obstacle is started.
Thanks in advance,
Rugile :)
The best solution to this that I have seen is to use a Matlab timer object.
Implementation code would look something like this:
%Setup timer
t = timer;
t.ExecutionMode = 'fixedSpacing'; %See `docsearch Timer Object Execution Modes` for explaination
t.Period = 1; %Number of seconds after one execution to the start of the next
t.TimerFcn = #checkAndExecuteParallelJob
start(t);
%Start main job
mainJob();
In another file
function checkAndExecuteParallelJob
if (conditionIsFalse)
%Fast return
return;
end
%..Code to execute parallel job. This will block the main execution
Matlab is not multi-threaded, so the execution of a timer callback function will interrupt and block the execution of any main function. Timers can also interrupt each other, sometimes but not always, with a complex set of rules that I once tried to reverse engineer but have since given up on. However, for the relatively simple problem you laid out, I think that a timer object would be sufficient.