% Data script for FOC FPGA demo

%% Set PWM Switching frequency and Sample times 


data.pwm.TimePerCount = 2E-8;
data.pwm.Period = 4E-5; % PWM period or 1/switching frequency 

data.pwm.CountPerPeriod = round(data.pwm.Period / data.pwm.TimePerCount);
data.pwm.CounterMax = data.pwm.CountPerPeriod / 2.0; % maximum count for up down counter 
data.pwm.DeadTimeCount = 5;
data.pwm.DeadTimeSec = 1E-7;

data.sampleTime.CurrentControl = data.pwm.Period; 
data.sampleTime.OverCurrentDetector = data.pwm.Period;
data.sampleTime.EncoderPosition = data.pwm.Period;

data.sampleTime.OpenLoopVelocityControl = 25*data.pwm.Period;
data.sampleTime.VelocityControl = 25*data.pwm.Period;
data.sampleTime.EncoderCalibration = 25*data.pwm.Period;

data.sampleTime.PwmPeripheral = 2E-8;
data.sampleTime.EncoderPeripheral = 2E-8;
data.sampleTime.AdcPeripheral = 2E-8;
data.sampleTime.MotorControlFpgaClock = 2E-8;


%% Motor Parameters

data.pmsm = mcb_SetPMSMMotorParameters('BLY172S');

%% Inverter Parameters

data.inverter.V_dc = 24;
data.inverter.ISenseMax = 5;
data.inverter.GatesActiveHigh = true;
data.inverter.GatesComplementary = false;
data.inverter.R_board =0.0001;
%% Control gains
data.pmsm.N_base = mcb_getBaseSpeed(data.pmsm,data.inverter); %rpm // Base speed of motor at given Vdc

data.PU_System = mcb_SetPUSystem(data.pmsm, data.inverter);


data.PI_params = mcb_SetControllerParameters(data.pmsm,data.inverter,data.PU_System,data.pwm.Period,12*data.sampleTime.CurrentControl ,data.sampleTime.VelocityControl);
%% data 

data.adc.TransducerVoltPerAmpere = [1 1];
data.adc.TransducerVoltAtZeroAmpere = [0 0];
data.adc.CountMin = 0;
data.adc.CountMax = 65535;  % 16 bit 
data.adc.CountPerTransducerVolt = -1024;
data.adc.CountAtZeroTransducerVolt = 32868;


data.paramAdcAmperePerCount = [-0.00097656 -0.00097656];
data.paramAdcCountAtZeroAmpere = [32868 32868];
data.paramAdcEnablecalibration = 1;
data.paramAdcCalibrationSamples = 16; 

data.dataRange.RotorVelocity.Max = 800; % max speed in rads/sec
data.dataRange.RotorVelocity.Min = -800;
data.dataRange.PhaseCurrent.Max = 12;
data.dataRange.PhaseCurrent.Min = -12;
data.dataRange.PhaseVoltage.Max = 12;
data.dataRange.PhaseVoltage.Min = -12;
data.dataRange.DqVoltage.Max = data.inverter.V_dc/sqrt(3);
data.dataRange.DqVoltage.Min = -data.inverter.V_dc/sqrt(3);
data.dataRange.EncoderCount.Max = 8000;
data.dataRange.EncoderCount.Min = 0;

data.enHwSwMaskDisplay = true;

data.encoder.RotorPositionAtIndex = 0;
data.encoder.ALeadsB = false;
data.encoder.NumberOfSlits = 1250;
data.encoder.CountPerRevolution = 5000;
data.encoder.RotorPositionResolution = 0.0012566370614359172;
data.encoder.PositionValidAtStartup = false;
data.encoder.DebounceCount = 20;


data.paramCurrentControlSatVoltage = 13.856406460551021;
data.paramEncoderCalibrateAtStartup = 1;
data.paramEncoderCalibrationDuration = 0.2;
data.paramEncoderCalibrationMaxError = 0.0012566370614359172;
data.paramEncoderCalibrationVoltage = 2;
data.paramEncoderRotorPositionAtZeroCount = 0;
data.paramEncoderRotorPositionPerCount = 0.0012566370614359172;
data.paramOpenLoopVelocityAcceleration = 40;
data.paramOpenLoopVelocityToVoltsBias = 0.4;
data.paramOpenLoopVelocityToVoltsScalar = 0.04;
data.paramOverCurrentDuration = 0.05;
data.paramOverCurrentEnable = 0;
data.paramOverCurrentLimit = 10.45;
data.paramPwmCompareAtZeroVolt = data.pwm.CountPerPeriod / 4.0;
data.paramPwmCompareMax = data.pwm.CountPerPeriod / 2.0;
data.paramPwmComparePerVolt = data.pwm.CounterMax/data.inverter.V_dc;
data.paramVelocityControlSatCurrent = data.pmsm.I_rated;


% End of parameters 
%%

dataDictionaryObj = Simulink.data.dictionary.open('TrenzData.sldd');
dataSectObj = getSection(dataDictionaryObj,'Design Data');

% Below function must be updated if a new parameter is added  
mcb_update_data_dictionary(dataSectObj, data ); 
clear data;

dataDictionaryObj.close;               % close the data dictionary handle
clear dataDictionaryObj dataSectObj;  % clear temporary variables from base workspace


