close all
clear all

hard_copy = 0;

ppath = 'R00000/';

listing = dir([ppath '*.mat']);


for i = 51
i
   
filename = listing(i).name

load([ppath filename]);

PistonDistance = RangeFinderCalibration(PistonDistance/100);
LoadDumpCurrent = LoadDumpCurrent/10;

for j = 1:length(PistonDistance)-6
  PistonDistance(j) = PistonDistance(j+6);
end

windowSize = 8;
PistonDistance_x = filtfilt(ones(1,windowSize)/windowSize,1,PistonDistance);
windowSize = 8;
PistonDistance_xdot = filtfilt(ones(1,windowSize)/windowSize,1,[0 diff(PistonDistance_x)./diff(time)]);




%Compute piston postion based on pressure and temperature
A = ones(length(PistonDistance),3);
A(:,2) = -1./UpperSpringPressure;
A(:,3) = -UpperSpringTemperature./UpperSpringPressure;
coeffs = A\PistonDistance';
zz_upper = A*coeffs;
A = ones(length(PistonDistance),3);
A(:,2) = -1./LowerSpringPressure;
A(:,3) = -LowerSpringTemperature./LowerSpringPressure;
coeffs = A\PistonDistance';
zz_lower = A*coeffs;

zz = filtfilt(ones(3,1)/3,1,(zz_upper+zz_lower)/2);

z = zz';
zdot = [0 diff(z)./diff(time)];












Au = 0.25*pi*(5^2);  %in^2
Al = 0.25*pi*(5^2-1.5^2);  %in^2


SpringForce = Al*LowerSpringPressure-Au*UpperSpringPressure;


figure('Position',[520   304   744   794]);
subplot(4,1,1)
plot(39.4*PistonDistance_x+4.1,SpringForce,'-x');
legend(['Average Spring Force = ' num2str(mean(SpringForce),4) ' lbs']);
axis([0 80 1200 3000]);
title([ppath filename]);
xlabel('Ram Position (in)');
ylabel('Spring Force (lbs)');

subplot(4,1,2)
plot(39.4*PistonDistance_x+4.1,LoadDumpCurrent.*LoadDumpVoltage);
legend(['Average Power = ' num2str(mean(LoadDumpCurrent.*LoadDumpVoltage),4) ' W']);
xlabel('Ram Position (in)');
ylabel('Load Dump Power (W)');

subplot(4,1,3)
plot(39.4*PistonDistance_x+4.1,LoadCellForce);
legend(['Average Force = ' num2str(mean(LoadCellForce),4) ' lbs']);
xlabel('Ram Position (in)');
ylabel('Load Cell Force (lbs)');

subplot(4,1,4)
plot(39.4*PistonDistance_x+4.1,LoadCellForce-SpringForce);
legend(['Average Force = ' num2str(mean(LoadCellForce-SpringForce),4) ' lbs']);
xlabel('Ram Position (in)');
ylabel('LoadCellForce minus SpringForce (lbs)');
print('-dpdf',[ppath filename]);


close all

end