%Author         : David Makowka
%Date           : 6.1.2020
%Description    : Script for parameter setting for simulation
%Warning        : Do not copy without permission

close all
clc

Ra=0.3; 
La=8.5e-6;
Kw=1.0;

%Napeti na tranzistorech
U_bus = 40;
Kp = 5;
OKp = 1.8; %observer Kp

SVMSignalsDistance = 0.8; %10

StartSpeed = +22;
NextSpeed = +22;
ChangeTime = 0.5;


nasobitel = 0.2;
zesik = 600000 * nasobitel;
posunuti = -15 * nasobitel;

%Speed regulator
S_I = 0.0;
S_P = 30;

%DQ regulator
DQ_I = La/Ra;
DQ_P = 100;

%parsing data from continuous model

%***********************************************************************************************************
%               ABC to DQ                                                
%***********************************************************************************************************

First_Angle_plus_oneThird           = max(ABC_DQ.First_Angles.Angle_plus_oneThird);
First_Angle_minus_oneThird          = max(ABC_DQ.First_Angles.Angle_minus_oneThird);

First_Angle_plus_oneThird_min       = min(ABC_DQ.First_Angles.Angle_plus_oneThird);
First_Angle_minus_oneThird_min      = min(ABC_DQ.First_Angles.Angle_minus_oneThird);
%-----------------------------------------------------------------------------------------------------------
Second_Sin                          = max(ABC_DQ.Second_SinCosofAngles.Sin);
Second_Cos                          = max(ABC_DQ.Second_SinCosofAngles.Sin);
Second_Sin_minus_oneThird           = max(ABC_DQ.Second_SinCosofAngles.Sin_minus_oneThird);
Second_Cos_minus_oneThird           = max(ABC_DQ.Second_SinCosofAngles.Cos_minus_oneThird);
Second_Sin_plus_oneThird            = max(ABC_DQ.Second_SinCosofAngles.Sin_plus_oneThird);
Second_Cos_plus_oneThird            = max(ABC_DQ.Second_SinCosofAngles.Cos_plus_oneThird);

Second_Sin_min                      = min(ABC_DQ.Second_SinCosofAngles.Sin);
Second_Cos_min                      = min(ABC_DQ.Second_SinCosofAngles.Sin);
Second_Sin_minus_oneThird_min       = min(ABC_DQ.Second_SinCosofAngles.Sin_minus_oneThird);
Second_Cos_minus_oneThird_min       = min(ABC_DQ.Second_SinCosofAngles.Cos_minus_oneThird);
Second_Sin_plus_oneThird_min        = min(ABC_DQ.Second_SinCosofAngles.Sin_plus_oneThird);
Second_Cos_plus_oneThird_min        = min(ABC_DQ.Second_SinCosofAngles.Cos_plus_oneThird);
%-----------------------------------------------------------------------------------------------------------
Third_A_x_Sin                       = max(ABC_DQ.Third_Multiplications.A_x_Sin);
Third_B_x_Sin_minus_oneThird        = max(ABC_DQ.Third_Multiplications.B_x_Sin_minus_oneThird);
Third_C_x_Sin_plus_oneThird         = max(ABC_DQ.Third_Multiplications.C_x_Sin_plus_oneThird);
Third_A_x_Cos                       = max(ABC_DQ.Third_Multiplications.A_x_Cos);
Third_B_x_Cos_minus_oneTrird        = max(ABC_DQ.Third_Multiplications.B_x_Cos_minus_oneTrird);
Third_C_x_Cos_plus_oneThird         = max(ABC_DQ.Third_Multiplications.C_x_Cos_plus_oneThird);

Third_A_x_Sin_min                   = min(ABC_DQ.Third_Multiplications.A_x_Sin);
Third_B_x_Sin_minus_oneThird_min    = min(ABC_DQ.Third_Multiplications.B_x_Sin_minus_oneThird);
Third_C_x_Sin_plus_oneThird_min     = min(ABC_DQ.Third_Multiplications.C_x_Sin_plus_oneThird);
Third_A_x_Cos_min                   = min(ABC_DQ.Third_Multiplications.A_x_Cos);
Third_B_x_Cos_minus_oneTrird_min    = min(ABC_DQ.Third_Multiplications.B_x_Cos_minus_oneTrird);
Third_C_x_Cos_plus_oneThird_min     = min(ABC_DQ.Third_Multiplications.C_x_Cos_plus_oneThird);
%-----------------------------------------------------------------------------------------------------------
Fourth_vq                           = max(ABC_DQ.Fourth_sums.vq_before_gain);
Fourth_vd                           = max(ABC_DQ.Fourth_sums.vd_before_gain);

Fourth_vq_min                       = min(ABC_DQ.Fourth_sums.vq_before_gain);
Fourth_vd_min                       = min(ABC_DQ.Fourth_sums.vd_before_gain);
%-----------------------------------------------------------------------------------------------------------
Output_vq                           = max(ABC_DQ.Outputs.vq);
Output_vd                           = max(ABC_DQ.Outputs.vd);

Output_vq_min                       = min(ABC_DQ.Outputs.vq);
Output_vd_min                       = min(ABC_DQ.Outputs.vd);

%**************************************************************************
%               Regulator                                                 *  
%**************************************************************************

%Speed regulator
S_odchylka          = max(REGULATOR.speed_regulator.S_odchylka);
S_I_nasob           = max(REGULATOR.speed_regulator.S_I_nasob);
S_integratoru       = max(REGULATOR.speed_regulator.S_integrator);
S_sum               = max(REGULATOR.speed_regulator.S_sum);
S_P_vystup          = max(REGULATOR.speed_regulator.S_vystup);

S_odchylka_min      = min(REGULATOR.speed_regulator.S_odchylka);
S_I_nasob_min       = min(REGULATOR.speed_regulator.S_I_nasob);
S_integratoru_min   = min(REGULATOR.speed_regulator.S_integrator);
S_sum_min           = min(REGULATOR.speed_regulator.S_sum);
S_P_vystup_min      = min(REGULATOR.speed_regulator.S_vystup);
%-----------------------------------------------------------------------------------------------------------
%Q regulator
Q_odchylka          = max(REGULATOR.vq_regulator.Q_odchylka);
Q_I_nasob           = max(REGULATOR.vq_regulator.Q_I_nasob);
Q_integratoru       = max(REGULATOR.vq_regulator.Q_integrator);
Q_sum               = max(REGULATOR.vq_regulator.Q_sum);
Q_P_vystup          = max(REGULATOR.vq_regulator.Q_vystup);

Q_odchylka_min      = min(REGULATOR.vq_regulator.Q_odchylka);
Q_I_nasob_min       = min(REGULATOR.vq_regulator.Q_I_nasob);
Q_integratoru_min   = min(REGULATOR.vq_regulator.Q_integrator);
Q_sum_min           = min(REGULATOR.vq_regulator.Q_sum);
Q_P_vystup_min      = min(REGULATOR.vq_regulator.Q_vystup);
%-----------------------------------------------------------------------------------------------------------
%D regulator
D_odchylka          = max(REGULATOR.vd_regulator.D_odchylka);
D_I_nasob           = max(REGULATOR.vd_regulator.D_I_nasob);
D_integratoru       = max(REGULATOR.vd_regulator.D_integrator);
D_sum               = max(REGULATOR.vd_regulator.D_sum);
D_P_vystup          = max(REGULATOR.vd_regulator.D_vystup);

D_odchylka_min      = min(REGULATOR.vd_regulator.D_odchylka);
D_I_nasob_min       = min(REGULATOR.vd_regulator.D_I_nasob);
D_integratoru_min   = min(REGULATOR.vd_regulator.D_integrator);
D_sum_min           = min(REGULATOR.vd_regulator.D_sum);
D_P_vystup_min      = min(REGULATOR.vd_regulator.D_vystup);

%***********************************************************************************************************
%               DQ to ABC                                                  
%***********************************************************************************************************


Fifth_cos           = max(DQ_AB.Fifth_Angles.cos);
Fifth_sin           = max(DQ_AB.Fifth_Angles.sin);
%-----------------------------------------------------------------------------------------------------------
Sixth_vd_x_cos      = max(DQ_AB.Sixth_Products.vd_x_cos);
Sixth_vq_x_sin      = max(DQ_AB.Sixth_Products.vq_x_sin);
Sixth_vd_x_sin      = max(DQ_AB.Sixth_Products.vd_x_sin);
Sixth_vq_x_cos      = max(DQ_AB.Sixth_Products.vq_x_cos);

Sixth_vd_x_cos_min  = min(DQ_AB.Sixth_Products.vd_x_cos);
Sixth_vq_x_sin_min  = min(DQ_AB.Sixth_Products.vq_x_sin);
Sixth_vd_x_sin_min  = min(DQ_AB.Sixth_Products.vd_x_sin);
Sixth_vq_x_cos_min  = min(DQ_AB.Sixth_Products.vq_x_cos);
%-----------------------------------------------------------------------------------------------------------
Seventh_Alpha       = max(DQ_AB.Seventh_AlphaBeta.a);
Seventh_Beta        = max(DQ_AB.Seventh_AlphaBeta.b);

Seventh_Alpha_min   = min(DQ_AB.Seventh_AlphaBeta.a);
Seventh_Beta_min    = min(DQ_AB.Seventh_AlphaBeta.b);


%***********************************************************************************************************
%                SPACE VECTOR MODULATION - VREF BLOCK                                               
%***********************************************************************************************************

SVM_1_VREF_a_squared       = max(SVM_1_VREF.a_squared);
SVM_1_VREF_b_squared       = max(SVM_1_VREF.b_squared);
SVM_1_VREF_a_plus_b        = max(SVM_1_VREF.a_plus_b);
SVM_1_VREF_vref_sqrt       = max(SVM_1_VREF.vref_sqrt);

SVM_1_VREF_a_squared_min   = min(SVM_1_VREF.a_squared);
SVM_1_VREF_b_squared_min   = min(SVM_1_VREF.b_squared);
SVM_1_VREF_a_plus_b_min    = min(SVM_1_VREF.a_plus_b);
SVM_1_VREF_vref_sqrt_min   = min(SVM_1_VREF.vref_sqrt);

%***********************************************************************************************************
%                 SPACE VECTOR MODULATION - TIME CALCULATIONS                                  
%***********************************************************************************************************

%Part1
SVM_2_Part1_First         = max(SVM_2.Part1.First);
SVM_2_Part1_Second        = max(SVM_2.Part1.Second);
SVM_2_Part1_Third        = max(SVM_2.Part1.Third);

SVM_2_Part1_First_min     = min(SVM_2.Part1.First);
SVM_2_Part1_Second_min    = min(SVM_2.Part1.Second);
SVM_2_Part1_Third_min    = min(SVM_2.Part1.Third);
%-----------------------------------------------------------------------------------------------------------
%Part2
SVM_2_Part2_First         = max(SVM_2.Part2.First);
SVM_2_Part2_Second        = max(SVM_2.Part2.Second);
SVM_2_Part2_Third         = max(SVM_2.Part2.Third);
SVM_2_Part2_Fourth        = max(SVM_2.Part2.Fourth);
SVM_2_Part2_Fifth         = max(SVM_2.Part2.Fifth);

SVM_2_Part2_First_min     = min(SVM_2.Part2.First);
SVM_2_Part2_Second_min    = min(SVM_2.Part2.Second);
SVM_2_Part2_Third_min     = min(SVM_2.Part2.Third);
SVM_2_Part2_Fourth_min    = min(SVM_2.Part2.Fourth);
SVM_2_Part2_Fifth_min     = min(SVM_2.Part2.Fifth);
%-----------------------------------------------------------------------------------------------------------
%Part3
SVM_2_Part3_First         = max(SVM_2.Part3.First);
SVM_2_Part3_Second        = max(SVM_2.Part3.Second);
SVM_2_Part3_Third         = max(SVM_2.Part3.Third);
SVM_2_Part3_Fourth        = max(SVM_2.Part3.Fourth);
SVM_2_Part3_Fifth         = max(SVM_2.Part3.Fifth);

SVM_2_Part3_First_min     = min(SVM_2.Part3.First);
SVM_2_Part3_Second_min    = min(SVM_2.Part3.Second);
SVM_2_Part3_Third_min     = min(SVM_2.Part3.Third);
SVM_2_Part3_Fourth_min    = min(SVM_2.Part3.Fourth);
SVM_2_Part3_Fifth_min     = min(SVM_2.Part3.Fifth);
%-----------------------------------------------------------------------------------------------------------
%Part4
SVM_2_Part4_First         = max(SVM_2.Part4.First);

SVM_2_Part4_First_min     = min(SVM_2.Part4.First);

%***********************************************************************************************************
%                 SPACE VECTOR MODULATION - CALCULATION OF TIME INTERVALS                                        
%***********************************************************************************************************

SVM_3_SUMS_Sum_1        = max(SVM_3_SUMS.Sum_1);
SVM_3_SUMS_Sum_2        = max(SVM_3_SUMS.Sum_2);
SVM_3_SUMS_Sum_3        = max(SVM_3_SUMS.Sum_3);

SVM_3_SUMS_Sum_1_min    = min(SVM_3_SUMS.Sum_1);
SVM_3_SUMS_Sum_2_min    = min(SVM_3_SUMS.Sum_2);
SVM_3_SUMS_Sum_3_min    = min(SVM_3_SUMS.Sum_3);

%***********************************************************************************************************
%                 SPACE VECTOR MODULATION - SWITCHING                                   
%***********************************************************************************************************

%Output of the multiport switch
SVM_4_SWITCHING_A_high_side         = max(SVM_4_SWITCHING.Switch_outputs.A_high_side);
SVM_4_SWITCHING_B_high_side         = max(SVM_4_SWITCHING.Switch_outputs.B_high_side);
SVM_4_SWITCHING_C_high_side         = max(SVM_4_SWITCHING.Switch_outputs.C_high_side);
SVM_4_SWITCHING_A_low_side          = max(SVM_4_SWITCHING.Switch_outputs.A_low_side);
SVM_4_SWITCHING_B_low_side          = max(SVM_4_SWITCHING.Switch_outputs.B_low_side);
SVM_4_SWITCHING_C_low_side          = max(SVM_4_SWITCHING.Switch_outputs.C_low_side);

SVM_4_SWITCHING_A_high_side_min     = min(SVM_4_SWITCHING.Switch_outputs.A_high_side);
SVM_4_SWITCHING_B_high_side_min     = min(SVM_4_SWITCHING.Switch_outputs.B_high_side);
SVM_4_SWITCHING_C_high_side_min     = min(SVM_4_SWITCHING.Switch_outputs.C_high_side);
SVM_4_SWITCHING_A_low_side_min      = min(SVM_4_SWITCHING.Switch_outputs.A_low_side);
SVM_4_SWITCHING_B_low_side_min      = min(SVM_4_SWITCHING.Switch_outputs.B_low_side);
SVM_4_SWITCHING_C_low_side_min      = min(SVM_4_SWITCHING.Switch_outputs.C_low_side);

%-----------------------------------------------------------------------------------------------------------
%Output of sumator, which is right after multiport switch
SVM_4_SWITCHING_A_high_side_sum         = max(SVM_4_SWITCHING.Sum_outputs.A_high_side);
SVM_4_SWITCHING_B_high_side_sum         = max(SVM_4_SWITCHING.Sum_outputs.B_high_side);
SVM_4_SWITCHING_C_high_side_sum         = max(SVM_4_SWITCHING.Sum_outputs.C_high_side);
SVM_4_SWITCHING_A_low_side_sum          = max(SVM_4_SWITCHING.Sum_outputs.A_low_side);
SVM_4_SWITCHING_B_low_side_sum          = max(SVM_4_SWITCHING.Sum_outputs.B_low_side);
SVM_4_SWITCHING_C_low_side_sum          = max(SVM_4_SWITCHING.Sum_outputs.C_low_side);

SVM_4_SWITCHING_A_high_side_sum_min     = min(SVM_4_SWITCHING.Sum_outputs.A_high_side);
SVM_4_SWITCHING_B_high_side_sum_min     = min(SVM_4_SWITCHING.Sum_outputs.B_high_side);
SVM_4_SWITCHING_C_high_side_sum_min     = min(SVM_4_SWITCHING.Sum_outputs.C_high_side);
SVM_4_SWITCHING_A_low_side_sum_min      = min(SVM_4_SWITCHING.Sum_outputs.A_low_side);
SVM_4_SWITCHING_B_low_side_sum_min      = min(SVM_4_SWITCHING.Sum_outputs.B_low_side);
SVM_4_SWITCHING_C_low_side_sum_min      = min(SVM_4_SWITCHING.Sum_outputs.C_low_side);



















disp("Script finished")