clear all;

%% Motor parameters (TG Drives  T2-0040-30-24)
    R2f = 2*0.1;            % Resistance of two phases
    L2f = 0.000346*sqrt(3); % Inductance of two phases
    p   = 5;                % Number of pole pairs
    J   = 6e-5;             % Moment of inertia
    PsiPM = 0.009428;       % Magnetic flux permanent magnets (BackEMF const)
    Km  = 0.1;              % Moment constant

    % Conversion to phase values
    Rs  = R2f/2;            % Resistance of one phase
    Lf  = L2f/sqrt(3);      % Inductance of one phase
    Ld  = Lf;               % Inductance in d-axis
    Lq  = Lf;               % Inductance in q-axis

    Tel = Lf/Rs;            % Electrical time constant

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Current controllers
    foi = 800;
    w0i = 2*pi*foi;
    Ts_fast = 100e-6;          % Sampling period for current controllers
   
    % pole placement design
    Tsettle = 1/(2*pi*foi);    % Settling time
    Ksi  = 1/(sqrt(2));        % Attenuation (3dB)
    Kp_i = Rs*(2*Ksi*w0i*Tel-1);           
    Ki_i = Tel*Rs*w0i*w0i;
    Ti_i = Kp_i/Tel/Rs/w0i^2;
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Speed controller
    fow  = 50;
    wow  = 2*pi*fow; 
    Ts_slow = 1e-3;            % Sampling period for speed controller
    Tsettlew   = 4.5/wow;      % Settling time
    Ksiw = 1;                  % Attenuation (3dB)
    
    Kp_w = 2*Ksiw*wow*J/Km;
    Ti_w = 2*Ksiw/wow;
    Ki_w = Kp_w/Ti_w;
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Voltages and currents
    DCbusMax = 100;           % DC-bus supply voltage [V]
    OutCurMax = 10;           % Maximum output current [A]
    
    % DCbus ripple
    f_DCbus = 100;            % DCbus frequency after rectifing
    Cout_DCbus = 2e-3;        % Capacity on the output of the rectifier
    
    % Power Stage
    Td = 1e-6;                % Dead-time length
    PWMps = 20e3;             % Power Stage PWM frequency
%     Cp_DCbus = 10e-9;         % Parasitic capacitance of one transistor
    load data_for_PS.mat;     % Load data for Custom Power Stage

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%