function mcb_getCharacteristicsAcim(acim,inverter,varargin)
% mcb_getCharacteristicsAcim Computes the drive and motor characteristics
% for ACIM
%
%   mcb_getCharacteristicsAcim(acim,inverter) returns the following
%   characteristics for the given ACIM:
%   - Torque vs Speed
%   - Power vs Speed
%   - Idq vs Speed
%   - Ipeak vs Speed
%   and  Motor characteristics speed and power w.r.t speed
%
%   The mcb_getCharacteristicsAcim function accepts an acim object, an inverter object

% Function      :   mcb_getCharacteristicsAcim
% Description   :   Compute Torque-Speed & Other curves for ACIM
% File name     :   mcb_getCharacteristicsAcim.m
% Inputs        :   acim object & inverter object

% Copyright 2021 The MathWorks, Inc.

%%  Torque & power Characteristics of motor
% Thevenin equivalent of ACIM T circuit representation is used to compute the
% motor characteristics 

Zeq=(acim.Rs + (1i*2*pi*acim.F_rated*acim.Lls))*(1i*2*pi*acim.F_rated*acim.Lm)/(acim.Rs + (1i*2*pi*acim.F_rated*acim.Lls) + (1i*2*pi*acim.F_rated*acim.Lm));
Req=real(Zeq);
Xeq = (imag(Zeq));
Xr = (2*pi*acim.F_rated*acim.Llr);
V1= ((inverter.V_dc/sqrt(3)) * (2*pi*acim.F_rated*acim.Lm)/sqrt(acim.Rs^2+((2*pi*acim.F_rated*acim.Lls) + (2*pi*acim.F_rated*acim.Lm))^2));
Slip = 1:-0.01:0; % consider the slip range in motoring region 
plot_speed = ((1-Slip)*acim.N_base); % speed for plotting the data

% Initialize the variables
T_ckt = ones(1,length(Slip));
P_ckt = ones(1,length(Slip));
IStator= ones(1,length(Slip));

for ite = 1:length(Slip)

    T_ckt(ite) = 3*(acim.Rr/(Slip(ite)*acim.N_base*2*pi/60))*(V1/sqrt(2))^2/( (Req+(acim.Rr/Slip(ite)))^2+ (Xeq+Xr)^2 );
    P_ckt(ite) =T_ckt(ite) * (1-Slip(ite))*acim.N_base*(2*pi/60);

    IStator(ite) = abs(V1)/sqrt ( (Req +(acim.Rr/Slip(ite)))^2 + (Xeq + (2*pi*acim.F_rated*acim.Llr))^2 ) ;

end


% Create a figure for Motor characteristics  
figure('Name',['ACIM : Motor Characteristics of ' acim.model],'NumberTitle','off');


subplot(1,2,1)
plot(plot_speed,T_ckt,'b');
title('Torque-Speed Characteristics');
xlabel('Speed (rpm)');
ylabel('Torque (Nm)');
grid on;

subplot(1,2,2)
plot(((1-Slip)*acim.N_base),P_ckt,'g');
title('Power -Speed Characteristics');
xlabel('Speed (rpm)');
ylabel('Power (W)');
grid on;

%% Drive characteristics of motor
% plots power and torque characteristics of motor drive

P_max = 2*pi*acim.N_rated*acim.T_rated/60;
N = 1:2*acim.N_base;
T_max  = P_max./((2*pi/60).*N);
T_max(1:acim.N_rated) = acim.T_rated*ones(1,acim.N_rated); % Constant torque region
Power =  2*pi.*N.*T_max/60;

figure('Name',['ACIM: Drive Characteristics of ' acim.model] ,'NumberTitle','off');

subplot(2,2,1)
plot(N,T_max,'b');
title('Torque-Speed Characteristics ');
xlabel('Speed (rpm)');
ylabel('Torque (Nm)');
grid on;

subplot(2,2,2)
plot(N,Power,'g');
title('Power -Speed Characteristics ');
xlabel('Speed (rpm)');
ylabel('Power (W)');
grid on;

Omega_ele     = acim.p*2*pi*N/60;
gain2 = (((3/2)*acim.p*(acim.Lm^2/acim.Lr)*1));



Id=  (acim.Id0 * 2*pi*acim.p*acim.N_rated/60)./(Omega_ele);
Id(1:acim.N_rated) = ones(1,acim.N_rated)*acim.Id0;


Iq = sqrt((acim.I_rated)^2 - Id.^2);
Iq_limit = (T_max/gain2)./(Id);


index       = length(find((Iq) < Iq_limit));
if any(index)
Iq_limit(index) = Iq(index);
end

I_peak =  sqrt(Id.^2 + Iq_limit.^2);

subplot(2,2,3)
plot(  N, Id,'g', N, Iq_limit,'b');
title('I_{dq} Vs Speed');
xlabel('Speed (rpm)');
ylabel('I_{dq} (A)');
legend('I_d','I_q');
grid on;

subplot(2,2,4)
plot(N,I_peak,'k',N,acim.I_rated*ones(1,length(N)),'r-.');
title('I_{peak} Vs Speed');
xlabel('Speed (rpm)');
ylabel('I_{peak} (A)');
legend('I_{peak}','I_{rated}');
scale = axis;
axis([scale(1:2) min(I_peak)-1 max(I_peak)+1])
grid on;


end