%%% PLL for grid synchronization: simulation parameters %%%
% variant with blanks
% PH 2025

% Nominal grid parameters
grid_freq0 = 50.0; % nominal grid frequency (Hz)
grid_w0 = 2*pi*grid_freq0; % nominal grid frequency (rad/s)
T_sim = 5/grid_freq0; % simulation duration (s)

% True grid parameters
grid_freq = 51.0; % nominal grid frequency (Hz)
grid_w = 2*pi*grid_freq; % nominal grid frequency (rad/s)

Vgpk = 21.5; % Assumed grid voltage amplitude (V) (15.2 Vrms)

pll_deltaw_bound = 0.2 * grid_w0; % +/- bound on PLL frequency deviation: 0.2 F0 = 10 Hz

% Control parameters
rise_time_pll = 3.0 / grid_freq0; % desired PLL rise time (s): 3 periods
wn_pll = 3.0 / rise_time_pll; % desired PLL 2nd order natural frequency (rad/s)
xsi_pll = 0.7; % desired damping factor
kp_pll = 2 * wn_pll * xsi_pll / Vgpk; % Proportional feedback gain
%ki_pll = (wn_pll * wn_pll) / Vgpk; % Integral feedback gain
ti_pll = 2 * xsi_pll / wn_pll; % Integral feedback time constant (s)

%% Frequency domain control analysis

% Laplace variable 's' ('p' in French)s
s = tf('s');
% Controller transfer
C = 1; % TO BE CHANGED!
% Process tranfer
G = 1/s; % TO BE CHANGED!
% Open loop transfer
Hol = C*G;
% Closed loop transfer
Hcl = Hol/(1+Hol);

% Plots
figure(1)
margin(Hol)

figure(2)
step(Hcl)