%% ************************************************************************
% Model         :   PMSM Field Oriented Control
% Description   :   Set Parameters for PMSM Field Oriented Control
% File name     :   mcb_pmsm_foc_hall_f28379d_data.m
% Copyright 2020 The MathWorks, Inc.

%% Parameters needed for Offset computation are
% target.PWM_Counter_Period  - PWM counter value for epwm blocks
% target.CPU_frequency       - CPU frequency of the microcontroller
% Ts                         - Control sample time
% PU_System.N_base           - Base speed for per unit conversion
% pmsm.p                     - Number pole pairs in the motor

% Other parameters are not mandatory for offset computation

%% 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        // simulation time step for controller
Ts_simulink     = T_pwm/2;      %sec        // simulation time step for model simulation
Ts_motor        = T_pwm/2;      %Sec        // Simulation sample time
Ts_inverter     = T_pwm/2;      %sec        // simulation time step for average value inverter
Ts_speed        = 10*Ts;        %Sec        // Sample time for speed controller

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

%% System Parameters // Hardware parameters 
pmsm = mcb_SetPMSMMotorParameters('Teknic2310P');
pmsm.PositionOffset = 0.17;

%% Parameters below are not mandatory for offset computation

inverter = mcb_SetInverterParameters('BoostXL-DRV8305');

inverter.ADCOffsetCalibEnable = 1; % Enable: 1, Disable: 0

% 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 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;

target = mcb_SetProcessorDetails('F28379D',PWM_frequency);
%% Derive Characteristics
pmsm.N_base = mcb_getBaseSpeed(pmsm,inverter); %rpm // Base speed of motor at given Vdc
% mcb_getCharacteristics(pmsm,inverter);

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

PU_System = mcb_SetPUSystem(pmsm,inverter);

%% Controller design // Get ballpark values!

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

%Updating delays for simulation
PI_params.delay_Currents    = int32(Ts/Ts_simulink);
PI_params.delay_Position    = int32(Ts/Ts_simulink);
PI_params.delay_Speed       = int32(Ts_speed/Ts_simulink);
PI_params.delay_Speed1       = (PI_params.delay_IIR + 0.5*Ts)/Ts_speed;

% mcb_getControlAnalysis(pmsm,inverter,PU_System,PI_params,Ts,Ts_speed); 