model_sim.m 3.24 KB
Newer Older
Martin Vítek's avatar
Martin Vítek committed
1 2 3
%
% Nixie clock boost converter
%
4

Martin Vítek's avatar
Martin Vítek committed
5
clear
6

Martin Vítek's avatar
Martin Vítek committed
7 8 9
% Converter parameters
par.R62 = 0.2;
par.R63 = 4.7;
Martin Vítek's avatar
Martin Vítek committed
10
par.RQ24 = 380e-3;
Martin Vítek's avatar
Martin Vítek committed
11 12 13 14 15 16 17
par.RQ25 = 23e-3;
par.RL3 = 1.1;
par.Rload = 12e3;
par.RsD6 = 2;
par.L3 = 680e-6;
par.Cout = 1.41e-6;
par.Ud6 = 0.5;
18

Martin Vítek's avatar
Martin Vítek committed
19 20 21
par.Rin = par.R62 + par.RQ25 + par.RL3 + par.RsD6;
par.Rout = par.R63 + par.Rload;

22 23
Uin = 12;
f = 23437.5;
Martin Vítek's avatar
Martin Vítek committed
24
d = 0.98;
Martin Vítek's avatar
Martin Vítek committed
25
Ts = 1/f;
Martin Vítek's avatar
Martin Vítek committed
26

Martin Vítek's avatar
Martin Vítek committed
27 28
uc_req = 180;
uc_lin = 180;
Martin Vítek's avatar
Martin Vítek committed
29
il_lin = 0.83;
30 31


Martin Vítek's avatar
Martin Vítek committed
32
% Continuous time matrixes
Martin Vítek's avatar
Martin Vítek committed
33 34 35
cssm.A = [-par.Rin/par.L3, 0, 1/par.L3;
          0, -1/(par.Cout*par.Rout), 0;
          0, 0, 0];
36

Martin Vítek's avatar
Martin Vítek committed
37 38 39
cssm.B = [-uc_lin/par.L3;
          il_lin/par.Cout;
          0];
Martin Vítek's avatar
Martin Vítek committed
40

Martin Vítek's avatar
Martin Vítek committed
41
cssm.C = eye(size(cssm.B,1));
Martin Vítek's avatar
Martin Vítek committed
42
cssm.D = zeros(size(cssm.B));
43 44


Martin Vítek's avatar
Martin Vítek committed
45
% Discrete system
Martin Vítek's avatar
Martin Vítek committed
46 47 48
dssm = c2d(ss(cssm.A, cssm.B, cssm.C, cssm.D), Ts);

% Add error integration
Martin Vítek's avatar
Martin Vítek committed
49 50 51 52
dsys.A = [dssm.A(1,1), dssm.A(1,2), dssm.A(1,3), 0;
          dssm.A(2,1), dssm.A(2,2), dssm.A(2,3), 0;
          dssm.A(3,1), dssm.A(3,2), dssm.A(3,3), 0;
          0, Ts, 0, 1];
Martin Vítek's avatar
Martin Vítek committed
53 54 55 56
dsys.B = [dssm.B(1,1), 0;
          dssm.B(2,1), 0;
          dssm.B(3,1), 0;
          0, -Ts];
Martin Vítek's avatar
Martin Vítek committed
57
dsys.C = eye(size(dsys.B, 1));
Martin Vítek's avatar
Martin Vítek committed
58 59 60
dsys.D = zeros(size(dsys.B));


Martin Vítek's avatar
Martin Vítek committed
61
%Simulate system (open loop)
Martin Vítek's avatar
Martin Vítek committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
% i = 1;
% i0 = 0.75;
% u0 = Uin;
% delta0 = 0.9;
% tmax = 0.1;
% 
% sim.vars(:,1) = [i0, u0, Uin, delta0];
% sim.t(i) = 0;
%     
% for t=dssm.Ts:dssm.Ts:tmax
%     sim.vars(:, i+1) = dsys.A*sim.vars(:, i) + dsys.B*(1-d);
%     sim.t(i+1) = t;
% 
%     i = i+1;
% end
Martin Vítek's avatar
Martin Vítek committed
77 78

%Plot it
Martin Vítek's avatar
Martin Vítek committed
79
% plot_sim(sim, "Open loop response", 1);
80

Martin Vítek's avatar
Martin Vítek committed
81

Martin Vítek's avatar
Martin Vítek committed
82
% Regulator
Martin Vítek's avatar
Martin Vítek committed
83
% Q = diag([0, 0.5, 0, 0.5]);
Martin Vítek's avatar
Martin Vítek committed
84 85
% Q = eye(4);
Q = diag([100, 0.5, 1e-8, 10]);
Martin Vítek's avatar
Martin Vítek committed
86
R = 1;
Martin Vítek's avatar
Martin Vítek committed
87
N = zeros(size(Q, 1),1);
Martin Vítek's avatar
Martin Vítek committed
88 89 90 91 92 93
% L = dlqr(dsys.A, dsys.B, Q, R, N);
[Lfull,V,LL] = my_dlqr(dsys.A, dsys.B(:,1), Q, R, N, 0, 0);

Cw = [0,1,0,0];
% p = inv(0.99*eye(4) - (dsys.A - dsys.B*L));
% K = inv(-0.99*Cw * p * dsys.B);
Martin Vítek's avatar
Martin Vítek committed
94

Martin Vítek's avatar
Martin Vítek committed
95 96
% p = eye(4) - (dsys.A - dsys.B*L);
% K = inv(-Cw * (p\dsys.B));
Martin Vítek's avatar
Martin Vítek committed
97

Martin Vítek's avatar
Martin Vítek committed
98 99
L = Lfull(1:4);
K = Lfull(6);
Martin Vítek's avatar
Martin Vítek committed
100 101

% Simulate system (regulator)
Martin Vítek's avatar
Martin Vítek committed
102
i = 1;
Martin Vítek's avatar
Martin Vítek committed
103
i0 = 0.75;
Martin Vítek's avatar
Martin Vítek committed
104 105 106
u0 = 12;
delta0 = 0;
delta_max = 180;
Martin Vítek's avatar
Martin Vítek committed
107
d0 = 0.95;
Martin Vítek's avatar
Martin Vítek committed
108
tmax = 0.05;
Martin Vítek's avatar
Martin Vítek committed
109

Martin Vítek's avatar
Martin Vítek committed
110
sim_reg.vars(:,1) = [i0, u0, Uin, delta0];
Martin Vítek's avatar
Martin Vítek committed
111
sim_reg.t(i) = 0;
Martin Vítek's avatar
Martin Vítek committed
112
sim_reg.d(i) = 1-d0;
Martin Vítek's avatar
Martin Vítek committed
113
    
Martin Vítek's avatar
Martin Vítek committed
114
for t=Ts:Ts:tmax
Martin Vítek's avatar
Martin Vítek committed
115
    sim_reg.d(:,i+1) = -L*sim_reg.vars(:, i) - K*uc_req;
Martin Vítek's avatar
Martin Vítek committed
116 117 118 119 120 121 122 123

    if(sim_reg.d(1,i+1) > 0.99)
        sim_reg.d(1,i+1) = 0.99;
    end
    if(sim_reg.d(1,i+1) < 0)
        sim_reg.d(1,i+1) = 0;
    end
    
Martin Vítek's avatar
Martin Vítek committed
124
    sim_reg.vars(:, i+1) = dsys.A*sim_reg.vars(:, i) + dsys.B*[sim_reg.d(i+1); uc_req];
Martin Vítek's avatar
Martin Vítek committed
125
    
Martin Vítek's avatar
Martin Vítek committed
126 127
    if (abs(sim_reg.vars(4, i+1)) > delta_max)
        sim_reg.vars(4, i+1) = sign(sim_reg.vars(4, i+1)) * delta_max;
Martin Vítek's avatar
Martin Vítek committed
128 129
    end
    
Martin Vítek's avatar
Martin Vítek committed
130 131
    sim_reg.t(i+1) = t;

Martin Vítek's avatar
Martin Vítek committed
132
    i = i+1;
Martin Vítek's avatar
Martin Vítek committed
133
end
134

Martin Vítek's avatar
Martin Vítek committed
135
% Plot it
Martin Vítek's avatar
Martin Vítek committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149
%plot_sim(sim_reg, "Regulator", 2);

figure(3);
sgtitle(sprintf("LQ regulator"));
subplot(1,4,1);
plot(sim_reg.t, sim_reg.vars(1,:));
title("il");
    
subplot(1,4,2);
plot(sim_reg.t, sim_reg.vars(2,:));
title("uc");
    
subplot(1,4,3);
plot(sim_reg.t, sim_reg.vars(3,:));
Martin Vítek's avatar
Martin Vítek committed
150
title("Uin");
Martin Vítek's avatar
Martin Vítek committed
151 152

subplot(1,4,4);
Martin Vítek's avatar
Martin Vítek committed
153 154
plot(sim_reg.t, sim_reg.vars(4,:));
title("delta");
Martin Vítek's avatar
Martin Vítek committed
155 156 157



Martin Vítek's avatar
Martin Vítek committed
158 159
function plot_sim(sim, what, num)
    figure(num);
Martin Vítek's avatar
Martin Vítek committed
160
    sgtitle(sprintf("%s", what));
Martin Vítek's avatar
Martin Vítek committed
161
    subplot(1,4,1);
Martin Vítek's avatar
Martin Vítek committed
162
    plot(sim.t, sim.vars(1,:));
Martin Vítek's avatar
Martin Vítek committed
163 164
    title("il");
    
Martin Vítek's avatar
Martin Vítek committed
165
    subplot(1,4,2);
Martin Vítek's avatar
Martin Vítek committed
166
    plot(sim.t, sim.vars(2,:));
Martin Vítek's avatar
Martin Vítek committed
167 168
    title("uc");
    
Martin Vítek's avatar
Martin Vítek committed
169
    subplot(1,4,3);
Martin Vítek's avatar
Martin Vítek committed
170
    plot(sim.t, sim.vars(3,:));
Martin Vítek's avatar
Martin Vítek committed
171 172 173 174
    title("Uin");
    
    subplot(1,4,4);
    plot(sim.t, sim.vars(4,:));
Martin Vítek's avatar
Martin Vítek committed
175
    title("delta");
Martin Vítek's avatar
Martin Vítek committed
176 177
    hold off
end