sampling time counter in simulink - simulink

how can I take simulink time from "clock" block in simulink and then increase a counter (k)
in a matlab embedded function at every sampling period Ts?
let us say if simulink clock time is t, then we may consider
if mod(t, Ts)==0
k=k+1;
end
but this will not work since simulink time is variable.
Any idea? Thanks.

I'm assuming that you are only interested in doing this for variable step solvers. Let us assume that your sampling time is every two seconds, and your solver is ode23t, with the simulation running for ten seconds. Then, you expect to have the value of your variable be 5.
Now, the way you set up the model is in the screenshot below. Constant is your sampling time, Clock is the source for the time. You need a Rate Transition block to ensure a periodic output for a non-periodic clock input into it. I've set a sampel time of 1 for it.
Finally, in your code, you need a persistent variable. You could also use Simulink's Data Store Read and Write blocks, but this seems simpler to me.

Related

How to convert a continous time simulink model to discrete time model in Simulink?

For example, I have a controller consisting of integrator and sine web signal combined in the input. My question may be very naive to you, please help me to understand.
In the picture I have a frequency which is the input of an integrator then two cos and sine block. How to make each Simulink block discrete using ZOH and rate transition block in Simulink.
Two potential solutions
Change the sample time of the sinewave to something that is non-zero. This will make it a discrete signal
Use a Zero Order Hold (as you've mentioned) to convert the signal to discrete, you will need to specify a sample time for this block to perform the conversion. Setting the time to 0.5 will sample the signal every half second for example.

What is the best practice to enable mixed sample time in Simulink model

An outside Library (from PreScan) requests 200 Hz while my control plant model needs to run at 100 Hz. Therefore, my question is that how can I coordinate these two activities? My concern is that if I use 200Hz in Simulink, it may compromise my control plant’s fidelity.
Is it possible to set simulink time step as 1/100 while keep the outside library to run at 200Hz?
Simulink works perfectly happily with multi-rate models. The thing (it appears) that you don't understand is the difference between the overall model sample rate - i.e. the settings of your solver - and the sample rate of individual blocks within your model.
It's very typical to have some blocks in your model sampled at say 100Hz, while other parts of your model sampled at 200Hz. In this case you would choose a discrete solver and give it a sample time of 200Hz. The 200Hz blocks would get executed at every solver time step, while the 100Hz blocks would get executed every second solver time step.
You should look at the Sample Times in Systems section of the documentation.
You can use both explicit and implicit rate control in Simulink.
Sample Time
To set the fundamental sample time go to: Configuration Parameter>Solver>Fixed-step size. You can also use the Simulink API:
get_param(bdroot, 'FixedStep')
set_param(bdroot, 'FixedStep', '0.005') % 200Hz
Colors
To activate the Sample Time colors go to: Display>Sample Time>All. The Sample Time Legend will help you understanding how the implicit rate control works.
Sample time Option
You can control the tasking and sample time options via: Configuration Parameter>Solver>Tasking and sample time options.
At the beginning you can activate the automatic handling of rate transition for data transfer. Then you shall analyse what colors your model elements are and place the Rate-Transition blocks on the data signal lines between the model elements with different sample rates.
Now the rate control is implicit. If you use the function calls to explicitly call your subsystems at a required rate by involving a predefined scheduler, than the rate control is explicit.
You can open build in Simulink examples to see how it works:
sf_ladder_logic_scheduler
sf_loop_scheduler

Transfer function estimation

I am trying to find a transfer function of bldc motor speed over duty cycle percent. I made two measurements for different duty cycle percentages in order to both estimate transfer function and its validation.
For the first one (%65 duty cycle step input) I got below measurement and its transfer function estimation.
For the second one (%70 duty cycle step input) I got below measurement and it transfer function estimation.
The problem is that my transfer functions are not validating each other as shown below. They do not give the same response for the same input. Can anyone explain the reason?
It looks like the two measurements are very different. One has a maximum of 220, the other has a maximum of 350. This means the data acquisition is at fault, or the motor is itself variable.
Why don't you try measuring 20 times, and see if the raw data look similar?
Otherwise would need more info about your recording setup and the protocol for testing the duty cycles. It doesn't sound like a matlab or programming problem.
-- edit
Transfer functions are usually the output as a function of the input. Not functions of time.
Transfer function estimation assumes that the system is linear and time-invariant.
Most likely the system exhibits a nonlinear response characteristic which causes a very large change in output amplitude when input is increased from 65% to 70%, so a transfer function obtained at one operating point is not valid for the other.

Matlab: How does Simulink simulate model with block computation time much longer than clock time period?

I have a Simulink model with master clock value of 4410 Hz. I know for a fact that computation time of some algorithms (e.g. cubic spline interpolation on a 4410 sample frame being accumulated in real-time) is much longer than the master time period (i.e. computation time of spline is cca 0.7 seconds). I would expect Simulink to output frame elements AFTER initial 1 second + propagation time delay (like in hardware languages, e.g. VHDL), but it actually starts outputting the elements of the frame just after one seconds (which is the length of frame, 4410/4410 seconds). This wouldn't be a problem if my output values weren't unexpected/wrong.
How does Simulink build the simulation in this case? It would appear that it stops the simulation for larger computation times, then continues it afterwards.
A simulink simulation assumes infinite computation capacity, it does not simulate computation times. It does not stop the simulation, it does not use a real clock at all. While simulink is a bit more complicated with the different solvers, you can take a look at discrete event simulation which should give a simple example of isolating the simulation clock form your real clock.

How to set maximum calculation time in matlab (without activating Simulink)

I'm doing simulation in Matlab where some data from the simulation are obtained by executing another software. The idea is when the calculation time is beyond a limit the data from the simulation will not be accepted. How to set a maximum calculation time to automatically stop this unnecessary calculation? I don't use Simulink at the moment.
Thanks in advance!
You can try investigating the use of tic, toc commands if you are doing loop-styled calculations. e.g. at the very start of the calculation:
tic;
just before end of each loop:
if (toc>60) %//However many seconds you want
break;
end
This might not be useful if you don't have loop-style calculations you have direct access to.