
from math import pi, sqrt
import matplotlib.pyplot as plt


# Parametres MCC
#===================
R1 = 2.0  # Ohm (resistance induit)
k1 = 1.4  # H   (coeff. intrinseque MCC)


# Essai à vide
#-----------------

# Mesures 
V1 = 200.0 # V DC
Ie1 = 1.0  # A
Ie2 = 2.5  # A
V2 = 191.0 # V AC

# Donnees
Gamma = 2.0 # N⋅m
p = 2       # paires de poles

# Calculs
I1 = Gamma/(k1*Ie1)
E1 = V1-R1*I1
Omega = E1/(k1*Ie1)
freq1 = Omega*p/(2*pi)
E2 = V2
k2 = abs(E2)/(Ie2*Omega)
freq = 50.0 # Hz
V1 = R1*I1 + 2*pi*freq/p*k1*Ie1

# Display
print(" ")
print("### Essai a vide ###")
print(" Vitesse de l'arbre : Omega =" , round(Omega*100)/100 , "rad/s, soit :" , round(Omega*30/pi), "tr/min")
print(" Frequence : f =" , round(freq1*100)/100 , "Hz")
print(" Coeff. intrinseque MS : k2 =" , round(k2*100)/100 )
print(" Tension induit MCC pour etre à 50 Hz : V1 =", round(V1*100)/100 , "V ")
print(" ")


# Essai en court-circuit
#-------------------------

# Mesures 
Ie2 = 0.8 # A
R2 = 0.2  # Ω
module_I2 = 10.14  # A 

# Donnees
Omega = 1500*pi/30
f = p*Omega/(2*pi)
module_E2 = k2*Ie2*Omega
# |I2| = |E2|/|R2+jL2ω|, soit :
L2w = sqrt((module_E2/module_I2)**2 - R2**2)
L2 = L2w/(2*pi*f)

# Display
print("### Essai en court-circuit ### ")
print(" Frequence : f =" , round(f*100)/100 , "Hz")
print(" FEM MS (module) : |E2| =",  round(module_E2*100)/100 , "V")
print(" Inductance synchrone : L2 =" , round(L2*1e4)/1e4 , "H")
print(" ")


# Essai en charge MCC
#-----------------------

V1 = 200.0 # V
Ie1 = 1.0  # A 
Gamma = 15.0   # N⋅m

I1 = Gamma/(k1*Ie1)
E1 = V1-R1*I1
Omega = E1/(k1*Ie1)
Pu1 = Gamma*Omega
P1 = V1*I1
eta1 = Pu1/P1

print("### Essai en charge MCC ###")
print(" Courant induit : I1 =", round(I1*100)/100 , "A")
print(" FEM : E1 =",  round(E1*100)/100 , "V")
print(" Vitesse : Omega =" , round(Omega*100)/100 , "rad/s, soit :" , round(Omega*30/pi) , "tr/min")
print(" Puissance utile : Pu1 =" , round(Pu1*100)/100 , "W")
print(" Puissance electrique : P1 =" , round(P1*100)/100 , "W")
print(" Rendement : eta1 =" , round(eta1*1e4)/100 , "%")
print(" ")


# Essai en charge MS
#-----------------------

# Donnees
Rc = 50.0  # Ohm
Ie2 = 3.0  # A 
Omega = 1400*pi/30 

# Calculs
f = p*Omega/(2*pi)
module_E2 = k2*Ie2*Omega
L2w = L2*2*pi*f 
j = 1j
# |I2| = |E2|/|R2+jL2ω|, soit :
module_I2 = module_E2/abs(R2+j*L2w+Rc)
module_V2 = Rc*module_I2
PJ2 = 3*R2*module_I2**2
Pc = 3*Rc*module_I2**2
eta2 = Pc/(Pc+PJ2)
Pm2 = Pc+PJ2
Gamma = Pm2/Omega

print("### Essai en charge MS ###")
print(" Frequence : f =" , round(f*100)/100 , "Hz")
print(" FEM (module) : |E2| =", round(module_E2*100)/100 , "V")
print(" Courant statorique (module) : |I2| =" , round(module_I2*100)/100 , "A")
print(" Tension statorique (module) : |V2| =" , round(module_V2*100)/100 , "V")
print(" Pertes Joules : PJ2 =" , round(PJ2*100)/100 , "W")
print(" Puissance utile : Pc =" ,  round(Pc*100)/100 , "W")
print(" Rendement : eta2 =" , round(eta2*100)/100 , "%")
print(" Puissance mecanique : Pm2 =" ,  round(Pm2*100)/100 , "W")
print(" Couple : Gamma =" , round(Gamma*100)/100 , "N⋅m")
print(" ")


# Entrainement a couple impose
#-------------------------------

# Donnees
Rc = 50.0 # Ω
Ie2 = 3.0 # A 

Omega = 1000*pi/30
Gamma_target = 15 # N⋅m 
epsilon = 1e-6
Vitesse = []
Couple = []
Gamma = 0
compt = 0
maxiter = 1000
while ( (abs(Gamma-Gamma_target)/Gamma_target > epsilon) and (compt < maxiter) ):
    Vitesse += [Omega*30/pi]   # stockage pour plot
    compt += 1
    f = p*Omega/(2*pi)
    module_E2 = k2*Ie2*Omega
    L2w = L2*2*pi*f
    module_I2 = module_E2/abs(R2+j*L2w+Rc)
    module_V2 = Rc*module_I2
    PJ2 = 3*R2*module_I2**2
    Pc = 3*Rc*module_I2**2
    eta2 = Pc/(Pc+PJ2)
    Pm2 = Pc+PJ2
    Gamma = Pm2/Omega
    Couple += [Gamma]          # stockage pour plot
    Omega += (Gamma_target-Gamma)/Gamma_target*Omega    # update vitesse


print("### Entrainement a couple impose ### ")
print(" Vitesse : Omega =" , round(Omega*100)/100 , "rad/s, soit :" , round(Omega*30/pi*100)/100 , "tr/min")
print(" Frequence : f =" , round(f*100)/100 , "Hz")
print(" FEM MS (module) : |E2| =" , round(module_E2*100)/100 , "V")
print(" Courant statorique (module) : |I2| =" , round(module_I2*100)/100 , "A")
print(" Tension statorique (module) : |V2| =" , round(module_V2*100)/100,  "V")
print(" Pertes Joules MS : PJ2 =" , round(PJ2*100)/100 , "W")
print(" Puissance utile : Pc =" , round(Pc*100)/100 , "W")
print(" Rendement : eta2 =" , round(eta2*1e4)/100 , "%")
print(" Puissance mecanique : Pm2 =" , round(Pm2*100)/100 , "W")
print(" Couple : Gamma =" , round(Gamma*1e4)/1e4 , "N⋅m")
print(" Nombre d'iterations :", compt)
print(" ")


# Association MCC-MS
#----------------------

# Donnees
V1 = 200.0 # V
Ie1 = 1.0  # A 
Rc = 50.0  # Ω
Ie2 = 3.0  # A 

Omega = 1000*pi/30
Gamma1 = 0
Gamma2 = 1
Vitesse2 = []
Couple1 = []
Couple2 = []
deltaOmega = 250*pi/30    # 250 rpm (choisi +/- au pif)  
compt = 0
while ( (abs(Gamma1-Gamma2)/abs((Gamma1+Gamma2)/2) > epsilon) and (compt < maxiter) ):
    Vitesse2 += [Omega*30/pi]      # stockage pour plot
    compt += 1
    # Point fonctionnement MCC
    E1 = k1*Ie1*Omega
    I1 = (V1-E1)/R1
    Gamma1 = k1*Ie1*I1
    Couple1 += [Gamma1]            # stockage pour plot

    # Point fonctionnement MS
    f = p*Omega/(2*pi)
    module_E2 = k2*Ie2*Omega
    L2w = L2*2*pi*f 
    module_I2 = module_E2/abs(R2+j*L2w+Rc)
    module_V2 = Rc*module_I2
    PJ2 = 3*R2*module_I2**2
    Pc = 3*Rc*module_I2**2
    eta2 = Pc/(Pc+PJ2)
    Pm2 = Pc+PJ2
    Gamma2 = Pm2/Omega
    Couple2 += [Gamma2]           # stockage pour plot 
    Omega += (Gamma1-Gamma2)*2/(Gamma1+Gamma2)*deltaOmega  # update vitesse


print("### Association MCC - MS ###")
print(" Vitesse : Omega =" , round(Omega*100)/100, "rad/s, soit :" , round(Omega*30/pi*100)/100 , "tr/min")
print(" Couple MCC : Gamma1 =" , round(Gamma1*100)/100 , "N⋅m")
print(" Couple MS : Gamma2 =" ,  round(Gamma2*100)/100 , "N⋅m")
print(" Nombre d'iterations :", compt)
print(" ")


# Trace
#---------

plt.figure(figsize=(5, 8))
plt.subplot(211)
plt.plot(Vitesse, Couple, 'b-o', linewidth=2.0) 
plt.plot(Vitesse[len(Vitesse)-1], Couple[len(Couple)-1], 'ro', markersize=10.0) # valeur finale
plt.xlabel('Vitesse (en tr/min)')
plt.ylabel('Couple (en N⋅m)')
plt.title('Fonctionnement à couple imposé') 
plt.grid(True)

plt.subplot(212)
plt.plot(Vitesse2, Couple1, 'g-o', Vitesse2, Couple2, 'b-o', linewidth=2.0) 
plt.plot(Vitesse2[len(Vitesse2)-1], Couple2[len(Couple2)-1], 'ro', markersize=10.0) # valeur finale
plt.xlabel('Vitesse (en tr/min)')
plt.ylabel('Couple (en N⋅m)')
plt.title('Association MCC-MS') 
plt.grid(True)

plt.subplots_adjust(top=0.92, bottom=0.08, left=0.150, right=0.95, hspace=0.25, wspace=0.35)
plt.show()

