Global Variable in Simulink

Has anyone deal with global variables before? becouse I have a project and I can't run it . Matlab tells me you have to convert it to a Signal.Object in the data Memory storage!

1 Comment

The parameter shall be intialized the initial functions in model properties right. Sorry for naive questions i am new to matlab am trying to earn it deeply thanks

Sign in to comment.

Answers (4)

Walter Roberson
Walter Roberson on 19 Feb 2022

10 Comments

Sorry I have followed these steps before but I couldn't fix it !!
Unfortunately saying that you couldn't fix it does not give me enough information to go on to know what symptoms you are seeing, or what your model is.
I have been successful in implementing globals using the instructions there, so I know it can be done.
OK, Sorry for that . I have to prepare predictive torque algorthim for Induction Machine so when I have to define my Parameters as global for "Equ. Circuits " of Induction Machine such as Ls (Stator Inductor) or Ts( sampling time) ...etc but when I followed those steps thats made more errors in my simulink .
That gives me more context, but does not tell me what symptoms you are seeing, and does not include a model that I could run to test with.
Sure sir, you may.
I have saved my Parameters as Function inside Simulink . Thanks in advanced
I was about to point out some problems line by line, but unfortunately I do not save a copy of the original model, and you moved your copy to your "Bin" folder . So I will only be able to approximate.
if isempty(Fs), Fs = 0 + 1j*0; end
That initializes Fs to a scalar complex double
Fs = Fs + Ts*(v(x_opt) - Rs*i_meas);
v is a complex double vector but is being indexed with a scalar, so the v() is a complex double scalar.
Rs is a real double scalar.
Ts is a real double scalar.
i_meas is an input. It is fed in from Stator Current on the Induction Motor subsystem.
Inside Induction Motor, the Stator Current signal is fed from a short series of calculation blocks such as gain and integrator. The source of those signals being calculated against is the signal named us from the left hand side.
The signal named us is created at the top left corner of the Induction Motor subsystem, as a Real+Imaginary-To-Complex being fed from a demux block. The demux block is being fed from input port 1, "Voltage Source"
Back in the main model, Voltage Source comes from an "abc to alpha-beta-zero" block. Based on the name, we could suspect that the output will have 3 elements (alpha, beta, zero).
The input to the conversion block is the unnamed subsystem. If you open the subsystem it is clear at the right hand side that there is a mux of 3 signals. So the unnamed subsystem is emitting a vector of length 3, and that is being fed into the abc conversion block, giving a vector of length 3 on output, which becomes the Voltage Source signal that is fed into the Induction motor subsystem. There, the input of length 3 is converted through the R+I to Complex block into a 2 x 1 complex signal. Not to a scalar signal.
That 2 x 1 complex signal then flows through the calculations in the Induction Motor subsystem, becoming a 2 x 1 vector Stator Current, which then becomes i_meas on input to the Function Block.
So... on the line indicated above,
Fs = Fs + Ts*(v(x_opt) - Rs*i_meas);
i_meas is 2 x 1 and the other items are scalars... so the output Fs has to be 2 x 1.
But for it to be 2 x 1, it has to be initialized as 2 x 1 or you have a size conflict.
Now with it being 2 x 1, several other variables become 2 x 1.
Then you get to
Isp1 = (1+Ts/t_sigma)*i_meas+Ts/(t_sigma+Ts)*...
(1/r_sigma*((kr/tr-kr*1i*wm)*Fr+v_o1));
Here, Fr has to be 2 x 1 because the 2 x 1 Fs is used to create it.
Here, wm is an input to the MATLAB Function block. wm is fed from the Rotor Speed output of the Induction Motor subsystem. And when you look inside the Induction Motor subsystem at the bottom right and chase the signals across... you find that Rotor Speed also needs to be 2 x 1 because it too is created using us from the top of the subsystem that we showed above has to be 2 x 1 after the Real+Imaginary to Complex merge.
So... we have a term (kr/tr-kr*1i*wm) that has to be 2 x 1 and we are using the * operator with Fr which has to be 2 x 1. And that is not a valid matrix multiplication.
Note: I have not completely solved the problems with global variables yet. I decided to substitute local initializations into the MATLAB Function for the moment to get the rest of everything working and then go back to try to fix the global variables. But the logic of the MATLAB Function turns out not to work.
Perhaps the Rotor Speed and Stator Current are intended to be scalars instead of 2 x 1... but they are 2 x 1 at present.
Perhaps the demux in the Induction Motor block needs to have another port added so you can split the signal into 3 pieces instead of 2 pieces, and then put a Sink on the third piece.
I can fix it without global variables, but I wanted to know why it didn’t work. My goal is to learn, not the model itself, but this is the first time that I deal with global parameters, so I wanted to know how I can overcome these consequences and what causes them..! Thank you Sir very much for your help
Sorry, I am still working on that matter. I thought I had a solution, but it does not work in practice.
In some of the cases, the variables you are initializing need to be converted to Simulink.Parameter objects including some that you declared as global . I do not think you can use a global variable to initialize a constant, such as in places you have gain blocks which have a ratio based on your variables.
But creating Parameter objects is the easy part that I managed to solve; I haven't figured out yet how to get the globals right.
I am very thankful to you. I hope if you were able to solve the problem that you would send it to me here with its reason and how I can overcome it in the next time.
I think I got it!
First off, note that the below all does not work -- but the reason it does not work is the signal size problems that I discussed in https://www.mathworks.com/matlabcentral/answers/1653710-global-variable-in-simulink#comment_2004255 -- the global problems seem to have been solved.
Your model needs to initialize several different values. You use the values in two different ways:
  1. Some of the values you use to initialize parameters for blocks, such as setting a gain. You need to have your MATLAB code create these as Simulink.Parameter objects. Do not declare them as global. In places where you need to use these parameters in your MATLAB code, use the parameters to initialize a constant block and use the constant block as a parameter to the MATLAB Function block (using a port.)
  2. Some of the values you never use as parameters to blocks. In this example, you never modify any of the values, so you could create them as Parameter objects like the above. Or you can have your MATLAB Function Block declare them as globals . Then in your code (shown below) that is initializing the workspace before calling sim(), create them as Simulink.Signal objects. Do not declare them as global at that level. That was my mistake before, and it took me a fair while to figure that out.
  3. Each global in the MATLAB Function Block nees to be listed in the Blocks and Port Manager for the block. When you are first creating the block, it might be easiest to put a breakpoint in the MATLAB code at the point it calls sim(), run to there, then go into the Blocks and Port Manager and make sure the signal is there for the MATLAB Function Block.
  4. In theory instead of having had to run the MATLAB code to just before sim(), you can Explore the model. Find the model in the hierarchy, and go down to its External Data section. Then use the Add button to create a new Simulink.Signals object (^S I think the shortcut is). Make sure that the "type" for it is set to DataStore Memory. You should now be able to see it in the MATLAB Function Block's Blocks and Ports Manager
  5. When you create a Simulink.Signal object at the MATLAB level, then to initialize it, set its InitialValue property, and the value for that should be a character vector. In the RSS (Real Simulink Signal) function below, notice the mat2str() around the passed-in numeric value; likewise for the CSS (Complex Simulink Signal) function.
  6. When you create a Simulink.Parameter object at the MATLAB Level, then to initialize it, set is Value property, and the value for that should be numeric (or as appropriate) directly, not text. See the RSP (Real Simulink Parameter) function below.
% Variables required by the control algorithm
% Sampling time of the predictive algorithm [s]
Ts_value = 4e-5;
% PI speed controller parameters
Tsw = 0.002; % Sampling time of the PI controller [s]
Kp = 3.016; % Proportional gain
Ki = 0.141; % Integrative gain
% Machine parameters
J = 0.062; % Moment of inertia [kg m^2]
p_value = 1; % Pole pairs
Lm_value = 170e-3; % Magnetizing inductance [H]
Ls_value = 175e-3; % Stator inductance [H]
Lr_value = 175e-3; % Rotor inductance [H]
Rs_value = 1.2; % Stator resistance [Ohm]
Rr = 1; % Rotor resistance [Ohm]
sf_nom_value = 0.71; % Nominal stator flux [Wb]
T_nom = 20; % Nominal torque [Nm]
% DC-link voltage [V]
Vdc = 520;
% Auxiliary constants
ts = Ls_value/Rs_value;
tr_value = Lr_value/Rr;
sigma = 1-(((Lm_value)^2)/(Lr_value*Ls_value));
kr_value = Lm_value/Lr_value;
r_sigma_value = Rs_value+kr_value^2*Rr;
t_sigma_value = sigma*Ls_value/r_sigma_value;
% Weighting factor for the cost function of PTC
lambda_value = T_nom/sf_nom_value;
% Voltage vectors
v0 = 0;
v1 = 2/3*Vdc;
v2 = 1/3*Vdc + 1j*sqrt(3)/3*Vdc;
v3 = -1/3*Vdc + 1j*sqrt(3)/3*Vdc;
v4 = -2/3*Vdc;
v5 = -1/3*Vdc - 1j*sqrt(3)/3*Vdc;
v6 = 1/3*Vdc - 1j*sqrt(3)/3*Vdc;
v7 = 0;
v_value = [v0 v1 v2 v3 v4 v5 v6 v7];
% Switching states
states_value = [0 0 0;1 0 0;1 1 0;0 1 0;0 1 1;0 0 1;1 0 1;1 1 1];
Lr = RSS(Lr_value); % Rotor inductance [H]
Ls = RSS(Ls_value); % Stator inductance [H]
Ts = RSS(Ts_value); % Sampling time [s]
lambda = RSS(lambda_value); % Weighting factor for the cost function of PTC
Rs = RSS(Rs_value); % Stator resistance [Ohm]
states = RSS(states_value);
v = CSS(v_value);
kr = RSP(kr_value);
Lm = RSP(Lm_value); % Magnetizing inductance [H]
p = RSP(p_value); %Pole pairs
r_sigma = RSP(r_sigma_value);
sf_nom = RSP(sf_nom_value); % Nominal stator flux [Wb]
t_sigma = RSP(t_sigma_value);
tr = RSP(tr_value);
result = sim('MPC_DTC_IM');
disp(result)
function SS = RSS(value)
SS = Simulink.Signal;
SS.DataType = 'double';
SS.Complexity = 'real';
SS.InitialValue = mat2str(value);
end
function SP = RSP(value)
SP = Simulink.Parameter;
SP.Value = value;
end
function SS = CSS(value)
SS = Simulink.Signal;
SS.DataType = 'double';
SS.Complexity = 'complex';
SS.InitialValue = mat2str(value);
end
Together with
function [sa,sb,sc] = control(T_ref,sflux_ref,wm,i_meas,kr,Lm,p,r_sigma,t_sigma,tr)
%#codegen
% Variables defined in the parameters file
global lambda states
global Lr Ls Rs Ts v
persistent x_opt Fs
if isempty(x_opt), x_opt = 1; end
if isempty(Fs), Fs = complex([0;0],[0;0]); end
%initialize variable to set types
sa = 0; sb = 0; sc = 0;
Fr = 0;
Fsp1 = complex(0,0);
g = 0;
Isp1 = complex(0,0);
Tp1 = complex(0,0);
v_o1 = complex(0,0);
%Stator flux estimate
Fs = Fs + Ts*(v(x_opt) - Rs*i_meas);
% Rotor flux estimate
Fr = Lr/Lm*Fs+i_meas*(Lm-Lr*Ls/Lm);
g_opt = 1e10;
for i = 1:8
% i-th voltage vector for current prediction
v_o1 = v(i);
% Stator flux prediction at instant k+1
Fsp1 = Fs + Ts*v_o1 - Rs*Ts*i_meas;
% Stator current prediction at instant k+1
Isp1 = (1+Ts/t_sigma)*i_meas+Ts/(t_sigma+Ts)*...
(1/r_sigma*((kr/tr-kr*1i*wm)*Fr+v_o1));
% Torque prediction at instant k+1
Tp1 = 3/2*p*imag(conj(Fsp1)*Isp1);
% Cost function
g = abs(T_ref - Tp1)+ lambda*abs(sflux_ref-abs(Fsp1));
if (g<g_opt)
g_opt = g;
x_opt = i;
end
end
%**************************************
% Optimization
[~, x_opt] = min(g);
% Output switching states
sa = 0; sb = 0; sc = 0; %to fix sizes
sa = states(x_opt,1);
sb = states(x_opt,2);
sc = states(x_opt,3);

Sign in to comment.

amr makhlouf
amr makhlouf on 23 May 2022
@osama could you please share with me the answer for this problem cause i received the same global thing error ??
amr makhlouf
amr makhlouf on 23 May 2022
Am bit confused how did you include control(kr,Lm,p,r_sigma,t_sigma,tr) in the function i mean they need to receive a signal from the induction motor model which is not applicable here so how do we implement this here

3 Comments

control() was a function in the model that was in the model posted in Google Drive at https://www.mathworks.com/matlabcentral/answers/1653710-global-variable-in-simulink#comment_2000730
Unfortunately the user moved the original to their trash folder so it cannot be retrieved (unless they put it back). I have my modified version of it but not the original.
I have figured out how to declare and use the global variables thanks to you . But could you please tell me what are varibles need to be create in ports and data manager so that i don't receive any error like this one : one or more output arguments not assigned during call to varargout
port and data manager cannot fix that problem. Your logic is failing to assign to a variable that is used for output.

Sign in to comment.

amr makhlouf
amr makhlouf on 27 May 2022
Okay thanks alot .. another question please the first part of the code before switching states do i have it to be write in the "InitFcn in call backs or to be just written in m file of the code ?

7 Comments

what i mean this part of the code which you called simulink paramter object
Lr = RSS(Lr_value); % Rotor inductance [H]
Ls = RSS(Ls_value); % Stator inductance [H]
Ts = RSS(Ts_value); % Sampling time [s]
lambda = RSS(lambda_value); % Weighting factor for the cost function of PTC
Rs = RSS(Rs_value); % Stator resistance [Ohm]
states = RSS(states_value);
v = CSS(v_value);
kr = RSP(kr_value);
Lm = RSP(Lm_value); % Magnetizing inductance [H]
p = RSP(p_value); %Pole pairs
r_sigma = RSP(r_sigma_value);
sf_nom = RSP(sf_nom_value); % Nominal stator flux [Wb]
t_sigma = RSP(t_sigma_value);
tr = RSP(tr_value);
result = sim('MPC_DTC_IM');
disp(result)
function SS = RSS(value)
SS = Simulink.Signal;
SS.DataType = 'double';
SS.Complexity = 'real';
SS.InitialValue = mat2str(value);
end
function SP = RSP(value)
SP = Simulink.Parameter;
SP.Value = value;
end
function SS = CSS(value)
SS = Simulink.Signal;
SS.DataType = 'double';
SS.Complexity = 'complex';
SS.InitialValue = mat2str(value);
end
where it should be written in m file or in initial or preload function
In the m file.
Do not Run the model inside Simulink. The .m file I provided sets up everything and runs the model.
okay thanks , i recieve this error The input arguments of the "rss" command must be nonnegative integers.
can you please send the lst two m files in r 2016a version
and could you please send me documentation about
1- Rss , Rsp , Css and sim function meaning
RSS is a helper function I wrote that creates a Real Simulink Signal.
RSP is a helper function I wrote that creates a Real Simulink Parameter
CSS is a helper function I wrote that creates a Complex Simulink Signal.
sim() is the fundamental Simulink command that starts a Simulink model executing. The graphical interface you get when you use open_system() or
simulink
are graphic interfaces that eventually internally invoke sim() when it is time to run the model.

Sign in to comment.

Communities

More Answers in the  Power Electronics Control

Categories

Find more on Simulink in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 19 Feb 2022

Commented:

on 28 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!