%% ************************************************************************
% Model         :   mcb_ipmsm_pos_est_f28379d
% Description   :   Set Parameters for initial rotor position estimation of
%                   Interior PMSM 
% File name     :   mcb_ipmsm_pos_est_f28379d_data.m

% Copyright 2021 The MathWorks, Inc.

%% Set PWM Switching frequency
PWM_frequency 	= 20e3;            %Hz    // converter s/w freq
T_pwm           = 1/PWM_frequency; %sec   // 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
Ts_motor_simscape = T_pwm/500;     %sec   // Simulation time step for
                                   %         Simscape based PWM inverter

%% Set data type for controller & code-gen
dataType = 'single';               %     // Tested only for single datatype

%% System Parameters // Hardware parameters

pmsm.model   = 'ADLEE-BM-180E'; %           // Model Name
pmsm.sn      = 'BM-180E';       %           // Model Number
pmsm.p       = 4;               %           // No of Pole Pairs
pmsm.Rs      = 0.1354;          %Ohm        // Phase resistor for
                                %              star equivalent motor
pmsm.J       = 1.653e-4;        %Kg-m2      // Inertia in SI units
pmsm.B       = 1.975e-4;        %Kg-m2/s    // Friction Co-efficient
pmsm.Ke      = 8.6454;          %Bemf Const // Vpk_LL/krpm
pmsm.I_rated = 10;              %A          // Rated current (phase-peak)
pmsm.N_max   = 10000;           %rpm        // Max speed

% PM flux computed from Ke
pmsm.FluxPM  = (pmsm.Ke)/(sqrt(3)*2*pi*1000*pmsm.p/60);
% Get T_rated from I_rated
pmsm.T_rated = (3/2)*pmsm.p*pmsm.FluxPM*pmsm.I_rated; 

% Ld vs Id data computed through experiment mention in documentation
% Stator current in d-axis
pmsm.nonlin.idVec    = [-16.385 -12.69 -10.105 -6.305 -3.755 -1.475 1.57 3.925 6.505 10.44 13.04 14.3325]; %A
% Stator d-axis inductance
pmsm.nonlin.Ld_data  = [1.77E-04 1.76E-04 1.76E-04 1.75E-04 1.76E-04 2.63E-04 2.59E-04 1.76E-04 1.73E-04 1.72E-04 1.70E-04	1.69E-04]; %H

% Stator current in q-axis
pmsm.nonlin.iqVec    = linspace(-13,13,20);   %A
% Q-axis inductance value at low currents
pmsm.nonlin.Lq_data  = 2.88E-04;              %H 

% Here Ld has been considered to be dependent only Id
pmsm.nonlin.LdMatrix = pmsm.nonlin.Ld_data'*ones(1,20);  %H

% Here Lq has been considered to be independent of Id, Iq
% Here low current Lq value has been chosen because Ld<Lq has to be
% satisfied for all Id, Iq
pmsm.nonlin.LqMatrix = pmsm.nonlin.Lq_data*ones(12,20);  %H

% Here PM flux has been considered to be independent of Id, Iq
pmsm.nonlin.PmMatrix = pmsm.FluxPM * ones(12,20);

% We have to use phase currents for algorithm in case of delta winding type
% selected for Simscape PMSM motor block.
mtr_handle = Simulink.findBlocks(gcs,'name','PMSM');
if(mtr_handle)
    pmsm.winding = get_param(mtr_handle,'winding_type');
    if pmsm.winding == "ee.enum.statorconnection.wye"
        % Phase currents and line current are equal and have no phase shift
        % in case of star/wye connected motor
        pmsm.PositionOffset = 0;                          %rad
        pmsm.CurrFactor     = 1;                          %
        % Stator phase resistor "pmsm.Rs" is for star equivalent motor
        pmsm.PhaseResFactor = 1;                          %
    elseif pmsm.winding == "ee.enum.statorconnection.delta"
        % Phase currents are (1/sqrt(3)) times and 30 elec. radian shift of
        % line currents in case of delta connected motor
        pmsm.PositionOffset = (30/pmsm.p)*(pi/180);       %rad
        pmsm.CurrFactor     = 1/sqrt(3);                  %
        % Phase resistor for delta equivalent motor is 3 times of phase
        % resistor for star/wye equivalent motor
        pmsm.PhaseResFactor = 3;                          %
    end
end

%% Inverter & Target Parameters

target = mcb_SetProcessorDetails('F28379D',PWM_frequency);

inverter = mcb_SetInverterParameters('BoostXL-DRV8305');

% Enable automatic calibration of ADC offset for current measurement
inverter.ADCOffsetCalibEnable = 1; % Enable: 1, Disable:0

% If automatic ADC offset calibration is disabled, uncomment and update the 
% offset values below manually
% inverter.CtSensAOffset = 2295;      % ADC Offset for phase current A 
% inverter.CtSensBOffset = 2286;      % ADC Offset for phase current B

% Update inverter.ISenseMax based for the chosen motor and target
inverter = mcb_updateInverterParameters(pmsm,inverter,target);

% Max and min ADC counts for current sense offsets
inverter.CtSensOffsetMax = 2500;    % Maximum permitted ADC counts for current sense offset
inverter.CtSensOffsetMin = 1500;    % Minimum permitted ADC counts for current sense offset

%% Parameters for Algorithm
% Input parameters for Pulsating High Frequency (PHF)
PHF.Frequency=2000;             %Hz    // Frequency value of 
                                %         sinusoidal voltage input
                                %      Frequency must be less than 1/10 th
                                %      of sampling speed and large enough 
                                %      such that motor do not rotate.
PHF.Amplitude=0.2;              %PU    // Per Unit amplitude of
                                %         sinusoidal voltage input
PHF.PHF_Duration=2;             %sec   // Execution time of PHF alogorithm
PHF.hftime=PHF.PHF_Duration/Ts; %count // Execution time of PHF alogorithm 

% Input parameters for Dual Pulse (DP)
DP.PulseAmp=0.4;                %PU    // Per Unit amplitude of dc voltage
                                %         dc voltage pulse input
DP.Duration_width=750e-6;       %sec   // Width of the pulse
DP.Duration_delay=50e-3;        %sec   // Delay between two Pulses
DP.width=DP.Duration_width/Ts;  %Count // Width of the pulse in count
DP.delay=DP.Duration_delay/Ts;  %Count // Delay between two Pulses

% Initial Position Estimation (IPE) algorithm parameters
% All below parameters are chosen by trial and error method
IPE.F_coeff=0.01;               %      // Low Pass Filter coefficient
IPE.kp=100;                     %      // Proportional gain for PID
IPE.ki=80;                      %      // Integral gain for PID
IPE.Idle_Duration=20e-3;        %sec   // Idle duration
IPE.idle=IPE.Idle_Duration/Ts;  %count // Idle duration