%Federal University of Santa Catarina
%Author: Tiago Davi Curi Busarello
%Date: September 2019
%
%Project: Voltage control for Boost Converter
%Valid for Boost Converter
%Controller designed based on the book of Marian K. Kazimierczuk and also
%on K-factor
%

format long
clear all
close all
clc

Vi=12;
Vo=20;
fs=20e3;
Hv=1/8;  %beta
ts = 1/fs;
VTm = 15; %peak of the carrier
td = 1.5*ts; %Delay of the PWM

L = 156e-6; %Inductor
C = 136e-6;  %Capacitor
rL = 0.19;  %Inductor resistance
rC = 0.111; %Capacitor resistance 
RL = 40;    %Load resistance
rDS = 0.18; %Drain-Source resistance of the Mosfet;
RF =  0.072; %Diode resistance

s = tf('s');
opts = bodeoptions('cstprefs');
opts.FreqUnits = 'Hz';
opts.PhaseWrapping = 'on';
opts.PhaseWrappingBranch = -360;

%Computing D
% D = 1 - Vi/Vo;

Dnom = 0.5;
r = Dnom*rDS + (1-Dnom)*RF + rL;

f0 = (1/(2*pi))*sqrt((RL*(1-Dnom)^2+r)/(L*C*(RL+rC))); %Corner frequency
wzn = 1/(C*rC);
wzp = (RL*(1-Dnom)^2-r)/L;

fzn = wzn/(2*pi); %Frequency of the left-half plane zero
fzp = wzp/(2*pi); %Frequency of the right-half plane zero

damp = (L + C*((r*(RL+rC))+RL*rC*(1-Dnom)^2))/(2*sqrt(L*C*(RL+rC)*(r+RL*(1-Dnom)^2))); %Damping factor
Q = 1/(2*damp); %Quality factor
p1 = -damp*2*pi*f0 + 2*pi*f0*sqrt(damp^2-1); %Frquency of the pole 1 in rad/s
p2 = -damp*2*pi*f0 - 2*pi*f0*sqrt(damp^2-1); %Frquency of the pole 2 in rad/s

fd = f0*(sqrt(1-damp^2));

%Delay of PWM 
wzd = 2/td;
wpd = 2/td;


Tp_d = (Vo/(1-Dnom))*(rC/(RL + rC))*(s+wzn)*(s-wzp)*(s-wzd)/((s^2+2*damp*2*pi*f0*s+(2*pi*f0)^2)*(s+wpd)); %Boost transfer function with PWM delay
Tp_ud = -(Vo/(1-Dnom))*(rC/(RL + rC))*(s+wzn)*(s-wzp)*(s-0)/((s^2+2*damp*2*pi*f0*s+(2*pi*f0)^2)*(s+0)); %Boost transfer function without PWM dealy

figure
bode(Tp_d,opts)
hold
bode(Tp_ud,opts)
grid
title('Comparison between the boost transfer function with and without considering the PWM delay')

%Open-loop transfer function without the controller and without considering
%the pwm delay
OLTFu = Tp_ud*Hv/VTm;

figure
bode(OLTFu,opts)
grid

%Design procedure for a integral-double-loop controller

fc = fs/10;   %Desired cut-off frequency
PMd = 60; %Desired Phase-Margin

%Voltage controller design based on the K-factor

inst = evalfr(OLTFu, 2*pi*fc*j);
fase_fc = rad2deg(phase(inst));  

ganho_fc = -mag2db(abs(inst));   %sinal de menos na frente 
ganho_real = 10^(ganho_fc/20);   %sem levar em conta o módulo

alpha = PMd - fase_fc -90;

K2 = (tand((alpha + 180)/4))^2;

R1 = 1000;
R2 = ganho_real*R1/(sqrt(K2));
C1 = sqrt(K2)/(2*pi*fc*R2);
C2 = 1/(2*pi*fc*R2*sqrt(K2));
C3 = sqrt(K2)/(2*pi*fc*R1);
R3 = 1/(2*pi*fc*C3*sqrt(K2));

Cv = (s^2*R2*C1*C3*(R1+R3) + s*(R2*C1+R1*C3+R3*C3)+1) /(s^3*R1*R2*R3*C1*C2*C3 + s^2*(R1*R3*C3*(C1+C2)+R1*R2*C1*C2)+s*R1*(C1+C2));

OLTFc = OLTFu*Cv;
figure
bode(OLTFu,opts);
title('Bode da FT de malha aberta sem controle');
hold 
bode(OLTFc,opts)
title('Bode da FT de malha aberta com controle');
bode(Cv,opts)
title('Bode da FT do controlador projetado');
grid

CLTF = OLTFc/(OLTFc + 1);
figure 
bode(CLTF,opts)
title('Bode da FT de malha fechada');
grid

%Discretization of the controller

D1 = R2*C1*C3*(R1+R3);
D2 = R2*C1 + R1*C3 + R3*C3;
D3 = R1*R2*R3*C1*C2*C3;
D4 = R1*R3*C1*(C1+C2) + R1*R2*C1*C2;
D5 = R1*(C1+C2);

Dcte = D3+D4*ts+D5*ts^2;

b0 = (ts*D1 + D2*ts^2 + ts^3)/Dcte;
b1 = -(2*ts*D1 + D2*ts^2)/Dcte;
b2 = D1*ts/Dcte;
b3 = 0;

a0 = 1;
a1 = -(3*D3+2*D4*ts+D5*ts^2)/Dcte;
a2 = (3*D3+D4*ts)/Dcte;
a3 = -D3/Dcte;

b = [b0 b1 b2 b3];
a = [a0 a1 a2 a3];

H = tf(b,a,ts);

figure
bode(H)
hold
bode(Cv)
grid
title('Comparison between the analog and digital controllers');

figure
margin(OLTFc)
grid
title('Phase Margin and Cut-off frequency obtained with the designed controller');

