%% ************************************************************************
% Model         :   PMSM Field Oriented Control
% Description   :   Set Parameters for PMSM Field Oriented Control
% File name     :   mcb_pmsm_foc_dyno_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_motor1 = mcb_SetPMSMMotorParameters('Teknic2310P');
pmsm_motor1.PositionOffset = 0.165; % Enter offset values from calibration

pmsm_motor2 = mcb_SetPMSMMotorParameters('Teknic2310P');
pmsm_motor2.PositionOffset = 0.165; % Enter offset values from calibration

%% Parameters below are not mandatory for offset computation

inverter_motor1 = mcb_SetInverterParameters('BoostXL-DRV8305');
inverter_motor2 = mcb_SetInverterParameters('BoostXL-DRV8305');

inverter_motor1.ADCOffsetCalibEnable = 1; % Enable: 1, Disable:0
inverter_motor2.ADCOffsetCalibEnable = 1; % Enable: 1, Disable:0



% % Update ADC Gain for DRV8305 on motor side
% if pmsm_motor1.I_rated < 5
%     inverter_motor1.ADCGain = 4;   % ADC Range = +- 4.825A wrto 0-4095 counts
%     inverter_motor1.SPI_Gain_Setting = 0x502A;
%     
% elseif pmsm_motor1.I_rated < 7
%     inverter_motor1.ADCGain = 2;   % ADC Range = +- 9.650A wrto 0-4095 counts
%     inverter_motor1.SPI_Gain_Setting = 0x5015;
% 
% else     
%     inverter_motor1.ADCGain = 1;   % ADC Range = +- 19.300A wrto 0-4095 counts       
%     inverter_motor1.SPI_Gain_Setting = 0x5000;        
%     
% end

inverter_motor1.I_max=21.4332;
inverter_motor2.I_max=21.4332;
target = mcb_SetProcessorDetails('F28379D',PWM_frequency);

%% Derive Characteristics
pmsm_motor1.N_base = mcb_getBaseSpeed(pmsm_motor1,inverter_motor1); %rpm // Base speed of motor at given Vdc
pmsm_motor2.N_base = mcb_getBaseSpeed(pmsm_motor2,inverter_motor2); %rpm // Base speed of motor at given Vdc

% mcb_getCharacteristics(pmsm_motor1,inverter_motor1);
% mcb_getCharacteristics(pmsm_motor2,inverter_motor2);

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

PU_System_motor1 = mcb_SetPUSystem(pmsm_motor1,inverter_motor1);
PU_System_motor2 = mcb_SetPUSystem(pmsm_motor2,inverter_motor2);

%% Controller design // Get ballpark values!
% for motor 1 
PI_params_motor1 = mcb.internal.SetControllerParameters(pmsm_motor1,inverter_motor1,PU_System_motor1,T_pwm,Ts,Ts_speed);

%Updating delays for simulation
PI_params_motor1.delay_Currents    = 1; %int32(Ts/Ts_simulink);
PI_params_motor1.delay_Position    = 1; %int32(Ts/Ts_simulink);

% mcb_getControlAnalysis(pmsm_motor1,inverter_motor1,PU_System_motor1,PI_params_motor1,Ts,Ts_speed); 

% for motor 2
PI_params_motor2 = mcb.internal.SetControllerParameters(pmsm_motor2,inverter_motor2,PU_System_motor2,T_pwm,Ts,Ts_speed);

%Updating delays for simulation
PI_params_motor2.delay_Currents    = 1; %int32(Ts/Ts_simulink);
PI_params_motor2.delay_Position    = 1; %int32(Ts/Ts_simulink);

% mcb_getControlAnalysis(pmsm_motor2,inverter_motor2,PU_System_motor2,PI_params_motor2,Ts,Ts_speed); 