% autor: Matouš Přikryl
% thesis: HIL testing of BMS
% code purpose: Automatic assessment of the HIL test (test specific - can
% be used only for predefined tests)

%% Clear workspace
clear all;
close all;
clc;

%% choose plot settings
test = 3;           % Choose test (1-3)

%% load files
TestString = string(test);

% load data from HIL
switch test
    case 1
        TestNumber = string(29);
    case 2
        TestNumber = string(31);
    case 3
        TestNumber = string(37);
    otherwise
        fprintf('zadáno špatné číslo testu - opakuj znovu se správným číslem\n');
        return;
end

NameFile = 'data_hil_2_0' + TestNumber;
NameFile_mat = append(NameFile, '.mat');

output_HIL = load(NameFile_mat);
output_HIL = output_HIL.(NameFile);

% load data from model
NameFileModel = 'HIL_model_out_' + TestString + '.mat';
output_model = load(NameFileModel);


%% Prepare data for use
Time = output_HIL.X.Data;

BattV = output_HIL.Y(1).Data;
LoadV = output_HIL.Y(2).Data;
NEG_CTRL = output_HIL.Y(3).Data;
POS_CTRL = output_HIL.Y(4).Data;
PRE_CTRL = output_HIL.Y(5).Data;
SDC_final = output_HIL.Y(6).Data;
ErrCode = output_HIL.Y(7).Data;
BMS_Status = output_HIL.Y(8).Data;
current = output_HIL.Y(9).Data;
TsStatus = output_HIL.Y(11).Data;


%% Assessment 
StatusOk = zeros(1, length(Time));

StatusErrorStart = zeros(1, 10);
StatusErrorEnd = zeros(1, 10);

Status_count = 1;
Status_count_2 = 1;


%% test == 1
if test == 1
    for i = 1:length(Time)
        if Time(i) < 9.5
            StatusOk(i) = 1;
        elseif Time(i) < 33
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 34
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 83
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 83.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 113
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 114
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 174.9
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 175.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 180
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        else
            StatusOk(i) = 0;
        end
    end

    StatusOk(end) = 1;

    for i = 1:(length(Time)-1)
        if StatusOk(i) > StatusOk(i+1)
            StatusErrorStart(Status_count) = Time(i);
            Status_count = Status_count + 1;
        elseif StatusOk(i) < StatusOk(i+1)
            StatusErrorEnd(Status_count_2) = Time(i);
            Status_count_2 = Status_count_2 + 1;
        end
    end

    StatusErrorStart = StatusErrorStart(StatusErrorStart~=0);
    StatusErrorEnd = StatusErrorEnd(StatusErrorEnd~=0);
end


%% test == 2
if test == 2
    for i = 1:length(Time)
        if Time(i) < 9.5
            StatusOk(i) = 1;
        elseif Time(i) < 18
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 19
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 771.9
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 772.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 813
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 814
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 1504.9
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 1505.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 1550
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        else
            StatusOk(i) = 0;
        end
    end

    StatusOk(end) = 1;

    for i = 1:(length(Time)-1)
        if StatusOk(i) > StatusOk(i+1)
            StatusErrorStart(Status_count) = Time(i);
            Status_count = Status_count + 1;
        elseif StatusOk(i) < StatusOk(i+1)
            StatusErrorEnd(Status_count_2) = Time(i);
            Status_count_2 = Status_count_2 + 1;
        end
    end

    StatusErrorStart = StatusErrorStart(StatusErrorStart~=0);
    StatusErrorEnd = StatusErrorEnd(StatusErrorEnd~=0);
end


%% test == 3
if test == 3
    for i = 1:length(Time)
        if Time(i) < 5.5
            StatusOk(i) = 1;
        elseif Time(i) < 11
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 12
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 16.9
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 17.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 0)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 19
            if BMS_Status(i) == 0
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 19.5
            if (BMS_Status(i) == 0) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 23
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 23.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 30
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 30.5
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 40.2
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 41
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 46.9
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 47.5
            if (BMS_Status(i) == 0) || (BMS_Status(i) == 2)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 49
            if BMS_Status(i) == 0
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 49.5
            if (BMS_Status(i) == 0) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 53.2
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 53.7
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 59.9
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 60.7
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 3)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 66.9
            if BMS_Status(i) == 3
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 67.5
            if (BMS_Status(i) == 0) || (BMS_Status(i) == 3)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 69
            if BMS_Status(i) == 0
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 69.5
            if (BMS_Status(i) == 0) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 73.2
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 73.8
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 74.6
            if BMS_Status(i) == 2
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 75.3
            if (BMS_Status(i) == 2) || (BMS_Status(i) == 3)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 81.9
            if BMS_Status(i) == 3
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 82.5
            if (BMS_Status(i) == 3) || (BMS_Status(i) == 0)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 84
            if BMS_Status(i) == 0
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 84.5
            if (BMS_Status(i) == 0) || (BMS_Status(i) == 1)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 88
            if BMS_Status(i) == 1
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 88.8
            if (BMS_Status(i) == 1) || (BMS_Status(i) == 4)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 109
            if BMS_Status(i) == 4
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 111
            if (BMS_Status(i) == 3) || (BMS_Status(i) == 4)
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        elseif Time(i) < 120
            if BMS_Status(i) == 3
                StatusOk(i) = 1;
            else
                StatusOk(i) = 0;
            end
        else
            StatusOk(i) = 0;
        end
    end

    StatusOk(end) = 1;

    for i = 1:(length(Time)-1)
        if StatusOk(i) > StatusOk(i+1)
            StatusErrorStart(Status_count) = Time(i);
            Status_count = Status_count + 1;
        elseif StatusOk(i) < StatusOk(i+1)
            StatusErrorEnd(Status_count_2) = Time(i);
            Status_count_2 = Status_count_2 + 1;
        end
    end

    StatusErrorStart = StatusErrorStart(StatusErrorStart~=0);
    StatusErrorEnd = StatusErrorEnd(StatusErrorEnd~=0);
end

%% Plot
% BMS status
figure('Renderer', 'painters', 'Position', [100 100 770 370]);
plot(output_HIL.X.Data, BMS_Status, 'LineWidth', 1.5)
grid on
grid minor
xlabel('time [s]')
ylabel('BMS status')
hold on;
xregion(Time(1), Time(end), 1, 4);
for i = 1:length(StatusErrorStart)
    xregion(StatusErrorStart(i), StatusErrorEnd(i), 2, 4);
    fprintf('Chyba nalezena (BMS_status), čas: %4.2f sec, trvani erroru: %4.2f sec\n', StatusErrorStart(i), (StatusErrorEnd(i) - StatusErrorStart(i)))
end
hold off;

if max(StatusErrorStart) > 0
    fprintf('')
else
    fprintf('Test proběhl bez chyby\n')
end

