close all;
clear all;

speeds = [4:4:24]*.0254;  %ips
Ap = .25*pi*(2^2-1.5^2);
Ap = Ap*(.0254^2);
no_leak_rpms = Ap*speeds*60/(2*4.9/1000000);


MaxScale = 1.2;
MinScale = 0.5;

VV_Table = [0   0.1 68  128   175  205  285 350 400   500 700];
II_Table = [0   0.1 6.1 11.3 15.5 16.5 17.  17.5 18.2  20  22];

VV = 0:50:max(VV_Table);
II = interp1(VV_Table,II_Table,VV);

ILim = 0.2*VV-80;
idx = find(ILim<0);
ILim(idx) = NaN;


Rw = 0.93;
Kv = 60.1/1000;

RPM = (VV+II*Rw)/(1.35*Kv);

RPMmax = (VV+MaxScale*II*Rw)/(1.35*Kv);
RPMmin = (VV+MinScale*II*Rw)/(1.35*Kv);
RPMlim = (VV+ILim*Rw)/(1.35*Kv);

figure('Position',[1000 66 917 1432],'PaperSize',[11 17],'PaperPosition',[.5 .5 10.5 16.5]);
subplot(2,1,1)
plot(VV,II,VV,MaxScale*II,VV,MinScale*II,VV,ILim,'k')
ylim([0 30]);
xlabel('Volts');
ylabel('Amps');
legend('Nominal','Max','Min','Lim','Location','SouthEast');
subplot(2,1,2)
plot(VV,VV./II,VV,VV./(MaxScale*II),VV,VV./(MinScale*II),VV,VV./ILim,'k');
ylim([0 70]);
xlabel('Volts');
ylabel('Reff');


print('-dpdf','PowerVsVoltage.pdf');


Pmin = MinScale*II.*VV;
Pmax = MaxScale*II.*VV;
P = II.*VV;
PLim = ILim.*VV;



figure('Position',[1000 66 917 1432],'PaperSize',[11 17],'PaperPosition',[.5 .5 10.5 16.5]);
subplot(4,1,1)
plot(RPM,P,RPMmax,Pmax,RPMmin,Pmin,RPMlim,PLim,'k')
ylim([0 20000]);
xlabel('RPM')
ylabel('Power (W)');
legend('Nominal','Max','Min','Location','NorthWest');

subplot(4,1,2)
plot(RPM,P./RPM,RPMmax,Pmax./RPMmax,RPMmin,Pmin./RPMmin,RPMlim,PLim./RPMlim,'k')
ylim([0 3]);
xlabel('RPM');
ylabel('W/RPM');

subplot(4,1,3)
plot(RPM,II,RPMmax,MaxScale*II,RPMmin,MinScale*II,RPMlim,ILim,'k')
xlabel('RPM');
ylabel('Current (A)');
ylim([0 30]);

subplot(4,1,4)
plot(RPM,VV,RPMmax,VV,RPMmin,VV,RPMlim,VV,'k')
xlabel('RPM');
ylabel('Volts');

print('-dpdf','PowerVsRPM.pdf');

save('PowerVsRPM','RPM','Pmin','P','Pmax');

