% The MATLAB implementation of current controller using digital model
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
clear all; close all; clc;
l_width = 2; f_size = 16;
Uc1=400; Uc2=-400; R=0.3; L=5.5e-3;
N=2000; fs=50000; Ts=1/fs; f=50;
RR=50; kr=0.025; h=0.10; fs_rr=fs*RR; Ts_rr=1/fs_rr;
t=(0:N-1)*Ts;
i_ref = square(2*pi*f*t,50);
%% conversion of the RL circuit to digital form 
num_s=[1]; den_s=[L R]; % voltage to current transfer function
[num_d den_d]=bilinear(num_s,den_s,fs_rr)
i_out=f_inv_RL_filter_hist(i_ref,h,kr,num_d,den_d,Uc1,Uc2,RR,Ts);
trr=(0:length(i_out)-1)*Ts/RR;
figure('Name','Currents','NumberTitle','off'); 
plot(trr,i_out,t,i_ref/kr,'r','linewidth',l_width),
set(gca,'FontSize',f_size), grid on; 
ylabel('i_{ref}(t)/k_r,i_{out}(t)'), xlabel('Time [s]'); 
legend('i_{ref}(t)/k_r','i_{out}(t)');
