|
@@ -12,7 +12,9 @@ from ui import *
|
|
|
|
|
|
import tkinter as tk
|
|
|
import tk_tools
|
|
|
+from tkinter import filedialog
|
|
|
import time
|
|
|
+import numpy as np
|
|
|
|
|
|
|
|
|
class Main(tk.Tk, Table):
|
|
@@ -30,49 +32,46 @@ class Main(tk.Tk, Table):
|
|
|
self.forceSensors = LoadCells()
|
|
|
self.forceSensors.start()
|
|
|
self.motorController = PWM(32)
|
|
|
- self.pid = PID()
|
|
|
-
|
|
|
- self.motorEnabled = False
|
|
|
+ self.pid = PID(100/22, 5, 1)
|
|
|
+ self.pid.setWindup(1)
|
|
|
|
|
|
print('initializing database...')
|
|
|
Table.__init__(self,
|
|
|
- ["windspeed", "motor_pwm"] +
|
|
|
+ ["windspeed", "set_value", "motor_pwm"] +
|
|
|
["pressure_{}".format(i) for i in range(8)] +
|
|
|
["adc_{}".format(i) for i in range(1)] +
|
|
|
+ ["force_X_sum", "force_Y_sum", "force_Z_sum"] +
|
|
|
["force_X_1", "force_Y_1", "force_Z_1"] +
|
|
|
["force_X_2", "force_Y_2", "force_Z_2"] +
|
|
|
["force_X_3", "force_Y_3", "force_Z_3"])
|
|
|
|
|
|
|
|
|
print('initializing GUI...')
|
|
|
- menubar = tk.Menu(self)
|
|
|
- filemenu = tk.Menu(menubar, tearoff=0)
|
|
|
- filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
|
|
|
- filemenu.add_separator()
|
|
|
- filemenu.add_command(label="Exit", command=self.stop)
|
|
|
- menubar.add_cascade(label="File", menu=filemenu)
|
|
|
+ self.menubar = tk.Menu(self)
|
|
|
+
|
|
|
+ self.frameVar = tk.StringVar()
|
|
|
+ self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_1', command=self.show_frame, label="Bedienelemente")
|
|
|
+ self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_2', command=self.show_frame, label="Kräfte")
|
|
|
+ self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_3', command=self.show_frame, label="Druck")
|
|
|
+ self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_4', command=self.show_frame, label="Einstellungen")
|
|
|
+
|
|
|
+ self.menubar.add_command(state=tk.DISABLED, label=" ")
|
|
|
|
|
|
- tk.Tk.config(self, menu=menubar)
|
|
|
+ self.motorEnabled = tk.IntVar()
|
|
|
+ self.menubar.add_checkbutton(indicatoron=0, variable=self.motorEnabled, background='#dd5252', label="Motor freischalten", command=lambda:
|
|
|
+ self.menubar.entryconfigure("Motor freischalten", background='#dd5252' if self.motorEnabled.get() == 0 else 'green'))
|
|
|
+ self.motorEnabled.set(0)
|
|
|
|
|
|
- label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
|
|
|
- label.pack(pady=10,padx=10)
|
|
|
- # top menu
|
|
|
- top = tk.Frame(self, borderwidth=2, relief="solid")
|
|
|
- button1 = tk.Button(top, text="Bedienelemente", command=lambda: self.show_frame(Page_1))
|
|
|
- button1.pack(side=tk.LEFT)
|
|
|
+ self.menubar.add_command(state=tk.DISABLED, label=" ")
|
|
|
|
|
|
- button2 = tk.Button(top, text="Kräfte", command=lambda: self.show_frame(Page_2))
|
|
|
- button2.pack(side=tk.LEFT)
|
|
|
+ self.menubar.add_command(label="Messwerte speichern", command = self.save_dialog)
|
|
|
+ self.menubar.add_command(label='Messwerte löschen', command=self.reset)
|
|
|
|
|
|
- button3 = tk.Button(top, text="Druck", command=lambda: self.show_frame(Page_3))
|
|
|
- button3.pack(side=tk.LEFT)
|
|
|
+ self.menubar.add_command(state=tk.DISABLED, label=" ")
|
|
|
|
|
|
- button4 = tk.Button(top,text="Einstellungen",command=lambda: self.show_frame(Page_4))
|
|
|
- button4.pack(side=tk.LEFT)
|
|
|
+ self.menubar.add_command(label="Beenden", foreground="red", command=self.stop)
|
|
|
|
|
|
- button5 = tk.Button(top, text="QUIT", fg="red",command=self.stop)
|
|
|
- button5.pack(side=tk.LEFT)
|
|
|
- top.pack(side="top", fill="both")
|
|
|
+ tk.Tk.config(self, menu=self.menubar)
|
|
|
|
|
|
container = tk.Frame(self)
|
|
|
container.pack(side="top", fill="both", expand = True)
|
|
@@ -85,19 +84,20 @@ class Main(tk.Tk, Table):
|
|
|
|
|
|
frame = F(container, self)
|
|
|
|
|
|
- self.frames[F] = frame
|
|
|
+ self.frames[F.__name__] = frame
|
|
|
|
|
|
frame.grid(row=0, column=0, sticky="nsew")
|
|
|
|
|
|
- self.show_frame(Page_1)
|
|
|
+ self.frameVar.set('Page_1')
|
|
|
+ self.show_frame()
|
|
|
|
|
|
print('program ready!')
|
|
|
|
|
|
+ self.intervalDelay = 300 # ms
|
|
|
self.interval()
|
|
|
|
|
|
- def show_frame(self, cont):
|
|
|
- self.currentFrame = self.frames[cont]
|
|
|
- self.currentFrame.tkraise()
|
|
|
+ def show_frame(self):
|
|
|
+ self.frames[self.frameVar.get()].tkraise()
|
|
|
|
|
|
def popupmsg(self, msg=""):
|
|
|
popup = tk.Toplevel(self.master)
|
|
@@ -106,35 +106,55 @@ class Main(tk.Tk, Table):
|
|
|
label.pack(side="top", fill="x", pady=10)
|
|
|
b1 = tk.Button(popup, text="Okay", command=popup.destroy)
|
|
|
b1.pack()
|
|
|
-
|
|
|
+
|
|
|
+ def save_dialog(self):
|
|
|
+ f = filedialog.asksaveasfile(mode='w+', defaultextension=".csv")
|
|
|
+ if f is None:
|
|
|
+ return
|
|
|
+ self.saveAsCsv(f)
|
|
|
+ f.close()
|
|
|
+
|
|
|
def interval(self):
|
|
|
- self.after(300,self.interval)
|
|
|
+ self.after(self.intervalDelay, self.interval)
|
|
|
start = time.time()
|
|
|
+
|
|
|
+ setValue = self.frames['Page_1'].speedSlider.get()
|
|
|
+ self.pid.setInput(setValue)
|
|
|
+
|
|
|
adcValue = self.adc.getVoltage(0)
|
|
|
- windSpeed = adcValue * 1337
|
|
|
+ windSpeed = adcValue * 5
|
|
|
pwmValue = self.pid.update(windSpeed)
|
|
|
- if not self.motorEnabled:
|
|
|
+
|
|
|
+ if self.motorEnabled.get() == 0:
|
|
|
+ self.pid.clear()
|
|
|
pwmValue = 0
|
|
|
+ elif pwmValue > 100:
|
|
|
+ pwmValue = 100
|
|
|
+ elif pwmValue < 0:
|
|
|
+ pwmValue = 0
|
|
|
+
|
|
|
self.motorController.setDutyCycle(pwmValue)
|
|
|
|
|
|
i2cValues = self.pressureSensors.getValues()
|
|
|
+ btValues = self.forceSensors.getForces()
|
|
|
|
|
|
self.addRow(
|
|
|
- [windSpeed, pwmValue] +
|
|
|
+ [windSpeed, setValue, pwmValue] +
|
|
|
i2cValues +
|
|
|
[adcValue] +
|
|
|
- self.forceSensors.getForces(0) +
|
|
|
- self.forceSensors.getForces(1) +
|
|
|
- self.forceSensors.getForces(2)
|
|
|
+ list(np.sum(btValues, axis = 0, initial=0)) +
|
|
|
+ list(btValues.flatten())
|
|
|
)
|
|
|
print("sensors: {:8.3f} ms".format((time.time() - start)*1000))
|
|
|
|
|
|
start = time.time()
|
|
|
for frame in self.frames:
|
|
|
- self.frames[frame].update(self.frames[frame] == self.currentFrame)
|
|
|
+ self.frames[frame].update(frame == self.frameVar.get())
|
|
|
|
|
|
print("draw: {:8.3f} ms".format((time.time() - start)*1000))
|
|
|
|
|
|
def stop(self):
|
|
|
self.forceSensors.stop()
|
|
|
+ self.motorEnabled.set(0)
|
|
|
+ self.motorController.setDutyCycle(0)
|
|
|
self.quit()
|