% This script plots out motor plots for two different voltages
% given input data for I_noload, W_noload (in rpm) and I_stall
% at two different voltages
% Modified 1.25-2011 -mrc
%% Need to check this still works for 2016 Matlab... Plot
%% might not be quite the same.

clear all;  close all;  clc;

RPM_TO_RADPERSEC = 0.1047;

%INPUT no load conditions for 6V
V1 = 6;
I_noLoad_1 = .043;
W_noLoad_1 = 2890*RPM_TO_RADPERSEC;
I_Stall_1 = .610;

%INPUT no load conditions for 12V
V2 = 12;
I_noLoad_2 = .052;
W_noLoad_2 = 5970*RPM_TO_RADPERSEC;
I_Stall_2 = 1.10;

%Calculated resistance
R = V1/I_Stall_1;  % or V2/I_Stall_2

%calculated motor constants
K1 = (V1 - I_noLoad_1*R)/W_noLoad_1;
K2 = (V2 - I_noLoad_2*R)/W_noLoad_2;

%calculated Tf
T_f_1 = K1*I_noLoad_1;
T_f_2 = K2*I_noLoad_2;

%calculated stall torque
T_Stall_1 = (K1*V1)/R;
T_Stall_2 = (K1*V2)/R;

%calculated step size for plots
N = 100;
Tstep1 = (T_Stall_1-T_f_1)/N;
Tstep2 = (T_Stall_2-T_f_2)/N;
Wstep1 = (W_noLoad_1 - 0)/N;
Wstep2 = (W_noLoad_2 - 0)/N;
Istep1 = (I_Stall_1-I_noLoad_1)/N;
Istep2 = (I_Stall_2-I_noLoad_2)/N;

%torque values
Torque1 = 0:Tstep1:(T_Stall_1 - T_f_1);
Torque2 = 0:Tstep2:(T_Stall_2 - T_f_2);

%omega values
W1 = W_noLoad_1:-Wstep1:0;
W2 = W_noLoad_2:-Wstep2:0;

%current values
I1 = I_noLoad_1:Istep1:I_Stall_1;
I2 = I_noLoad_2:Istep2:I_Stall_2;

%calculated power
Pout1 = Torque1.*W1;
Pout2 = Torque2.*W2;

%calculated efficiency
Efficiency1 = (1-I_noLoad_1./I1).*(1-I1./I_Stall_1);
Efficiency2 = (1-I_noLoad_2./I2).*(1-I2./I_Stall_2);

%Plot 1: Torque-Speed
figure(1)
clf
plot(W1,Torque1,'linewidth',2,W2,Torque2,'r','linewidth',2)
grid on
title('Torque-Speed Curve')
xlabel ('speed (rad/s)','fontsize',14)
ylabel ('torque (Nm)','fontsize',14)

%Plot 2: Power-Current
figure(2)
clf
plot(I1,Pout1,'linewidth',2,I2,Pout2,'r','linewidth',2)
grid on
title('Power-Current Curve')
xlabel('Current [amps]','fontsize',14)
ylabel('Power   [W]''fontsize',14)

figure(3)
plot(Pout1,W1,'linewidth',2,Pout2,W2,'r','linewidth',2)
xlabel('Angular Velocity [rad/s]')
ylabel('Power   [W]')
title('Power-Speed Curve')
legend('6V','12V','location','Best')
grid on

%Plot 2: Power-Current
figure(2)
clf
plot(I1,Pout1,'linewidth',2,I2,Pout2,'r','linewidth',2)
grid on
title('Power-Current Curve')
xlabel('Current [amps]','fontsize',14)
ylabel('Power   [W]''fontsize',14)

%Plot 3: Power-Speed
figure(3)
plot(W1,Pout1,'linewidth',2,W2,Pout2,'r','linewidth',2)
grid on
xlabel('Angular Velocity [rad/s]')
ylabel('Power   [W]')
title('Power-Speed Curve')

%Plot 4: Efficiency-Current
figure(4)
plot(I1,Efficiency1,'linewidth',2,I2,Efficiency2,'r','linewidth',2)
grid on
xlabel('Current [amps]')
ylabel('Efficiency  [ ]')
title('Efficiency-Current Curve')



