%% ************************************************************************
% Model         :   PMSM Field Weakening Control with MTPA
% Description   :   Set Parameters for PMSM Field Weakening Control with MTPA
% File name     :   mcb_pmsm_fwc_qep_f28069MLaunchPad_data.m
% Copyright 2020 The MathWorks, Inc.

%% Simulation Parameters

%% Set PWM Switching frequency
PWM_frequency 	= 10e3;    %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 pmsm
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 motor parameters

pmsm = mcb_SetPMSMMotorParameters('BLY171D');
% pmsm = mcb_SetPMSMMotorParameters('Teknic2310P');

% Set inverter parameters
inverter = mcb_SetInverterParameters('BoostXL-DRV8305');

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

%% 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    = 5.625e6;  % Set baud rate for serial communication
target.ADCCalibEnable   = 1;        % Enable : 1, Disable :0 to Auto-calibrate ADCs

% %Update QEP position sensor with calibrated value below
pmsm.PositionOffset = 0.1732;    %PU position// QEP Offset 
% pmsm.QEPSlits       = 1000;    %           // QEP Encoder Slits

% %Update custom parameters for your motor below
% pmsm.Rs     = 0.5581;     % Stator Resistor
% pmsm.Ld     = 1.3998e-4;  % D-axis inductance value
% pmsm.Lq     = 1.3453e-4;  % Q-axis inductance value
% pmsm.Ke     = 4.78;       %Bemf Const	// Vline_peak/krpm
% pmsm.J      = 8.8513e-6;  % Inertia
% pmsm.B      = 4.0710e-5;  % Friction Co-efficient
% pmsm.I_rated = 7.1;       % Rated current
% 
% pmsm.FluxPM  = (pmsm.Ke)/(sqrt(3)*2*pi*1000*pmsm.p/60); %PM flux computed from Ke
% pmsm.T_rated = (3/2)*pmsm.p*pmsm.FluxPM*pmsm.I_rated;   %Get T_rated from I_rated

% %Update ADC Gain for DRV8305
if pmsm.I_rated < 5
    inverter.ADCGain = 4;   % ADC Range = +- 4.825A wrto 0-4095 counts
    inverter.SPI_Gain_Setting = 0x502A;
    
elseif pmsm.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 scale for 3.3V ADC
inverter.ADC_Correction = (3.3 - 1.65)/(3.0 - 1.65);  % ADC Gain correction for 3.3V ADC
      
inverter.I_max = (inverter.I_max*inverter.ADC_Correction)/inverter.ADCGain;

%% Derive Characteristics
pmsm.N_base = mcb_getBaseSpeed(pmsm,inverter); %rpm // Base speed of motor at given Vdc
% mcb_getCharacteristics(pmsm,inverter);       % Uncomment for motor characteristics

%% PU System details // Set base values for pu conversion

PU_System = mcb_SetPUSystem(pmsm,inverter);

%% Controller design 
PI_params = mcb.internal.SetControllerParameters(pmsm,inverter,PU_System,T_pwm,Ts,Ts_speed);

%Updating delays for simulation
PI_params.delay_Currents    = 1;
PI_params.delay_Position    = 1;

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

%% Displaying model variables
pmsm
inverter
target
PU_System
