% autor: Matouš Přikryl
% thesis: HIL testing of BMS
% code purpose: Compare output of MIL with output from HIL

%% clean workspace
clear all;
close all;
clc;

%% choose display configuration
mode = 1;                           % 1 for normal, 2 for debug, 3 only HIL, 4 only model
test = 1;                           % test number (1-3)

%% load files
TestString = string(test);

% load data from HIL
switch test
    case 1
        TestNumber = string(41); % 29
    case 2
        TestNumber = string(31); % 31
    case 3
        TestNumber = string(37); % 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;
Time_model = output_model.out.tout;

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;


%% plot data comparisons
% Batt and Load voltages
figure('Renderer', 'painters', 'Position', [100 100 770 370])
legend();
grid on
grid minor
xlim([0 Time(end)])
title('Load voltage')
xlabel('time [s]')
ylabel('voltage load [V]')

hold on;
if mode ~= 4
    %plot(Time, BattV, 'DisplayName', 'BattV')
    plot(Time, LoadV, 'DisplayName', 'LoadV', 'LineWidth', 1.5)
end
if mode ~= 3
    %plot(Time_model, squeeze(output_model.out.BatteryVoltage.signals.values), 'DisplayName', 'BattV model')
    plot(Time_model, output_model.out.HV_load, 'DisplayName', 'LoadV model', 'LineWidth', 1.5)
end
hold off;

% SDC final
figure('Renderer', 'painters', 'Position', [100 100 770 370])
legend();
grid on
grid minor
xlim([0 Time(end)])
title('SDC final')
xlabel('time [s]')
ylabel('SDC final')

hold on;
if mode ~= 4
plot(Time, SDC_final, 'DisplayName', 'SDC final', 'LineWidth', 1.5)
end
if mode ~= 3
plot(Time_model, output_model.out.SDC_final_analog.signals.values, 'DisplayName', 'SDC final model', 'LineWidth', 1.5)
end
hold off;

% AIR control
figure('Renderer', 'painters', 'Position', [100 100 770 650]);

subplot(3,1,1);
legend();
grid on
grid minor
xlim([0 Time(end)])
xlabel('time [s]')
ylabel('NEG CTRL')

hold on;
if mode ~= 4
plot(Time, NEG_CTRL, 'DisplayName', 'NEG CTRL', 'LineWidth', 1.5)
end
if mode ~= 3
plot(Time_model, output_model.out.NEG_CTRL, 'DisplayName', 'NEG CTRL model', 'LineWidth', 1.5)
end
hold off;

subplot(3,1,2);
legend();
grid on
grid minor
xlim([0 Time(end)])
xlabel('time [s]')
ylabel('POS CTRL')

hold on;
if mode ~= 4
plot(Time, POS_CTRL, 'DisplayName', 'POS CTRL', 'LineWidth', 1.5)
end
if mode ~= 3
plot(Time_model, output_model.out.POS_CTRL, 'DisplayName', 'POS CTRL model', 'LineWidth', 1.5)
end
hold off;

subplot(3,1,3);
legend();
grid on
grid minor
xlim([0 Time(end)])
xlabel('time [s]')
ylabel('PRE CTRL')

hold on;
if mode ~= 4
plot(Time, PRE_CTRL, 'DisplayName', 'PRE CTRL', 'LineWidth', 1.5)
end
if mode ~= 3
plot(Time_model, output_model.out.PRE_CTRL, 'DisplayName', 'PRE CTRL model', 'LineWidth', 1.5)
end
hold off;

% BMS status
figure('Renderer', 'painters', 'Position', [100 100 770 370]);
legend();
grid on
grid minor
xlim([0 Time(end)])
xlabel('time [s]')
ylabel('BMS status')

hold on;
if mode ~= 4
plot(Time, BMS_Status, 'DisplayName', 'BMS status', 'LineWidth', 1.5)
end
if mode ~= 3
plot(Time_model, output_model.out.master_status, 'DisplayName', 'BMS status model', 'LineWidth', 1.5)
end
% plot(Time_model, output_model.out.error_voltage.signals.values, 'DisplayName', 'error voltage')
hold off;

%% adittional plots - mainly for debug
if mode == 2
    % Max and min temperatures
    maxT = squeeze(max(output_model.out.CellTemperature.signals.values));
    minT = squeeze(min(output_model.out.CellTemperature.signals.values));
    
    figure(5)
    legend();
    grid on
    grid minor
    title('Input Temperatures')
    xlabel('time [s]')
    ylabel('status')
    
    hold on;
    plot(Time_model, maxT, 'DisplayName', 'maximal input temperatures')
    plot(Time_model, minT, 'DisplayName', 'minimal input temperatures')
    hold off;
    
    % Max and min voltages
    maxV = squeeze(max(output_model.out.CellVoltage.signals.values));
    minV = squeeze(min(output_model.out.CellVoltage.signals.values));
    
    figure(6)
    legend();
    grid on
    grid minor
    title('Input Voltages')
    xlabel('time [s]')
    ylabel('status')
    
    hold on;
    plot(Time_model, maxV, 'DisplayName', 'maximal input voltages')
    plot(Time_model, minV, 'DisplayName', 'minimal input voltages')
    hold off;
    
    % error code
    figure(7)
    plot(Time, ErrCode)
    grid on
    grid minor
    title('error code BMS')
    xlabel('time [s]')
    ylabel('error code')

%     % current
%     figure(8)
%     plot(Time, current)
%     grid on
%     grid minor
%     title('current')
%     xlabel('time [s]')
%     ylabel('current [A]')

    % fan RPM
    figure(9)
    plot(Time, output_HIL.Y(10).Data)
    grid on
    grid minor
    title('Fan RPM')
    xlabel('time [s]')
    ylabel('RPM [1/min]')

    % debug model
    figure(10)
    plot(Time_model, output_model.out.debug)
    grid on
    grid minor
    title('debug model')
    xlabel('time [s]')
    ylabel('debug code')

%     figure(11)
%     plot(Time, TsStatus)
%     grid on
%     grid minor
%     title('Ts status')
%     xlabel('time [s]')
%     ylabel('Ts status')
end
