%% load data


clc;
clearvars;
close all;
data = readtable("CurrentVsPressureEVLSERVO1_MAXON_EC60.xlsx");

%

psi_to_dbar = 0.689476;

i_500 = data.x500RPM_MeanOfShuntCurrent_A_(:);
p_500 = data.x500RPM_Pressure_PSI_(:) * psi_to_dbar;
v_500 = data.x500RPM_Votage_V_(:);

i_1000 = data.x1000RPM_MeanOfShuntCurrent_A_(:);
p_1000 = data.x1000RPM_Pressure_PSI_(:) * psi_to_dbar;
v_1000 = data.x1000RPM_Votage_V_(:);

i_2000 = data.x2000RPM_MeanOfShuntCurrent_A_(:);
p_2000 = data.x2000RPM_Pressure_PSI_(:) * psi_to_dbar;
v_2000 = data.x2000RPM_Votage_V_(:);

i_elmo = data.Elmo_I_A_(1:8);
p_elmo = data.Elmo_Pressure(1:8) * psi_to_dbar;
v_elmo = data.Elmo_V(1:8);

% Power
pwr_500 = i_500 .* v_500;
pwr_1000 = i_1000 .* v_1000;
pwr_2000 = i_2000 .* v_2000;
pwr_elmo = i_elmo .* v_elmo;

%
%% Plot 1


figure('Color', 'w');

plot(i_500, p_500, 'LineWidth', 2, 'DisplayName', '500 RPM');
hold on;
plot(i_1000, p_1000, 'Color', [0.5, 0, 0.5], 'LineWidth', 2, 'DisplayName', '1000 RPM');
%plot(i_1000, p_1000, 'LineWidth', 2, 'DisplayName', '1000 RPM');
plot(i_2000, p_2000, 'LineWidth', 2, 'DisplayName', '2000 RPM');
plot(i_elmo, p_elmo, '--', 'LineWidth', 2, 'DisplayName', 'Elmo at 1000 RPM');

grid on;
box on;

legend;

xlabel('Current (A)', 'FontSize', 14, 'FontWeight', 'bold'); % Should we look at power insted? It would give a nicer slope
ylabel('Pressure (dBar)', 'FontSize', 14, 'FontWeight', 'bold'); % Changed from PSI to dBar

title('Pressure vs Current at 28.8V', 'FontSize', 16, 'FontWeight', 'bold');

legend('Location', 'southeast', 'FontSize', 10);

set(gca, 'FontSize', 12, 'LineWidth', 1.5);

hold off;

%
%% Plot 2

fig = figure('Color', 'w');
fig.Position(3) = fig.Position(3) * 1.2;

plot(p_500, pwr_500, 'LineWidth', 1, 'DisplayName', '500 RPM', ...
    'Marker', 'x', 'MarkerSize', 4);
hold on;

plot(p_1000, pwr_1000, 'Color', [0.5, 0, 0.5], 'LineWidth', 1, ...
    'DisplayName', '1000 RPM', 'Marker', 'x', 'MarkerSize', 4);

plot(p_2000, pwr_2000, 'LineWidth', 1, 'DisplayName', '2000 RPM', ...
    'Marker', 'x', 'MarkerSize', 4);

plot(p_elmo, pwr_elmo, '--', 'LineWidth', 1, 'DisplayName', 'Elmo at 1000 RPM', ...
    'Marker', 'o', 'MarkerSize', 3);

grid on;

legend('Location', 'northwest', 'FontSize', 10);

xlabel('Pressure (dBar)', 'FontSize', 14, 'FontWeight', 'bold');
ylabel('Power (W)', 'FontSize', 14, 'FontWeight', 'bold');
title('Power vs Pressure at 28.8V', 'FontSize', 16, 'FontWeight', 'bold');

set(gca, 'FontSize', 12, 'LineWidth', 1);
hold off;

%



%

% target pressures (dBar)
target_pressures = [100, 200, 350];

pwr_elmo_interp = interp1(p_elmo, pwr_elmo, target_pressures, 'linear');
pwr_1000_interp = interp1(p_1000, pwr_1000, target_pressures, 'linear');

efficiency_increase = (pwr_elmo_interp - pwr_1000_interp) ./ pwr_elmo_interp * 100;

fprintf('\nEfficiency increase (1000 RPM vs Elmo) at target pressures:\n');
for i = 1:length(target_pressures)
    fprintf('At %d dBar: Elmo = %.2f W, 1000 RPM = %.2f W → %.1f%% ↑ efficiency\n', ...
        target_pressures(i), ...
        pwr_elmo_interp(i), ...
        pwr_1000_interp(i), ...
        efficiency_increase(i));
end