% autor: Matouš Přikryl
% thesis: HIL testing of BMS
% code purpose: Generate input data for HIL tests, or MIL test model of the
% BMS

%% initialization
clear all;          
close all;
clc;

%% HIL variant management
Test = 3;                                   % choose active test variant
Battery_RC = 2;                             % choose 1 for 1RC, choose 2 for 2RC, choose 3 for 1RC_1Batt (minimum computational power needed)
Mode = 1;                                   % choose 1 to run HIL on BMS model, choose 2 to generate data for dSPACE

%% params for simulink
% battery params - by: Jakub Lysák
addpath('.\baterie_Lysak\BatteryModel')

switch Battery_RC
    case 1
        addpath('.\baterie_Lysak\v3_1RC\')
        eD2batterypack1RC_param
    case 2 
        addpath('.\baterie_Lysak\v2_2RC\')
        eD2batterypack2RC_param      
    case 3
        addpath('.\baterie_Lysak\v4_1RC_1Batt\')
        eD2batterypack1RC_1Batt_param
end

% simulation time selection
if Test == 1
    time = 180;
elseif Test == 2
    time = 1550;
elseif Test == 3
    time = 120;
elseif Test == 4
    time = 100;
else
    time = 1;
end

% HIL test params - BMS_master
BMS_STATUS_BOOT = 0;
BMS_STATUS_READY = 1;
BMS_STATUS_OK = 2;
BMS_STATUS_ERROR = 3;
BMS_STATUS_CHARGE = 4;

AIR_MIN_WAIT = 300;
RESTART_TIMEOUT = 2000;
PRECHARGE_MIN_TIME = 2000;
PRECHARGE_MAX_TIME = 4000;
SLAVES_WAIT_AFTER_RESET = 1500;
CHARGER_WAIT_AFTER_SHUTDOWN = 10000;
RESET_BUTTON_DELAY = 1400;
% ERROR_WAIT_FROM_OK_STATE = 3000;
AIR_MAX_CLOSE_TIME = 5000;

U_min = 3;                          % minimal cell voltage
U_max = 4.25;                       % maximal cell voltage
T_max = 60 + 273.15;                % maximal cell temperature
T_max_balanc = 40 + 273.15;         % maximal cell temperature for balancing cells
TS_OPEN_MAX_VOLTAGE = 60;           % maximal open battery load voltage

balancingTreshold = 0.1;            % voltage treshold for balancing [V]
SDC_analog_treshold = 20;           % treshold for SDC in logic 1

cellNumber = 136;

% Fan params
fan.lowSpeed = 1800;                % [rpm]
fan.highSpeed = 16500;              % [rpm]


%% variant definition
TEST_1 = Simulink.Variant('Test == 1');             % most basic test
TEST_2 = Simulink.Variant('Test == 2');             % advanced test
TEST_3 = Simulink.Variant('Test == 3');             % test 3
TEST_4 = Simulink.Variant('Test == 4');             % test 4

RC_1 = Simulink.Variant('Battery_RC == 1');         % battery with one time constant
RC_2 = Simulink.Variant('Battery_RC == 2');         % battery with two time constants
RC_1_1Batt = Simulink.Variant('Battery_RC == 3');   % battery with one time constant and only one cell

%% start simulation and save data
TestString = string(Test);

switch Mode
    case 1
        out = sim("HiL_BMS_model.slx", time);
        save('HIL_model_out_' + TestString + '.mat', 'out');
    case 2
        out = sim("HIL_test_stateflow.slx", time);
        in = struct(out);
        in = in.Data;
        save('input_HIL_' + TestString + '.mat', 'in');
end

