####################################################################################################

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

####################################################################################################

# V A Hz
data_unloaded = (
    (12.1, 0.56,  1.4),
    (14.0, 0.56,  3.1),
    (15.0, 0.56,  3.7),
    (16.0, 0.58,  4.5),
    (20.0, 0.63,  6.9),
    (22.5, 0.65,  8.5),
    (25.0, 0.65, 10.3),
    (32.0, 0.68, 14.5),
    (32.5, 0.65, 16.6),
    (40.0, 0.70, 20.8),
    (50.0, 0.72, 28.0),
    (63.0, 0.73, 38.0),
)

data_wheel = (
    (24.0, 1.09,  1.7),
    (28.0, 1.17,  2.6),
    (30.0, 1.20,  3.0),
    (30.0, 1.04,  3.8),
    (35.0, 1.28,  4.0),
    (40.0, 1.34,  5.1),
    (40.0, 1.30,  5.3),
    (40.0, 1.16,  6.1),
    (45.0, 1.36,  6.5),
    (50.0, 1.41,  7.6),
    (50.0, 1.30,  8.3),
    (55.0, 1.44,  8.8),
    (60.0, 1.48,  9.8),
    (60.0, 1.44, 10.1),
    (61.6, 1.45, 10.3),
    (65.0, 1.45, 11.9),
    (70.0, 1.58, 11.9),
    (70.0, 1.54, 12.0),
    (75.0, 1.58, 13.3),
    (80.0, 1.65, 14.4),
    (80.0, 1.60, 14.3),
)

Dmotor = 45 # Fixme: check
Dmachine = 75

Rbelt = Dmotor / Dmachine # = 0.6 # Fixme: R = 0.6 / 2 ???

CVmotor = 1/6 # 1 CV = 735.5 W et 1 HP = 745.7 W
Wmotor = 735.5 * CVmotor # = 123 W

Un_MOTOR = 220
RPM_MAX = 2200 # stitch/mm

####################################################################################################

class DataUnloaded:

    data = np.array(data_unloaded)
    U_array = data[:,0]
    I_array = data[:,1]
    frequency_array = data[:,2]
    rpm_array = frequency_array * 60

    U_slope, U_cst = np.polyfit(U_array, rpm_array, 1)
    I_slope, I_cst = np.polyfit(U_array, I_array, 1)

class DataWheel:

    data = np.array(data_wheel)
    U_array = data[:,0]
    I_array = data[:,1]
    frequency_array = data[:,2]
    rpm_array = frequency_array * 60

    U_slope, U_cst = np.polyfit(U_array, rpm_array, 1)
    I_slope, I_cst = np.polyfit(U_array, I_array, 1)

####################################################################################################

def axis1to2(x):
    return x

def axis2to1(x):
    return x

####################################################################################################

fig, ax = plt.subplots(constrained_layout=True)

# x_array = np.array((0, Un_MOTOR))
# slope = RPM_MAX / Un_MOTOR
# ax.plot(x_array, (x_array - 30) * slope , '--b', label='')
ax.plot((30, Un_MOTOR), (1*60, RPM_MAX), '--b', label='expected RPM on AC line')

ax.plot(DataUnloaded.U_array, DataUnloaded.I_array*1000, 'o', label=r'I unloaded')
ax.plot(DataUnloaded.U_array, DataUnloaded.rpm_array, 'o', label=r'RPM unloaded')
x_array = np.arange(10, 90, 10)
# ax.plot(DataUnloaded.U_array, DataUnloaded.U_slope*DataUnloaded.U_array + DataUnloaded.U_cst, '--k')
# ax.plot(DataUnloaded.U_array, (DataUnloaded.I_slope*DataUnloaded.U_array + DataUnloaded.I_cst)*1000, '--k')
ax.plot(x_array, DataUnloaded.U_slope*x_array + DataUnloaded.U_cst, '--k')
ax.plot(x_array, (DataUnloaded.I_slope*x_array + DataUnloaded.I_cst)*1000, '--r')

ax.plot(DataWheel.U_array, DataWheel.I_array*1000, 'o', label=r'I wheel')
ax.plot(DataWheel.U_array, DataWheel.rpm_array, 'o', label=r'RPM wheel')
x_array = np.arange(20, 250, 10)
# ax.plot(DataWheel.U_array, DataWheel.U_slope*DataWheel.U_array + DataWheel.U_cst, '--k')
ax.plot(x_array, DataWheel.U_slope*x_array + DataWheel.U_cst, '--k')
ax.plot(x_array, (DataWheel.I_slope*x_array + DataWheel.I_cst)*1000, '--r')

ax.set_xlim([0, 250])
ax.set_ylim([0, 2500])
ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
ax.yaxis.set_major_locator(ticker.MultipleLocator(500))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(1))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(100))
ax.grid(True, color='k', linestyle='-', linewidth=0.5)
ax.xaxis.grid(True, which='minor')
ax.yaxis.grid(True, which='minor')

ax.set_title(r'DC Singer Motor')
ax.set_xlabel(r'$U\ [V]$')
ax.set_ylabel(r'$RPM\ [min^{-1}]$')
yaxis2 = ax.secondary_yaxis('right', functions=(axis1to2, axis2to1))
yaxis2.set_ylabel(r'$I\ [mA]$')
ax.legend()

ax.axvspan(15, 20, alpha=0.5, color='red')
ax.text(15, 1510, r'$\mathrm{Start}\ U > 18\,V$', fontsize=12)

# print('poly:', U_slope, U_cst)
ax.text(41.5, 1110, r'RPM unloaded = {:.1f} U + {:.1f}'.format((DataUnloaded.U_slope), DataUnloaded.U_cst), fontsize=14)

ax.text(62.5, 510, r'RPM wheel = {:.1f} U + {:.1f}'.format((DataWheel.U_slope), DataWheel.U_cst), fontsize=14)

ax.text(62.5, 410, r'RPM wheel/unloaded = {:.1f}'.format(DataWheel.U_slope/DataUnloaded.U_slope), fontsize=14)

ax.axhline(y=1000)
ax.axhline(y=1600)
ax.axhline(y=RPM_MAX)
ax.text(10, RPM_MAX + 10, r'RPM max 2200', fontsize=14)
# ax.axhline(y=5500) # DDL-8700 JUKI 5,500 point/mm

plt.show()
