%% ************************************************************************
% Model         :   ACIM Field Oriented Control 
% Description   :   Set Parameters for AC Induction Motor's Field Oriented Control
% File name     :   mcb_acim_foc_sensorless_f28379d_data.m
% Copyright 2020 The MathWorks, Inc.

%% System Parameters

%% Set PWM Switching frequency
PWM_frequency 	= 20e3;    %Hz          // Converter s/w freq
T_pwm           = 1/PWM_frequency;  %s  // PWM switching time period

%% Set Sample Times
Ts          	= T_pwm;        %sec        // Sample time for control system
Ts_simulink     = T_pwm/2;      %sec        // Simulation time step for model simulation
Ts_motor        = T_pwm/2;      %sec        // Simulation sample time for acim
Ts_inverter     = T_pwm/2;      %sec        // Simulation time step for inverter
Ts_speed        = 10*Ts;        %sec        // Sample time for speed controller

%% Set data type for the model (simulation & code-generation)
dataType = 'single';            % Floating point code-generation 
% dataType = fixdt(1,32,17);    % Fixed point code-generation  

%% System Parameters // Hardware parameters 
% Set inverter parameters
inverter = mcb_SetInverterParameters('BoostXL-DRV8305');

% Set target hardware parameters
target = mcb_SetProcessorDetails('F28379D',PWM_frequency);

% Set motor parameters
acim.model   = 'EM_Synergy-M800006';%// Manufacturer Model Number
acim.sn      = '001';      %         // Manufacturer Serial Number
acim.p       = 2;          % Pole pairs
acim.Rs      = 1.79;       % Stator Resistor
acim.Rr      = 1.05;       % Rotor Resistor
acim.Lls     = 6.81e-3;    % Stator leakage inductance 
acim.Lm      = 30e-3;      % Magnetizing inductance 
acim.Llr     = 6.81e-3;    % Rotor leakage inductance 
acim.J       = 76.6e-6;    % Inertia
acim.B       = 68.148e-6;  % Friction Co-efficient
acim.I_rated = 1.3;        % Rated current (Phase-peak)
acim.I_max   = 1.83;       % Max current (Phase-peak)
acim.V_rated = 14.7;       % Rated voltage (line-line, rms)
acim.N_rated = 1121;       % Rated speed (rpm, speed at full load)
acim.N_base  = 1500;       % Synchronous speed (rpm)
acim.F_rated = 50;         % Rated frequency (Hz)

%% Calibration section // Uncomment and update relevant parameters

% %Update ADC offsets with manually calibrated values below
% inverter.CtSensAOffset = 2292;
% inverter.CtSensBOffset = 2286;

target.SCI_baud_rate    = 12e6;  % Set baud rate for serial communication
target.ADCCalibEnable   = 1;     % Enable : 1, Disable :0 to Auto-calibrate ADCs

% Open loop reference values
T_Ref_openLoop          = 1;                    %Sec // Time for open-loop start-up
Speed_openLoop_PU       = 0.2;                  %PU  // Per-Unit speed referene for open-loop start-up
Vd_Ref_openLoop_PU      = Speed_openLoop_PU;    % Use 1.2x for Dyno setup and 1x for others

% Update motor parameters
acim.Ls = acim.Lls + acim.Lm;
acim.Lr = acim.Llr + acim.Lm;
acim.sigma = (1 - ((acim.Lm^2)/(acim.Ls*acim.Lr))); % Leakage factor

acim.FluxRated  = (acim.V_rated*(sqrt(2/3))/(2*pi*acim.F_rated)); %Rated flux computed from V-by-f
acim.T_rated = (3/2)*acim.p*(acim.Lm/acim.Lr)*acim.FluxRated*acim.I_rated;   %Get T_rated from I_rated

% Computing imaginary/equivalent inductances to set PI controller parameters
acim.Ld = acim.sigma*acim.Ls; 
acim.Lq = acim.sigma*acim.Ls;
acim.FluxPM = (acim.Lm/acim.Lr)*acim.FluxRated;

% Update inverter parameters
% ADC Gain for DRV8305
if acim.I_rated < 5
    inverter.ADCGain = 4;   % ADC Range = +- 4.825A wrto 0-4095 counts
    inverter.SPI_Gain_Setting = 0x502A;
    
elseif acim.I_rated < 7
    inverter.ADCGain = 2;   % ADC Range = +- 9.650A wrto 0-4095 counts
    inverter.SPI_Gain_Setting = 0x5015;

else     
    inverter.ADCGain = 1;   % ADC Range = +- 19.300A wrto 0-4095 counts       
    inverter.SPI_Gain_Setting = 0x5000;        
    
end

% ADC Gain correction to enable division by 2048 for optimization
inverter.ADC_Correction = (3.0 - 1.5)/(3.0 - 1.65);     % ADC Gain correction for 3.0V ADC

inverter.I_max = (inverter.I_max*inverter.ADC_Correction)/inverter.ADCGain;

%% PU System details // Set base values for pu conversion
PU_System = mcb_SetPUSystem(acim,inverter);

%% Controller design 
% Slow response (use for datasheet parameters)
PI_params = mcb.internal.SetControllerParameters(acim,inverter,PU_System,T_pwm,10*Ts,Ts_speed);

% Fast response (requires estimated parameters)
% PI_params = mcb.internal.SetControllerParameters(acim,inverter,PU_System,T_pwm,Ts,Ts_speed);

%Updating delays for simulation
PI_params.delay_Currents    = 1;

% %Uncomment for frequency domain analysis
% mcb_getControlAnalysis(acim,inverter,PU_System,PI_params,Ts,Ts_speed);

%% Displaying model variables
acim
inverter
target
PU_System
