Any Tic Toc function in Simulink for embedded blocks - matlab

I have a system with some embedded Matlab blocks where I'd like to perform some actions after a certain amount of time, in this case turn on lights and switches in an interface to which I send signals from Simulink.
The problem is that I thought I'd use "tic"-"toc" and "while" in a Matlab function block to perform these actions, say one parameter becoming 1 after 5 seconds, the following parameter becoming 1 after 12 seconds and so on, but I noticed that tic-toc apparently doesn't work in Simulink for embedded functions.
Is there any similar functions that could be used in Simulink for embedded functions or is there any other way to do this?
Edit: I've tried to get the clock's time as well, but it's a growing value. Is there any way to "lock" the time as a parameter when the block's function is executed?

You shouldn't be using absolute time in an embedded system, which is at least one of the reasons why tic-toc and clock from MATLAB don't work with Simulink Coder.
You should create your own counter, which you start and stop when you need to.
This is pretty easy to do using a Unit Delay and Summation block.
If you need to be able to enable and/or reset the counter then use the appropriate block from the Additional Discrete library.

Related

Sampling time as an input-simulink

I am trying to make a library of functions that will allow me to parameterize filters and other function. In simulink standard blocks, I am only able to set a sampling time prior, rather than take an input. Is there any ideas on how I can create this? My first approach was to use conditionals and a clock to allow this parameter to exist, but the clock cannot be parameterized either.
Thanks in advance.
The only way to do this is to write each of your blocks as S-Functions.
If you are using m-code then within the setup method you'll need to define the block sample time as
block.SampleTimes = [-2 0];
then within the output method you'll need to set the next sample time (based on the value of the input signal) by assigning the new value into block.NextTimeHit.
An example of doing this can be found here: A Square Wave with Jitter
A similar thing can be done in a c-mex S-Function by using the mdlGetTimeOfNextVarHit method.

import a continuous bitsream from workspace into simulink using "In" block and then buffer it using "buffer" block

how to import a bitsream form binary vector from workspace into simulink.Actually I have found that I can use simin block or In block but my binary vector is independant of time. I tried to use Const block and it works but afer that when I wanted to put my output in the Buffer block in simulink, it didn't work because the input is continuous and not discrete. So I am asking if it's a way to add time to my binary uni-dimensional without having any influence on the result?and how can I do it?
Or is there another way to import this date to avoid this problem with Buffer block?
Your screenshot shows your constant block to have a sample time of Inf. As the error message suggest, you need to change that to a discrete sample time. In addition, you should also:
check your model is using a fixed-step solver
check what time step you are using for your chosen fixed-step solver (ideally the same as your constant).
You can have multi-rate models, but you need to manage the rate transitions with Rate Transition blocks. For more details on sample times, see the documentation, in particular how to view sample time information in a Simulink model. You should probably also have a quick look at the Choose a Solver section.

How to vary a specific variable during a SIMULINK simulation using Matlab programming

I am trying to vary a variable every 10 seconds while a simulation on simulink is running. I defined (Kb+Ks)/N inside some Gain blocks and I want to vary only Kb from its minimum to maximum value and back to its minimum value during simulation. I have tried using set_param(model, parameter, value) but it varies (Kb+Ks)/N instead of only Kb. I have also tried a 'for loop command,' however this runs the simulations one at a time. Please how do I solve this problem?
As suggested in the comments, you can't do what you want with a simple gain block. Replace Kb with a Repeating Sequence block, a From Workspace block, or whatever source signal you want to use. Then add that signal to a constant block Ks/N and multiply the output of the add block to whatever signal was previously going through the gain block.

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).

control simulink from M-file

I am trying to control a simulink from a M-file.
What I want to do in the M-file is give the simulink model some input, run the simulink model, change one input value at 0.6 seconds, continue running the simulink model with the new input.
I already know that by using set_param, I can start, pause and continue the simulink, but the problem is I don't know how to pause the simulink model at a certain time(0.6s), is it possible to get the current time from simulink model and read it in the M-file?
Another way I already know is using sim to run simulink model from 0 to 0.6s, and use SimState to save the information at 0.6s, then load these information to resume the simulation. I am trying to change the input before the simulation resumed, but it seems that the model will load the input values from the information it saved, it won't take the new input value.
I stuck in this problem for a very long time, could someone help me with this please?
Thank you very much.
You can get the current time of a running simulation with:
get_param('simulink_model_name', 'SimulationTime');
So for instance by checking this value from your M-file during simulation by using
timer(...)
you can detect when the simulation is at 0.6 seconds.
I used a combination of simulink and m-script to achieve a similar goal.
In your model, add one 'assert' block. Double click it, and uncheck 'Stop Simulation when assertion fails'. In the 'Simulation Callback when assertion fails' field, add three commands:
set_param(bdroot,'SimulationCommand','pause');
run('myscript.m'); %insert the script name
set_param(bdroot,'SimulationCommand','continue');
Now connect the inport of this block to a 'not equal to' relational operator. Connect the first inport of the relational operator to a clock (pls set the decimation for analog clock or the sample time [usually -1 for inherited] for the digital clock).
The second inport is connected to constant block with a value of 0.6
On simulating the model, the simulation will pause at 0.6 sec, execute the m-file to change the input parameter (considering that it's tunable) and then continue with the simulation.
The assertion block is called when its input signal becomes 0. At 0.6 sec, the output of the relational operator will be 0.
Let me know if it worked.
This is not currently possible from an M-file. If you want to dynamically change the input at a given time externally, it will require an S-Function. Even this solution is difficult and wrought with flakey-ness since the Mathworks does not want to support this functionality in that it defeats one of the features of another toolbox they sell. In time, I believe they will grant this privledge, but it does not exist today. Also, why not use a dynamic input block to change the input value, like a map, signal builder, etc. ?