Control modelica model in dymola, using matlab and command importInitial(dsName="dsfinal.txt") - matlab

I want to control from matlab a modelica model implemented in Dymola. At each x-seconds, matlab reads the states values and it calculates the new parameters values of the model and calls dymola to simulate the model with these new parameters values.
I try to initialize the states using the final values of the previous simulation using importInitial(dsName="dsfinal"); and then simulating.
This works if I give the command straight in the simulation log but it doesn't work when I call it from matlab, eventhought exactly the same command appear in the dymola simulation log.
Could any one help?
Thx!

The problem was due to the change of the parameters values.
Using following code solved it:
1) use simulateModel() with all simulation parameters you want for the first simulation
2) use importInitial('dsfinal.txt') to import the final state values
3) use modelName.parameterName = newValue to change parameter value
4) use simulate() to simulate further with the same settings as first simulation.
See also: http://www.claytex.com/how-to-restart-a-simulation/

Related

How to call simulink model(.slx) from script

I'm a super beginner in Simulink models and control systems.
I have .slx Simulink model for drone dynamics system.
It takes in two inputs (roll cmd, pitch cmd) and outputs velocity x, velocity y, position x, and position y.
From here, it seems like I can open the system by calling
open_system('myModel.slx', 'loadable');
But how do I put inputs and get output values?
Is there a way I can do this in a gui?
EDIT:
Here is the full layout of my model:
When I did
roll_CMD=10;
pitch_CMD=20;
I got a warning saying:
Input port 1 of 'SimpleDroneDynamics/...' is not connected.
How do I feed inputs using port numbers?
How do I get outputs with port numbers? I tried
[vx, vy, px, py] = sim('SimpleDroneDynamics.slx');
and got an error saying
Number of left-hand side argument doesn't match block diagram...
Is there a way to continuously feed inputs at every time step? This being controller module, I think I'm supposed to feed in different values based on output position and velocity.
EDIT2:
I'm using Matlab2017a
About the first two points of your question:
In simulink:
For the inputs you can use a constant block and when you double click the input block you can assign a value, which can be a workspace variable.
To get the outputs to your workspace you can use the simout block (make sure to put Save format to array).
Connect inputs to your simulink model
Connect outputs of your simulink model to the simout blocks.
MATLAB script
clc;
clear all;
roll = 10;
pitch = 20;
sim('/path_to_simulinkmodel.slx')
time = simout(:,1);
velocity_X = simout(:,2);
velocity_Y = simout(:,3);
position_X = simout(:,4);
position_Y = simout(:,5);
About the third point of your question
You can define the duration of your simulation in the block diagram editor. You can put a variable which is defined in the calling script. There are multiple ways of achieving time dependent input variables:
One option I personally don't recommend is using a for-loop and calling the simulink model with a different value of roll and pitch
for i = 1:numberOfTimesteps
roll = ...
...
sim('simulinkModel.slx')
end
A second and more efficient approach is changing the constant blocks to other source blocks like ramp signals or sinusoid signals
First of all Simulink model use main Matlab workspace. So you can change your variables values at command window (or just at your script) and run Simulink model.
There are several ways to initialize this constants for Simulink. One more useful way is to create script containing all your variables and load it at Simulink model starts. You can do it by adding script name in Simulink/Model Explorer/Callbacks. (There are different callbacks - on Loading, on Starting and etc.). Read more about this: here.
Now you can run your simulation using sim function:
sim('name_of_model')
name_of_model must contain path if model is not in the active MATLAB folder (active folder you can see in your matlab window just under the main menu).
There are different properties of sim function, read about them in help this can be useful for you. By the way: you can change some parameters of your model using sim. You even can find any block in your model and change it's properties. Read more about sim and about finding current blocks. Interesting that the last solution give you ability to change parameters during the simulation!
About getting output. After you run simulation you get tout variable in main workspace. It is an array of timesteps. But if you add outport block (like at my image) you also get another variable in workspace yout. yout is an Datasets. It contain all your outports values. For 2 outports for example:
yout
yout =
Simulink.SimulationData.Dataset
Package: Simulink.SimulationData
Characteristics:
Name: 'yout'
Total Elements: 2
Elements:
1 : ''
2 : ''
Get the values of any of outports:
yout.get(1).Values
it is a timeseries data type, so:
yout.get(1).Values.Time - give you times
yout.get(2).Values.Data - give you values of this outport at each time
We have one more method to take output values:
[t,x,y] = sim('model_name')
it returns double arrays. t- time array, y - matrix of all outports values (it already double and contain only values without times, but for each simulation time!)
So now you can create common Matlab GUI and work at this variables! There is no any difficulties. You can read more about GUI for Simulink here.

Interfacing Simulink with MATLAB

Designing a model which is doing some comparison on data ,fetched from GUI.
I have ".m file" which has GUI functionality based on GUI GUIDE.I want to run my .m file (Inside I simulate my model also once data has been read.) I am using one Push Button on GUI.After pushing that button my model starts simulating as per code mentioned below.
h=str2num(get(handles.edit_h,'String'));
l=str2num(get(handles.edit_l,'String'));
options = simset('SrcWorkspace','current');
sim('level_monitor',[],options);
My model gives output as constant values(like 1,2,3,4).I dont want to plot graph on scope but want to use this constant variables in GUI for setting string message on UI.How to access value coming to output port of simulink via GUI function(m-script).
As per my understanding MATLAB code uses its own workspace and Simulink has its own workspace.(Base and model workspace). How to read data available at Simulink outport to my matlab code(GUI .m file)? I have tried using "Simout(To Workspace block) also but it not resolve my issue.
Kindly help me out with this.
You should be using the form of the sim function that returns an output structure, i.e.
simOut = sim('level_monitor',[],options);
simOut is then a structure that contains fields for all of the variables that the simulation would nominally write to the Base Workspace.
See
>> doc sim
for more information.

Setting integrator initial condition basing on the signal in Simulink

I have simple Simulink model and I would like to change the initial condition of integrator based on some signal. This signal can take values 1 or 0 and initial conditions of integrator should be equal to 1.16 or 0.65 respectively.
I tried to set a parameter x_init in Model Workspace (and then use it in Integrator block), but I couldn't access it via function. Then I tried to run MATLAB function inside simulink model with set_param(...), but I got error:
Function 'set_param' is not supported for code generation. Consider adding coder.extrinsic('set_param') at the top of the function to bypass code generation.
This is how the structure of model looks like in Model Explorer. I would like to change the x initial condition.
Using a workspace variable as you are doing is the wrong approach.
Change the Initial Condition Source property of the integrator to external. This will give the block an additional in port. The value of the signal fed into this port when the integrator is reset is taken as the initial condition.

Is it possible to use to set a parameter in Simulink without running a Matlab script?

When a Simulink model contains a parameter, its value can be set using a script.
For instance, a constant block could have the value a, and the script would then contain the expression a=2 to set its value.
Is it possible to set the parameter inside the Simulink model, without running a Matlab script?
For instance, the Simulink model could containa drop-down menu that alows you to set the value of the parameter.
Possible solutions include
Making your own GUI (see GUIDE)
Using masked subsystems
Using Simulink Dashboard blocks

Getting the solver type and step size (for fixed step solvers)

we are trying to integrate a simulation model into Simulink as a block. We have a custom continuous block which loads an m file that contains the functions Derivatives, Outputs etc.
My question is: is there a way to find out which solver is used currently and with which parameters? Our model won't be able to support variable time solvers and I would like to give a warning. Similarly, the model requires the fixed step time for initialization.
Thanks in advance.
You can get the current solver name using
get_param('modelName', 'SolverName');
Some of the other common solver parameters are
AbsTol
FixedStep
InitialStep
ZcThreshold
ExtrapolationOrder
MaxStep
MinStep
RelTol
SolverMode
You can find other parameters you may wish to query by opening the .mdl file in your favorite text editor and digging through it.
If I'm understanding your use case correctly, you are trying to determine the type of solver (and other solver params) for the top-level simulink system containing your block.
I think the following should give you what you want:
get_param(bdroot, 'SolverType'); % //Returns 'Variable-step' or 'Fixed-step'
get_param(bdroot, 'FixedStep'); % //Returns the fixed step size
Notice that for purposes of generality/reusability, this uses bdroot to identify the top-level system (rather than explicitly specifying the name of this system).
If you want to find out more about other model parameters that you can get/set, I would check out this doc.
Additionally, I'm interested to know why it is that your model doesn't support a variable-step solver?