clear all
close all
clc

Ot=800;        %Pozadovane otacky
Mz=0.05;        %Zatazovi moment
Tm=3;           %v case t
n=1;
Parametre
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Riadenie pomocou napätia
sim('BLDC_Control_U.slx')
time=ScopeData.time;
y=ScopeData.signals.values;

figure(n)
plot(time,y)
grid on
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
title('BLDC - Napatova regulacia')
n=n+1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Porovnanie matematického modelu a SimModelu
sim('BLDC_Model_Equals_Full')
time_e=ScopeData_Equal.time;
y2=ScopeData_Equal.signals.values;

figure(n)
plot(time_e,y2)
grid on
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
title('BLDC - Model vytvorený z rovníc')
n=n+1;

figure(n)
plot(time_e,y2)
grid on
hold on
plot(time,y,'color','r')
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
title('Porovnanie SimModelu a Modelu vytvoreného z rovníc')
legend('Rovnicový model','SimModel')
n=n+1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Riadenie pomocou PWM
clear time y y2 time_e

sim('BLDC_Control_DutyCycle.slx')
time=ScopeData_DutyCycle.time;
y=ScopeData_DutyCycle.signals.values;

figure(n)
plot(time,y)
grid on
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
title('BLDC - Riadenie pomocou PWM')
n=n+1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Interpolácia rýchlosti
clear time y

sim('BLDC_Interpolacia_Rychlosti.slx')
time=ScopeData_Interpolate.time;
y=ScopeData_Interpolate.signals(1).values;
f1=ScopeData_Interpolate.signals(2).values;
f2=ScopeData_Interpolate.signals(4).values;
f3=ScopeData_Interpolate.signals(5).values;

figure(n)
plot(time,y)
grid on
hold on
plot(time,f1,'color','r')
plot(time,f2,'color','g')
plot(time,f3,'color','y')
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
legend('Skutočné otáčky','Interpolované otáčky - 1.metóda','Interpolované otáčky - 2.metóda','Interpolované otáčky - 3.metóda')
title('BLDC - Interpolácia otáčok')
n=n+1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Interpolácia uhlu
clear time y f1 f2 f3

sim('BLDC_Interpolacia_Uhol.slx')
time=ScopeData_Uhol.time;
y=ScopeData_Uhol.signals(1).values;
f1=ScopeData_Uhol.signals(2).values;
f2=ScopeData_Uhol.signals(3).values;
f3=ScopeData_Uhol.signals(4).values;

figure(n)
plot(time,y)
grid on
hold on
plot(time,f1,'color','r')
plot(time,f2,'color','g')
plot(time,f3,'color','y')
xlabel('Čas [s]')
ylabel('uhol [rad]')
legend('Skutočný uhol','Uhol na základe stavu','Interpolácia uhla - 1.metóda','Interpolácia uhla - 2.metóda')
title('BLDC - Interpolácia uhlu')
n=n+1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulácia zjednodušeného modelu
clear time y

Lambda=1.0449e-06;
trap=0.9;

sim('BLDC_equals_Simply.slx')
time=ScopeData_Equal_Simply.time;
y=ScopeData_Equal_Simply.signals.values;

figure(n)
plot(time,y)
grid on
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
title('Zjednodušený rovnicový model')
n=n+1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimácia parametrov z matematického modelu
clear time y

Lambda=0.0143;
trap=0.3;

sim('BLDC_MathModel.slx')
time=ScopeData_MathModel.time;
y=ScopeData_MathModel.signals.values(:,1);
ym=ScopeData_MathModel.signals.values(:,2);
uhol=ScopeData_MathModel_Uhol.signals.values(:,1);
uholm=ScopeData_MathModel_Uhol.signals.values(:,2);

figure(n)
plot(time,y)
grid on
hold on
plot(time,ym,'color','r')
xlabel('Čas [s]')
ylabel('otáčky [ot/min]')
title('Získavanie parametrov z matematického modelu')
legend('Skutočné otáčky','Matematický model')
n=n+1;

figure(n)
plot(time,uhol)
grid on
hold on
plot(time,uholm,'color','r')
xlabel('Čas [s]')
ylabel('uhol [rad]')
title('Získavanie parametrov z matematického modelu')
legend('Skutočný uhol','Matematický model')
n=n+1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulácia vektorového riadenia
clear time y
trap=1; %Prenastavenie orezavania

sim('Vector_Control.slx')
time=ScopeData_Vector.time;
y=ScopeData_Vector.signals.values;

figure(n)
plot(time,y)
grid on
xlabel('Čas [s]')
ylabel('uhlová rýchlosť [rad/s]')
title('Vektorové riadenie')
n=n+1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulácia vektorového riadenia s EKF
clear time 
trap=1;

sim('Vector_Control_EKF.slx')
time=ScopeData_Vector_EKF_I.time;
Ia=ScopeData_Vector_EKF_I.signals.values(:,1);
Ia_est=ScopeData_Vector_EKF_I.signals.values(:,2);
Ib=ScopeData_Vector_EKF_I.signals.values(:,3);
Ib_est=ScopeData_Vector_EKF_I.signals.values(:,4);
Ic=ScopeData_Vector_EKF_I.signals.values(:,5);
Ic_est=ScopeData_Vector_EKF_I.signals.values(:,6);


Omega_est=ScopeData_Vector_EKF_Otacky.signals(1).values(:,1);
Omega_real=ScopeData_Vector_EKF_Otacky.signals(1).values(:,2);
Phi_est=ScopeData_Vector_EKF_Otacky.signals(2).values(:,1);
Phi_real=ScopeData_Vector_EKF_Otacky.signals(2).values(:,2);

figure(n)
grid on
hold on
plot(time,Ia,'color','y')
plot(time,Ib,'color','r')
plot(time,Ic,'color','m')
plot(time,Ia_est,'color','g')
plot(time,Ib_est,'color','b')
plot(time,Ic_est,'color','k')

xlabel('Čas [s]')
ylabel('Prúd [A]')
title('Porovnanie prúdov')
legend('Ia','Estimované Ia','Ib','Estimované Ib','Ic','Estimované Ic')
n=n+1;

figure(n)
subplot(2,1,1)
plot(time,Omega_real,'color','r')
grid on
hold on
plot(time,Omega_est,'color','y')
xlabel('Čas [s]')
ylabel('Uhlová rýchlosť [rad/s]')
title('Porovnanie rýchlosti')
legend('Omega','Estimovaná Omega')

subplot(2,1,2)
plot(time,Phi_real,'color','r')
grid on
hold on
plot(time,Phi_est,'color','y')
xlabel('Čas [s]')
ylabel('Uhol [rad]')
title('Porovnanie uhlov')
legend('Phi','Estimované Phi')
