figure(1);
%clear
color = 'rgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbkrgbk';
%MaxonEC30_272770_jgb

for gg = 1:20
    GearRatio = 1*1.01^(gg-1);
    GearEff = NaN;
    oil = 1;
    for iii = 1:60,
        speed = iii*0.02;
        OutputTorqueIn = 0.43047*speed^2 * 9/13;   % 80% propellor efficiency and 300 RPM at 1 m/s (drop 
        OutputOmegaIn = 31.42*speed;        % 300 RPM at 1 m/s
        
        % For plotting, store the speed and gear ration matrices..
        VehicleSpeed(gg, iii) = speed;
        TestGearRatio(gg, iii) = GearRatio;
        [OutputOmega(gg, iii), Pout(gg, iii), PViscous(gg, iii), PSeal(gg, iii), PGear(gg, iii), PMotorFriction(gg, iii), eff(gg, iii), VoltageIn(gg, iii), Amps(gg, iii), OutputTorque(gg, iii), PowerOhmic(gg, iii), TempMotor(gg, iii), Convergence(gg, iii)] = MotorCalc(OutputTorqueIn, OutputOmegaIn, GearRatio, GearEff, oil, MotorConstants);
    end;
end

figure(1); 
subplot(2, 2, 1); 
hold off;
[C,h] = contour(VehicleSpeed, TestGearRatio, eff); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
title('Efficiency');
xlabel('Vehicle Speed (m/s)'); ylabel('Gear Ratio');

%figure(2);
subplot(2, 2, 2); 
hold off;
[C,h] = contour(VehicleSpeed, TestGearRatio, VoltageIn, 'k'); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
hold on;
[C,h] = contour(VehicleSpeed, TestGearRatio, TempMotor, [5 10 20 40 80 160], 'r'); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
xlabel('Vehicle Speed (m/s)'); ylabel('Gear Ratio');
title('Voltage(k)/Motor Temperature(r)')
hold off;

%figure(3);
subplot(2, 2, 3); 
hold off;
[C,h] = contour(VehicleSpeed, TestGearRatio, (PowerOhmic)./(VoltageIn.*Amps));%, [0.1 0.2 0.4 0.8]); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
title('Electrical Losses/Electrical Power');
xlabel('Vehicle Speed (m/s)'); ylabel('Gear Ratio');

%figure(4);
subplot(2, 2, 4); 
hold off;
[C,h] = contour(VehicleSpeed, TestGearRatio, PViscous./Pout, 'k'); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
hold on;
[C,h] = contour(VehicleSpeed, TestGearRatio, (PSeal+PMotorFriction)./Pout, [0.125 0.25 0.5 1 2 4], 'g'); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
[C,h] = contour(VehicleSpeed, TestGearRatio, PGear./Pout, 'r'); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
title('PViscous(k)/PSeal+PMotorFriction(g)/PGear(r) in units of Pout');
xlabel('Vehicle Speed (m/s)'); ylabel('Gear Ratio');
hold off;

figure(9); 
hold off;
[C,h] = contour(VehicleSpeed, TestGearRatio, eff); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
title('Efficiency');
xlabel('Vehicle Speed (m/s)'); ylabel('Gear Ratio');


figure(10); 
%subplot(2, 2, 1); 
hold off;
meshc(VehicleSpeed, TestGearRatio, eff); 
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
title('Efficiency');
xlabel('Vehicle Speed (m/s)'); ylabel('Gear Ratio');
