appWindow.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
  2. # License: http://creativecommons.org/licenses/by-sa/3.0/
  3. from database import Table
  4. from analogPressure.sdpArray import SdpArray
  5. from analogPressure.mcp3008 import MCP3008
  6. from digitalPressure.sdp610Array import Spd610Array
  7. from wirelessLoadCell.loadCells import LoadCells
  8. from motorController.pwmOutput import PWM
  9. from motorController.pidController import PID
  10. from ui import *
  11. import tkinter as tk
  12. import tk_tools
  13. from tkinter import filedialog
  14. import time
  15. import numpy as np
  16. class Main(tk.Tk, Table):
  17. def __init__(self, *args, **kwargs):
  18. print('initializing window...')
  19. tk.Tk.__init__(self, *args, **kwargs)
  20. tk.Tk.wm_title(self, "Windkanal-Tool")
  21. #early response to user
  22. self.update_idletasks()
  23. print('initializing sensors...')
  24. self.adc = MCP3008(0,0)
  25. self.pressureSensors = Spd610Array()
  26. self.forceSensors = LoadCells()
  27. self.forceSensors.start()
  28. self.motorController = PWM(32)
  29. self.pid = PID(100/22, 5, 1)
  30. self.pid.setWindup(1)
  31. print('initializing database...')
  32. Table.__init__(self,
  33. ["windspeed", "set_value", "motor_pwm"] +
  34. ["pressure_{}".format(i) for i in range(8)] +
  35. ["adc_{}".format(i) for i in range(1)] +
  36. ["force_X_sum", "force_Y_sum", "force_Z_sum"] +
  37. ["force_X_1", "force_Y_1", "force_Z_1"] +
  38. ["force_X_2", "force_Y_2", "force_Z_2"] +
  39. ["force_X_3", "force_Y_3", "force_Z_3"])
  40. print('initializing GUI...')
  41. self.menubar = tk.Menu(self)
  42. self.frameVar = tk.StringVar()
  43. self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_1', command=self.show_frame, label="Bedienelemente")
  44. self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_2', command=self.show_frame, label="Kräfte")
  45. self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_3', command=self.show_frame, label="Druck")
  46. self.menubar.add_radiobutton(indicatoron=0, variable=self.frameVar, value='Page_4', command=self.show_frame, label="Einstellungen")
  47. self.menubar.add_command(state=tk.DISABLED, label=" ")
  48. self.motorEnabled = tk.IntVar()
  49. self.menubar.add_checkbutton(indicatoron=0, variable=self.motorEnabled, background='#dd5252', label="Motor freischalten", command=lambda:
  50. self.menubar.entryconfigure("Motor freischalten", background='#dd5252' if self.motorEnabled.get() == 0 else 'green'))
  51. self.motorEnabled.set(0)
  52. self.menubar.add_command(state=tk.DISABLED, label=" ")
  53. self.menubar.add_command(label="Messwerte speichern", command = self.save_dialog)
  54. self.menubar.add_command(label='Messwerte löschen', command=self.reset)
  55. self.menubar.add_command(state=tk.DISABLED, label=" ")
  56. self.menubar.add_command(label="Beenden", foreground="red", command=self.stop)
  57. tk.Tk.config(self, menu=self.menubar)
  58. container = tk.Frame(self)
  59. container.pack(side="top", fill="both", expand = True)
  60. container.grid_rowconfigure(0, weight=1)
  61. container.grid_columnconfigure(0, weight=1)
  62. self.frames = {}
  63. for F in (Page_1, Page_2, Page_3, Page_4):
  64. frame = F(container, self)
  65. self.frames[F.__name__] = frame
  66. frame.grid(row=0, column=0, sticky="nsew")
  67. self.frameVar.set('Page_1')
  68. self.show_frame()
  69. print('program ready!')
  70. self.intervalDelay = 300 # ms
  71. self.interval()
  72. def show_frame(self):
  73. self.frames[self.frameVar.get()].tkraise()
  74. def popupmsg(self, msg=""):
  75. popup = tk.Toplevel(self.master)
  76. popup.wm_title("Error!")
  77. label = tk.Label(popup, text=msg, font=LARGE_FONT)
  78. label.pack(side="top", fill="x", pady=10)
  79. b1 = tk.Button(popup, text="Okay", command=popup.destroy)
  80. b1.pack()
  81. def save_dialog(self):
  82. f = filedialog.asksaveasfile(mode='w+', defaultextension=".csv")
  83. if f is None:
  84. return
  85. self.saveAsCsv(f)
  86. f.close()
  87. def interval(self):
  88. self.after(self.intervalDelay, self.interval)
  89. start = time.time()
  90. setValue = self.frames['Page_1'].speedSlider.get()
  91. self.pid.setInput(setValue)
  92. adcValue = self.adc.getVoltage(0)
  93. windSpeed = adcValue * 5
  94. pwmValue = self.pid.update(windSpeed)
  95. if self.motorEnabled.get() == 0:
  96. self.pid.clear()
  97. pwmValue = 0
  98. elif pwmValue > 100:
  99. pwmValue = 100
  100. elif pwmValue < 0:
  101. pwmValue = 0
  102. self.motorController.setDutyCycle(pwmValue)
  103. i2cValues = self.pressureSensors.getValues()
  104. btValues = self.forceSensors.getForces()
  105. self.addRow(
  106. [windSpeed, setValue, pwmValue] +
  107. i2cValues +
  108. [adcValue] +
  109. list(np.sum(btValues, axis = 0, initial=0)) +
  110. list(btValues.flatten())
  111. )
  112. print("sensors: {:8.3f} ms".format((time.time() - start)*1000))
  113. start = time.time()
  114. for frame in self.frames:
  115. self.frames[frame].update(frame == self.frameVar.get())
  116. print("draw: {:8.3f} ms".format((time.time() - start)*1000))
  117. def stop(self):
  118. self.forceSensors.stop()
  119. self.motorEnabled.set(0)
  120. self.motorController.setDutyCycle(0)
  121. self.quit()