% mcb_pmsm_foc_mode_script.m
% Initializes the parameters for mcb_host_mode_control.slx and 
% mcb_pmsm_operating_mode_f28379d.slx 

% Copyright 2020 The MathWorks, Inc.
%% 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 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';

%% System Parameters 
% Set motor parameters
pmsm = mcb_SetPMSMMotorParameters('Teknic2310P');
pmsm.PositionOffset = 0.165;

%% Target & Inverter Parameters
target = mcb_SetProcessorDetails('F28379D',PWM_frequency);

inverter = mcb_SetInverterParameters('BoostXL-DRV8305'); %('BOOSTXL-3PhGaNInv');

% 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

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

% Display parameters
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    = 1;
PI_params.delay_Position    = 1;

%% Displaying model variables
disp(pmsm);
disp(inverter);

%% create bus with parameteres and to use in example
createBus;
initbParam=bParam.createMATLABStruct('bParam');

%% Initialize the Dashboard block in simulation
init.mode=0;
init.signal1=7;
init.signal2=8;

% open loop run
init.Vpk=0.15;
init.N_rpm=0.15;

% Torque mode
init.Id_ref=0;
init.Iq_ref=0;
init.Nmode=0; % 0 is locked rotor, 1 is free run
init.Tlmt=0.6; % Speed limit in torque mode

% Speed mode
init.Nref=0.2;

% order of msgId defined in transmission block
msgId=[162, 164, 165, 171, 172, 173, 174 175, 176, 191, 192, 201, 202, 203, 181, 204];
