Run a continuous Simulink model with real-time input? - matlab

I'm new to Matlab/Simulink but have a requirement to interface with a Simulink model from a piece of software.
At the moment I have created a simple Simulink function 'Inc' which has 1 input to an addition block with a constant 1 and an output. I have used To/From Workspace blocks and can run the simulation over a fixed time using a time/value vector input, such as simin = [0,0;5,0;5,1;10,1].
What I would like to do is run my model continuously? Reading Workspace values in real-time rather than a pre-defined time based vector?
I can't see how to set this up? Can Matlab/Simulink do this?
To summarise, I would like simin and simout to be single values i.e. simin = 1, then with the model running continuously(infinitely) at the next fixed step simout would update to simout = 2. simin changes would be made at the Workspace at varying intervals.

The short answer is you can't (easily) do that, that's not how Simulink works. The MATLAB workspace is read/accessed at the beginning of the simulation and passed to the Simulink engine, and even if the values in the workspace change before the simulation is finished, this is not taken into account by the Simulink model running.
There are ways to work around this, but if you are a novice to MATLAB & Simulink, be aware that these are fairly advanced techniques, and I would advise to familiarise yourself with Simulink first.
Have a look at these similar questions for suggestions of how to do what you want:
Stream data form MATLAB to Simulink
Problem of variable updating in workspace
How Do I Change a Block Parameter Based on the Output of Another Block?
Tuning block parameters at every time step in a simulation
Simulink Signal Viewing using Event Listeners and a MATLAB UI
Obviously, you need to change the simulation end time to Inf or some large number.

Related

Set initial values in simulink idmodel block for an identified process model

I used system identification tool to obtain a state space (order 2) and a process model (2 inputs, 1 output, 2nd order transfer function + delay each) of some data. The models show very nice fit to experimental data in the system identification model output window [Figure 1] but when I use the idmodel block in simulink to simulate the same data it does not look at all like it was on the tool.
I have used exactly the same block diagram with both fitted models. With the state-space fitted model the results are coherent with the system identification tool [Figures 2-3], meanwhile the process model it's totally different [Figures 4-5].
The state-space model only works well with certain experimental conditions, while the process model gives a good fit for all my experiments, that's the reason why I try to use it. The pictures below correspond to only 1 experiment.
I think the problem resides in that I can't set initial conditions to the idmodel block when using a process model instead of a state space model. I can get the initial conditions for the process model using findstates(model, data), but I don't know how to apply them. Any hints on how to set initial conditions for identified transfer function/process models in simulink? Maybe a possible workaround without simulink? I'm open to any solution or ideas.
Thank you.
Figure 1. System identification tool output for both state-space and process model
Figure 2. Simulink output of state-space model
Figure 3. idmodel block with state-space model: initial conditions parameter available
Figure 4. idmodel block with process model: initial conditions parameter NOT available
Figure 5. Simulink output of state-space model
After reaching matlab central and pointing me in the right direction I came up with a solution. Thanks to Rajiv Singh.
I first needed to convert the process model from idproc to idss using idss() instead of ss() -See this article-, then use compare() instead of findstates() to obtain the initial conditions and feed the initial conditions to the idmodel block in simulink. Graphically:
model=idss(T3s_2d);
[y,fit,x0]=compare(run_data_s{8}, model);
%T3s_2d is the identified process model (idproc) from system identification toolbox
%run_data_s is the iddata object with the experimental runs

How to take values generated from a MATLAB program and display them in a Simulink block?

I have a working MATLAB program hooked up to an Arduino and rotary sensor that displays the current angle. I would like to display this angle on a Simulink model, so I can control a motor based off the current angle. Is there any way to do this without creating an angle sensor in Simulink and just using my MATLAB code that already works?
Your question is not really clear and it depends if you are trying to do it before simulation or during simulation.
If you want to do it before the simulation, just create a constant block with a variable name that is taken from the workspace, and let your function to set that variable in the workspace.
If you want to do it during simulation, it is a little more difficult but still possible. Lets create a sample a MVCE. The simulink model mask.slx contains a constant the is set to 0, and this constant value is displayed in the display block on the right. The simulation time is set to inf, so when you play it, you must stop it manually.
It is possible to checge the value of the constant while the simulation is running using this simple Matlab call:
set_param('mask/Value_to_set', 'Value', '10')
you should also consider that the constanta must be a Tunable gain (it is by default).
(as you can see the simulation is running).
There are some additional (and surely better) solution you can use:
Include your Matlab function in a MATLAB User Defined Function block, and call it at each simulation iteration. If you have compilation issue you shall follow the coder.extrinsic way (here an example for fmincon)
Use the Simulink Support Package for Arduino Uno Hardware Add-on that is available in the Add on store.

Using System Identification Toolbox transfer function with Simulink

I believe I am doing something fundamentally wrong when trying to import and test a transfer function in Simulink which was created within the System Identification Toolbox (SIT).
To give a simple example of what I am doing.
I have an input which is an offset sinusoidal wave from 12 seconds to 25 seconds with an amplitude of 1 and a frequency of 1.5rad/s which gives a measured output.
I have used SIT to create a simple 2 pole 1 zero transfer function which gives the following agreement:
I have then tried to import this transfer function into Simulink for investigation in the following configuration which has a sinusoidal input of frequency 1.5rad/s and a starting t=12. The LTI system block refers to the transfer function variable within the workspace:
When I run this simulation for 13 seconds the input to the block is as expected but the post transfer function signal shows little agreement with what would be expected and is an order of magnitude out.
pre:
post:
Could someone give any insight into where I am going wrong and why the output from the tf in simulink shows little resemblance to the model output displayed in the SIT. I have a basic grasp of control theory but I am struggling to make sense of this.
This could be due to different initial conditions used in SimuLink and the SI Toolbox, the latter should estimate initial conditions with the model, while Simulink does nothing special with initial conditions unless you specify them yourself.
To me it seems that your original signals are in periodic regime, since your output looks almost like a sine wave as well. In periodic regime, initial conditions have little effect. You can verify my assumption by simulating your model for a longer amount of time: if at the end, your signal reaches the right amplitude and phase lag as in your data, you will know that the initial conditions were wrong.
In any case, you can get the estimated initial state from the toolbox, I think using the InitialState property of the resulting object.
Another thing that might go wrong, is the time discretization that you use in Simulink in case you estimated a continuous time model (one in the Laplace variable s, not in z or q).
edit: In that case I would recommend you check what Simulink uses to discretize your CT model, by using c2d in MATLAB and a setup like the one below in Simulink. In MATLAB you can also "simulate" the response to a CT model using lsim, where you have to specify a discretization method.
This set-up allows you to load in a CT model and a discretized variant (in this case a state-space representation). By comparing the signals, you can see whether the discretization method you use is the same one that SimuLink uses (this depends on the integration method you set in the settings).

How do I integrate an MPC, PID and System models together to simulate on Matlab

I have a system with a Model Predictive Controller and PID Controller.
Assuming I have models for each controller and can express them in discrete time, please how do I integrate them together to simulate properties of the system in matlab?
Thanks
... continuing from the comments.
This is what Simulink is made for. Of course there are ways to do it without Simulink, but often you still use Simulink tools and functions just without the graphical Interface.
I assume you have your transfer functions "on paper". So you need the tf function to define your system model.
G = tf(num,den)
num and den are the coefficient vectors of your transfer function of numerator and denumerator. In Simulink you use the Transfer Fcn block and you define it with
G.num{1} %Numerator coefficients
G.den{1} %Denumerator coefficients
Your PID-controller cannot be defined using this block, as Simulink requires a higher or equal order for the denumerator. Instead use the PID controller Block. You need to calculate the Proportional, Integral and Differential gain before.
Then read the documentation about the MPC toolbox - I'm not familiar with it and can't help you on that - it is explained how you can create an mpc object regarding all your constraints (see your other question).
Then you have various options to transform your mpc object into something Simulink can deal with. I'd recommend the ss - the state space model - which can be implemented using the state space block. There is also a MPC Controller block, I don't have the toolbox - but you'll be able to find out how it can be used.
Finally you find source blocks, like a step to generate a test signal. And there are Sinks, in the easiest case scope to display your results. You can also save them to workspace or whatever...

Plotting in Matlab using Real Time data from Simulink

I'm trying to solve a problem of simulating in real time in Simulink (This is solved) but plotting (real time) in Matlab ?
Details:
I want to be able to run a Simulink simulation (which is running in real time) and be able to turn on / off manual switches while the simulation is happening. This works well when I'm using the built in Scopes in Simulink but now I want to export that data to Matlab in real time as well (To make a custom looking graph).
So is there a way, to export this data (it can be sampled if that is necessary) to Matlab and make a plot that is constantly updating. Meanwhile I can still manipulate the switches in Simulink and influence the simulation manually ?
Simulink is effectively running continuously until I stop it.
Thanks for the help!
There should be some kind of notification going when simulink updates the data to be visualized. Maybe this is the linkdata feature.
Another, worse, solution is the drawnow command to redraw the graphs continously (the latter could be unnescessary costly for you program).