Commit 29464337 authored by Tristan Renon's avatar Tristan Renon 💬
Browse files

Add some tkinter stuff... struggling on making a .exe

parent 569b5cb8
Prog/Python/motorPhaser/.idea/ Prog/Python/motorPhaser/.idea/
Prog/Python/motorPhaser/__pycache__/ Prog/Python/motorPhaser/__pycache__/
Prog/Python/motorPhaser/venv/ Prog/Python/motorPhaser/venv/
\ No newline at end of file Prog/Python/motorPhaser/build/
Prog/Python/motorPhaser/dist/
\ No newline at end of file
...@@ -7,6 +7,7 @@ import generateSinLib as gsl ...@@ -7,6 +7,7 @@ import generateSinLib as gsl
nSamples = 50 nSamples = 50
inputNPhases = 5 inputNPhases = 5
# inputPhaseAmplitude = (0, 1, 1, 1, 1) # inputPhaseAmplitude = (0, 1, 1, 1, 1)
inputPhaseAmplitude = (0, 1.382, 1.382, 1.382, 1.382) inputPhaseAmplitude = (0, 1.382, 1.382, 1.382, 1.382)
inputPhaseShiftMec = (0, 72, 72*2, 72*3, 72*4) inputPhaseShiftMec = (0, 72, 72*2, 72*3, 72*4)
...@@ -15,13 +16,20 @@ inputPhaseShiftReg = (0, 36, 180-36, 180+36, 360-36) ...@@ -15,13 +16,20 @@ inputPhaseShiftReg = (0, 36, 180-36, 180+36, 360-36)
# generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin = gsl.generateSin(inputNPhases, nSamples=nSamples) # generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin = gsl.generateSin(inputNPhases, nSamples=nSamples)
generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin = gsl.generateSin(n=inputNPhases, phaseShiftReg=inputPhaseShiftReg, phaseAmplitude=inputPhaseAmplitude, phaseShiftMec=inputPhaseShiftMec, nSamples=nSamples) generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin = gsl.generateSin(n=inputNPhases, phaseShiftReg=inputPhaseShiftReg, phaseAmplitude=inputPhaseAmplitude, phaseShiftMec=inputPhaseShiftMec, nSamples=nSamples)
# generatedSin, nPhases, phaseShiftReg, phaseAmplitude, phaseShiftMec, xSin = gsl.generateSin(n=inputNPhases, phaseAmplitude=inputPhaseAmplitude, phaseShiftMec=inputPhaseShiftMec, nSamples=nSamples)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(13, 4)) fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(13, 4))
limit = max(phaseAmplitude) * 1.1 limit1 = inputNPhases / 2 * 1.1
ax3.set(xlim=(0, 2 * np.pi), ylim=(-limit, limit)) limit2 = max(phaseAmplitude) * 1.1
ax3.set(xlim=(0, 2 * np.pi), ylim=(-limit2, limit2))
actualAngles = list()
for j in range(nPhases): for j in range(nPhases):
ax3.plot(xSin, generatedSin[j]) ax3.plot(xSin, generatedSin[j])
actualAngles.append(phaseShiftMec[j] / 180 * np.pi)
vertical = ax3.axvline(x=xSin[0], color='black') vertical = ax3.axvline(x=xSin[0], color='black')
...@@ -30,9 +38,9 @@ def animate(i): ...@@ -30,9 +38,9 @@ def animate(i):
ax1.cla() ax1.cla()
ax2.cla() ax2.cla()
ax1.set(xlim=(-(inputNPhases + 1) / 2, (inputNPhases + 1) / 2), ax1.set(xlim=(-limit1, limit1),
ylim=(-(inputNPhases + 1) / 2, (inputNPhases + 1) / 2)) ylim=(-limit1, limit1))
ax2.set(xlim=(-limit, limit), ylim=(-limit, limit)) ax2.set(xlim=(-limit2, limit2), ylim=(-limit2, limit2))
sliding1 = [[0, 0], [0, 0]] sliding1 = [[0, 0], [0, 0]]
...@@ -41,9 +49,9 @@ def animate(i): ...@@ -41,9 +49,9 @@ def animate(i):
for j in range(nPhases): for j in range(nPhases):
# ------------------------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------------------------
# Calculations (Do it only one and reused after) # Calculations (Do it only once and reuse after)
actualExcitation = generatedSin[j][i] actualExcitation = generatedSin[j][i]
actualAngle = phaseShiftMec[j] / 180 * np.pi actualAngle = actualAngles[j]
actualSin = np.sin(actualAngle) actualSin = np.sin(actualAngle)
actualCos = np.cos(actualAngle) actualCos = np.cos(actualAngle)
actualSinGen = actualSin * actualExcitation actualSinGen = actualSin * actualExcitation
......
...@@ -2,23 +2,21 @@ import numpy as np ...@@ -2,23 +2,21 @@ import numpy as np
def generateSin(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None, nSamples=50): def generateSin(n=3, phaseShiftReg=None, phaseAmplitude=None, phaseShiftMec=None, nSamples=50):
# Generate things if user did not tell about them # Generate things if user did not tell about them
if phaseAmplitude is None: if nSamples is None:
nSamples = 50
if phaseAmplitude is None or len(phaseAmplitude) != n:
phaseAmplitude = np.ones(n) phaseAmplitude = np.ones(n)
if phaseShiftMec is None: if phaseShiftMec is None or len(phaseShiftMec) != n:
if n == 2: if n == 2:
phaseShiftMec=(0, 90) phaseShiftMec=(0, 90)
else: else:
phaseShiftMec = np.linspace(0, 360, n, endpoint=False) phaseShiftMec = np.linspace(0, 360, n, endpoint=False)
if phaseShiftReg is None: if phaseShiftReg is None or len(phaseShiftReg) != n:
phaseShiftReg = phaseShiftMec phaseShiftReg = phaseShiftMec
# Make sure people gave valid parameters
assert len(phaseShiftReg) == n, "phaseShiftReg is not the same size as Phases (n)"
assert len(phaseAmplitude) == n, "phaseAmplitude is not the same size as Phases (n)"
assert len(phaseShiftMec) == n, "phaseShiftMec is not the same size as Phases (n)"
# Generate x (0 to 2pi) # Generate x (0 to 2pi)
xSin = np.linspace(0, 2 * np.pi, nSamples) xSin = np.linspace(0, 2 * np.pi, nSamples)
ret = list() ret = list()
......
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#---------End of imports
from tkinter import Frame,Label,Entry,Button
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def Clear(self):
print("clear")
self.textAmplitude.delete(0, 'end')
self.textAmplitude.insert(0, "1.0")
self.textSpeed.delete(0, 'end')
self.textSpeed.insert(0, "1.0")
self.textPhases.delete(0, 'end')
self.textPhases.insert(0, "5")
self.textPhaseAmplitude.delete(0, 'end')
self.textPhaseAmplitude.insert(0, "0, 1.382, 1.382, 1.382, 1.382")
self.textPhaseShiftMec.delete(0, 'end')
self.textPhaseShiftMec.insert(0, "0, 72, 144, 216, 288")
self.textPhaseShiftReg.delete(0, 'end')
self.textPhaseShiftReg.insert(0, "0, 72, 144, 216, 288")
self.textnSamples.delete(0, 'end')
self.textnSamples.insert(0, "50")
def Plot(self):
self.v = float(self.textSpeed.get())
self.A = float(self.textAmplitude.get())
self.nSamples = int(self.textnSamples.get())
self.inputNPhases = int(self.textPhases.get())
self.inputPhaseAmplitude = np.ones(self.inputNPhases)
self.inputPhaseShiftMec = np.zeros(self.inputNPhases)
self.inputPhaseShiftReg = np.zeros(self.inputNPhases)
for j in range(self.inputNPhases):
self.inputPhaseAmplitude[j] = float(self.textPhaseAmplitude.get().split(", ")[j])
self.inputPhaseShiftMec[j] = float(self.textPhaseShiftMec.get().split(", ")[j])
self.inputPhaseShiftReg[j] = float(self.textPhaseShiftReg.get().split(", ")[j])
print(self.inputPhaseShiftReg)
def animate(self, i):
self.line.set_ydata(self.A*np.sin(self.x+self.v*i)) # update the data
return self.line,
def init_window(self):
self.master.title("Electric Motor Rotating Field - Visualisation Tool")
self.pack(fill='both', expand=1)
#Create the controls, note use of grid
self.labelSpeed = Label(self, text="Speed (km/Hr)", width=50)
self.labelSpeed.grid(row=0, column=0)
self.labelAmplitude = Label(self, text="Amplitude", width=50)
self.labelAmplitude.grid(row=2, column=0)
self.labelPhases = Label(self, text="Number of phases", width=50)
self.labelPhases.grid(row=4, column=0)
self.labelPhaseAmplitude = Label(self, text="Amplitude of excitation", width=50)
self.labelPhaseAmplitude.grid(row=6, column=0)
self.labelPhaseShiftMec = Label(self, text="Mechanical phase shift", width=50)
self.labelPhaseShiftMec.grid(row=8, column=0)
self.labelPhaseShiftReg = Label(self, text="Electrical phase shift", width=50)
self.labelPhaseShiftReg.grid(row=10, column=0)
self.textSpeed = Entry(self, width=50)
self.textSpeed.grid(row=1, column=0)
self.textAmplitude = Entry(self, width=50)
self.textAmplitude.grid(row=3, column=0)
self.textPhases = Entry(self, width=50)
self.textPhases.grid(row=5, column=0)
self.textPhaseAmplitude = Entry(self, width=50)
self.textPhaseAmplitude.grid(row=7, column=0)
self.textPhaseShiftMec = Entry(self, width=50)
self.textPhaseShiftMec.grid(row=9, column=0)
self.textPhaseShiftReg = Entry(self, width=50)
self.textPhaseShiftReg.grid(row=11, column=0)
self.textnSamples = Entry(self, width=50)
self.textnSamples.grid(row=13, column=0)
self.textAmplitude.insert(0, "1.0")
self.textSpeed.insert(0, "1.0")
self.v = 1.0
self.A = 1.0
self.textPhases.insert(0, "5")
self.textPhaseAmplitude.insert(0, "0, 1.382, 1.382, 1.382, 1.382")
self.textPhaseShiftMec.insert(0, "0, 72, 144, 216, 288")
self.textPhaseShiftReg.insert(0, "0, 72, 144, 216, 288")
self.textnSamples.insert(0, "50")
self.buttonPlot = Button(self, text="Plot", command=self.Plot, width=50)
self.buttonPlot.grid(row=15, column=0)
self.buttonClear = Button(self, text="Clear", command=self.Clear, width=50)
self.buttonClear.grid(row=16, column=0)
self.buttonClear.bind(lambda e: self.Clear)
tk.Label(self, text="Phasers Animation").grid(row=17, column=0)
self.fig = plt.Figure()
self.x = 20*np.arange(0, 2*np.pi, 0.01) # x-array
self.ax = self.fig.add_subplot(111)
self.line, = self.ax.plot(self.x, np.sin(self.x))
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.get_tk_widget().grid(row=18, column=0)
self.ani = animation.FuncAnimation(self.fig, self.animate, np.arange(1, 200), interval=25, blit=False)
root = tk.Tk()
root.geometry("700x1000")
app = Window(root)
tk.mainloop()
\ No newline at end of file
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from tkinter import Frame, Label, Entry, Button
import generateSinLib as gsl
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def Clear(self):
self.textPhases.delete(0, 'end')
self.textPhaseAmplitude.delete(0, 'end')
self.textPhaseShiftMec.delete(0, 'end')
self.textPhaseShiftReg.delete(0, 'end')
def Plot(self):
self.inputNPhases = int(self.textPhases.get())
self.inputPhaseAmplitude = np.ones(self.inputNPhases)
self.inputPhaseShiftMec = np.zeros(self.inputNPhases)
self.inputPhaseShiftReg = np.zeros(self.inputNPhases)
if self.textPhaseAmplitude.get() == '' or len(self.textPhaseAmplitude.get().split(", ")) != self.inputNPhases:
self.inputPhaseAmplitude = None
else:
for j in range(self.inputNPhases):
temp = self.textPhaseAmplitude.get().split(", ")[j]
if temp == '':
self.inputPhaseAmplitude = None
break
else:
self.inputPhaseAmplitude[j] = temp
if self.textPhaseShiftMec.get() == '' or len(self.textPhaseShiftMec.get().split(", ")) != self.inputNPhases:
self.inputPhaseShiftMec = None
else:
for j in range(self.inputNPhases):
temp = self.textPhaseShiftMec.get().split(", ")[j]
if temp == '':
self.inputPhaseShiftMec = None
break
else:
self.inputPhaseShiftMec[j] = temp
if self.textPhaseShiftReg.get() == '' or len(self.textPhaseShiftReg.get().split(", ")) != self.inputNPhases:
self.inputPhaseShiftReg = None
else:
for j in range(self.inputNPhases):
temp = self.textPhaseShiftReg.get().split(", ")[j]
if temp == '':
self.inputPhaseShiftReg = None
break
else:
self.inputPhaseShiftReg[j] = temp
self.generatedSin, self.nPhases, self.phaseShiftReg, self.phaseAmplitude, self.phaseShiftMec, self.xSin = gsl.generateSin(n=self.inputNPhases,
phaseShiftReg=self.inputPhaseShiftReg,
phaseAmplitude=self.inputPhaseAmplitude,
phaseShiftMec=self.inputPhaseShiftMec,
nSamples=self.inputnSamples)
self.ax3.cla()
self.limit1 = self.nPhases / 2 * 1.1 * max(self.phaseAmplitude)
self.limit2 = max(self.phaseAmplitude) * 1.1
self.ax3.set(xlim=(0, 2 * np.pi), ylim=(-self.limit2, self.limit2))
self.actualAngles = list()
for j in range(self.nPhases):
self.ax3.plot(self.xSin, self.generatedSin[j])
self.actualAngles.append(self.phaseShiftMec[j] / 180 * np.pi)
self.vertical = self.ax3.axvline(x=self.xSin[0], color='black')
def animate(self, i):
# Subplot 1
self.ax1.cla()
self.ax2.cla()
self.ax1.set(xlim=(-self.limit1, self.limit1),
ylim=(-self.limit1, self.limit1))
self.ax2.set(xlim=(-self.limit2, self.limit2), ylim=(-self.limit2, self.limit2))
sliding1 = [[0, 0], [0, 0]]
plotX = np.zeros(i)
plotY = np.zeros(i)
for j in range(self.nPhases):
# ------------------------------------------------------------------------------------------------------
# Calculations (Do it only once and reuse after)
actualExcitation = self.generatedSin[j][i]
actualAngle = self.actualAngles[j]
actualSin = np.sin(actualAngle)
actualCos = np.cos(actualAngle)
actualSinGen = actualSin * actualExcitation
actualCosGen = actualCos * actualExcitation
# ------------------------------------------------------------------------------------------------------
# Subplot 1 --------------------------------------------------------------------------------------------
colors = next(self.ax1._get_lines.prop_cycler)['color']
# New start = sum of last starts
sliding1[0][0] += sliding1[1][0]
sliding1[0][1] += sliding1[1][1]
# New end = sin-cos (x-y) of mechanical multiplied by power supply
sliding1[1] = [actualCosGen, actualSinGen]
self.ax1.arrow(sliding1[0][0], sliding1[0][1],
sliding1[1][0], sliding1[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)
# Needed to draw the ellipse
xTemp = actualCos * self.generatedSin[j][:i]
yTemp = actualSin * self.generatedSin[j][:i]
# Have to sum all sin in x and all cos in y (still for ellipse)
plotX += xTemp
plotY += yTemp
# ------------------------------------------------------------------------------------------------------
# Subplot 2 --------------------------------------------------------------------------------------------
sliding2 = [[0, 0], [0, 0]]
colors = next(self.ax2._get_lines.prop_cycler)['color']
sliding2[0] += sliding2[1]
sliding2[1] = [actualCosGen, actualSinGen]
self.ax2.arrow(sliding2[0][0], sliding2[0][1],
sliding2[1][0], sliding2[1][1],
head_width=.1, head_length=.1, fc=colors, ec=colors)
# -----------------------------------------------------------------------------------------------------
# Subplot 3 -------------------------------------------------------------------------------------------
self.vertical.set_xdata(x=self.xSin[i])
self.ax1.plot(plotX, plotY, color='skyblue')
def init_window(self):
self.master.title("Electric Motor Rotating Field - Visualisation Tool")
self.pack(fill='both', expand=1)
#Create the controls, note use of grid
self.labelPhases = Label(self, text="Number of phases", width=50)
self.labelPhases.grid(row=4, column=0)
self.labelPhaseAmplitude = Label(self, text="Amplitude of excitation", width=50)
self.labelPhaseAmplitude.grid(row=6, column=0)
self.labelPhaseShiftMec = Label(self, text="Mechanical phase shift", width=50)
self.labelPhaseShiftMec.grid(row=8, column=0)
self.labelPhaseShiftReg = Label(self, text="Electrical phase shift", width=50)
self.labelPhaseShiftReg.grid(row=10, column=0)
self.textPhases = Entry(self, width=50)
self.textPhases.grid(row=5, column=0)
self.textPhaseAmplitude = Entry(self, width=50)
self.textPhaseAmplitude.grid(row=7, column=0)
self.textPhaseShiftMec = Entry(self, width=50)
self.textPhaseShiftMec.grid(row=9, column=0)
self.textPhaseShiftReg = Entry(self, width=50)
self.textPhaseShiftReg.grid(row=11, column=0)
# -----------------------------------------------------------------------------------
self.textPhases.insert(0, "5")
self.textPhaseAmplitude.insert(0, "0, 1.382, 1.382, 1.382, 1.382")
self.textPhaseShiftMec.insert(0, "0, 72, 144, 216, 288")
self.textPhaseShiftReg.insert(0, "0, 36, 144, 216, 324")
self.buttonPlot = Button(self, text="Plot", command=self.Plot, width=50)
self.buttonPlot.grid(row=15, column=0)
self.buttonClear = Button(self, text="Clear", command=self.Clear, width=50)
self.buttonClear.grid(row=16, column=0)
self.buttonClear.bind(lambda e: self.Clear)
tk.Label(self, text="Phasers Animation").grid(row=17, column=0)
self.fig, (self.ax1, self.ax2, self.ax3) = plt.subplots(1, 3, figsize=(13, 4))
# -----------------------------------------------------------------------------------
self.inputnSamples = 50
self.inputNPhases = 5
self.inputPhaseAmplitude = (0, 1.382, 1.382, 1.382, 1.382)
self.inputPhaseShiftMec = (0, 72, 144, 216, 288)
self.inputPhaseShiftReg = (0, 36, 144, 216, 324)
self.generatedSin, self.nPhases, self.phaseShiftReg, self.phaseAmplitude, self.phaseShiftMec, self.xSin = gsl.generateSin(n=self.inputNPhases,
phaseShiftReg=self.inputPhaseShiftReg,
phaseAmplitude=self.inputPhaseAmplitude,
phaseShiftMec=self.inputPhaseShiftMec,
nSamples=self.inputnSamples)
self.limit1 = self.nPhases / 2 * 1.1
self.limit2 = max(self.phaseAmplitude) * 1.1
self.ax3.set(xlim=(0, 2 * np.pi), ylim=(-self.limit2, self.limit2))
self.actualAngles = list()
for j in range(self.nPhases):
self.ax3.plot(self.xSin, self.generatedSin[j])
self.actualAngles.append(self.phaseShiftMec[j] / 180 * np.pi)
self.vertical = self.ax3.axvline(x=self.xSin[0], color='black')
# -----------------------------------------------------------------------------------
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.get_tk_widget().grid(row=18, column=0)
self.ani = animation.FuncAnimation(self.fig, self.animate, interval=20, frames=self.inputnSamples, blit=False, save_count=self.inputnSamples)
root = tk.Tk()
root.geometry("1300x700")
app = Window(root)
tk.mainloop()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment