close all
clear all


DataqFileName = 'ConstantSpeedMoves_2.WDQ';  %Dataq binary file
LogFileName = '2021.03.03T16.04.41.mat';
TargetPositionFileName = ' ';



AlignTestData(TargetPositionFileName,DataqFileName,LogFileName,['AlignedData_' TargetPositionFileName]);

load(['AlignedData_' TargetPositionFileName]);
figure('Position',[   1537          88         990        1410]);
ah1 = subplot(4,1,1);
plot(Time_DateNum,TM_TargetProfile);
hold all
plot(Time_DateNum,TM_Range);
plot(Time_DateNum,SC_Range);
grid on;
legend('TargetProfileRange','TestMachineRange','SC\_Range');
title(['Target and Measured Range Profiles, aligned with SpringController origin: filename = ' LogFileName]);
datetick('x');

ah2 = subplot(4,1,2);
hold all
plot(Time_DateNum,SC_LowerPressure);
hold all
plot(Time_DateNum,SC_UpperPressure);
ylabel('psia');
grid on;
legend('Lower Spring Pressure (psia)','Upper Spring Pressure (psia)');
title(['Pneumatic Spring Pressures: filename = ' LogFileName]);
datetick('x');

ah3 = subplot(4,1,3);
plot(Time_DateNum,TM_LowerHydPressure);
hold all
plot(Time_DateNum,TM_UpperHydPressure);
grid on;
datetick('x');
ylabel('Hydraulic Pressure (psig)');
legend('Lower Hyd Pressure','Upper Hyd Pressure');

A_hPiston = 8.87/(2.54^2);  %inches^2
A_pPiston_Upper = 126.7/(2.54^2);  %inches^2
A_pPiston_Lower = 115.2/(2.54^2);  %inches^2
A_Rod = A_pPiston_Upper-A_pPiston_Lower;

PneumaticForce = SC_LowerPressure*A_pPiston_Lower-SC_UpperPressure*A_pPiston_Upper;
HydraulicForce = A_hPiston*(TM_LowerHydPressure-TM_UpperHydPressure);
FrictionForce = TM_Force-PneumaticForce-HydraulicForce;


ah4 = subplot(4,1,4);
plot(Time_DateNum,TM_Force)
hold all
plot(Time_DateNum,PneumaticForce);
plot(Time_DateNum,HydraulicForce);
plot(Time_DateNum,FrictionForce);
grid on;
datetick('x');
ylabel('Force (lbs)');
legend('- Test Machine Load Cell','Pneumatic Force','HydraulicForce','Friction Force');

linkaxes([ah1 ah2 ah3 ah4],'x');

figure('Position',[   1537          88         990        1410]);

ah1 = subplot(4,1,1);
plot(TM_Range,TM_Force);
grid on;
xlabel('Rod Positon (inches)');
ylabel('lbs')
title(['Force versus Position Results: filename = ' LogFileName]);
legend('Measured Rod Force')

ah2 = subplot(4,1,2);
plot(TM_Range,PneumaticForce);
grid on;
xlabel('Rod Positon (inches)');
ylabel('lbs')
legend('Pneumatic Spring Force')

ah3 = subplot(4,1,3);
plot(TM_Range,HydraulicForce);
grid on;
xlabel('Rod Positon (inches)');
ylabel('lbs')
legend('Hydraulic  Force')

ah4 = subplot(4,1,4);
plot(TM_Range,FrictionForce);
grid on;
ylabel('lbs')

xlabel('Rod Positon (inches)');
legend('Friction Force (measured-pneumatic-hydraulic')
linkaxes([ah1 ah2 ah3 ah4],'x');




TM_Speeds = [0 diff(TM_Range)./diff(24*3600*Time_DateNum)];
windowSize = 10;
epsilon = .4;
speeds = [];
for i = [.5 1:24]
    speeds = [speeds i -i];
end
[idx] = PickSpeeds(TM_Speeds,windowSize,epsilon,speeds);    %Automatic constant speed picking


figure
ah1 = subplot(2,1,1)
plot(Time_DateNum,TM_Range);
ah2 = subplot(2,1,2)
plot(Time_DateNum,TM_Speeds);
hold all
for i = 1:length(idx)
    plot(Time_DateNum(idx{i}),TM_Speeds(idx{i}),'rx');
end
linkaxes([ah1 ah2],'x');



for i =  1:length(idx)
    Mean_TM_Speed(i) = mean(TM_Speeds(idx{i}));
    Mean_TM_Force(i) = mean(TM_Force(idx{i}));
    
    Mean_PneumaticForce(i) = mean(PneumaticForce(idx{i}));
    Mean_HydraulicForce(i) = mean(HydraulicForce(idx{i}));
    Mean_FrictionForce(i) = mean(FrictionForce(idx{i}));
end



figure('Position',[   1537          88         990        1410]);


ah1 = subplot(4,1,1);
plot(Mean_TM_Speed,Mean_TM_Force,'x');
grid on;
xlabel('Mean Rod Speed (inches per second)');
ylabel('lbs')
title(['Mean Force Results : filename = ' LogFileName]);
legend('Mean Measured Rod Force','Location','NorthWest')

ah2 = subplot(4,1,2);
plot(Mean_TM_Speed,Mean_PneumaticForce,'x');
grid on;
xlabel('Mean Rod Speed (inches per second)');
ylabel('lbs')
legend('Mean Pneumatic Spring Force','Location','NorthWest')

ah3 = subplot(4,1,3);
plot(Mean_TM_Speed,Mean_HydraulicForce,'x');
grid on;
xlabel('Mean Rod Speed (inches per second)');
ylabel('lbs')
legend('Mean Hydraulic  Force','Location','NorthWest')

ah4 = subplot(4,1,4);
plot(Mean_TM_Speed,Mean_FrictionForce,'x');
grid on;
ylabel('lbs')

xlabel('Mean Rod Speed (inches per second)');
legend('Mean Friction Force (measured-pneumatic-hydraulic','Location','NorthWest')
linkaxes([ah1 ah2 ah3 ah4],'x');

