# Global Variable in Simulink

51 views (last 30 days)

Show older comments

##### 1 Comment

amr makhlouf
on 23 May 2022

### Answers (4)

Walter Roberson
on 19 Feb 2022

##### 10 Comments

Walter Roberson
on 27 Feb 2022

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:

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

amr makhlouf
on 23 May 2022

##### 0 Comments

amr makhlouf
on 23 May 2022

##### 3 Comments

Walter Roberson
on 26 May 2022

amr makhlouf
on 27 May 2022

##### 7 Comments

Walter Roberson
on 28 May 2022

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.

### Communities

More Answers in the Power Electronics Control

### See Also

### Community Treasure Hunt

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

Start Hunting!