% This file is used to compute the values of Kp and Ki for the current regulator.
% Place this file in the same folder than the Simulink file.


% PARAMETERS
fs = 20e3;                          % Sampling, interrupt and switching frequency [Hz]
Ts = 1/fs;                          % Sampling period [s]

Lg = 2.5e-3;                        % Grid inductance [H]
Rg = 22e-3;                         % Grid inductor resistance [Ohm]

C = 3*260e-6;                       % Capacitance of the DC bus [F]

Vdc = 130;                          % Inital DC bus voltage [V]
Vg = 40;                            % RMS grid voltage [V]
Vg_peak = Vg*sqrt(2);               % Peak grid voltage [V]
Ig_d0 = 6;                          % Initial current on d-axis [A]
Ig_q0 = 2;                          % Initial current on q-axis [A]

phi = atan(Ig_q0/Ig_d0);            % Current phase angle [rad]
  

Vga0 = Vg_peak*sin(0);              % Initial phase A grid voltage [V]
Vgb0 = Vg_peak*sin(-2*pi/3);        % Initial phase B grid voltage [V]
Vgc0 = Vg_peak*sin(2*pi/3);         % Initial phase C grid voltage [V]

Ig_peak = sqrt(Ig_d0^2+Ig_q0^2);    % Inital peak current

Iga0 = Ig_peak*cos(phi);            % Initial phase A grid current [A]
Igb0 = Ig_peak*cos(phi-2*pi/3);     % Initial phase B grid current [A]
Igc0 = Ig_peak*cos(phi+2*pi/3);     % Initial phase C grid current [A]


% BOOST CURRENT REGULATOR -------------------------------------------------

K1 = 1/Rg;
T1 = Lg/Rg;

Td = (1/2 + 1/2 + 1/3)*Ts;

Tn1 = T1;
Ti1 = 2*K1*Td;

Kp1 = Tn1/Ti1;
Ki1 = 1/Ti1;