appWindow.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. import time
  14. class Main(tk.Tk, Table):
  15. def __init__(self, *args, **kwargs):
  16. print('initializing window...')
  17. tk.Tk.__init__(self, *args, **kwargs)
  18. tk.Tk.wm_title(self, "Windkanal-Tool")
  19. #early response to user
  20. self.update_idletasks()
  21. print('initializing sensors...')
  22. self.adc = MCP3008(0,0)
  23. self.pressureSensors = Spd610Array()
  24. self.forceSensors = LoadCells()
  25. self.forceSensors.start()
  26. self.motorController = PWM(32)
  27. self.pid = PID()
  28. self.motorEnabled = False
  29. print('initializing database...')
  30. Table.__init__(self,
  31. ["windspeed", "motor_pwm"] +
  32. ["pressure_{}".format(i) for i in range(8)] +
  33. ["adc_{}".format(i) for i in range(1)] +
  34. ["force_X_1", "force_Y_1", "force_Z_1"] +
  35. ["force_X_2", "force_Y_2", "force_Z_2"] +
  36. ["force_X_3", "force_Y_3", "force_Z_3"])
  37. print('initializing GUI...')
  38. container = tk.Frame(self)
  39. container.pack(side="top", fill="both", expand = True)
  40. container.grid_rowconfigure(0, weight=1)
  41. container.grid_columnconfigure(0, weight=1)
  42. menubar = tk.Menu(container)
  43. filemenu = tk.Menu(menubar, tearoff=0)
  44. filemenu.add_command(label="Save settings", command = lambda: self.popupmsg("Not supported just yet!"))
  45. filemenu.add_separator()
  46. filemenu.add_command(label="Exit", command=quit)
  47. menubar.add_cascade(label="File", menu=filemenu)
  48. tk.Tk.config(self, menu=menubar)
  49. label = tk.Label(self, text="Bedienelemente", font=LARGE_FONT)
  50. label.pack(pady=10,padx=10)
  51. # top menu
  52. top = tk.Frame(self, borderwidth=2, relief="solid")
  53. button1 = tk.Button(top, text="Bedienelemente", command=lambda: self.show_frame(Page_1))
  54. button1.pack(side=tk.LEFT)
  55. button2 = tk.Button(top, text="Kräfte", command=lambda: self.show_frame(Page_2))
  56. button2.pack(side=tk.LEFT)
  57. button3 = tk.Button(top, text="Druck", command=lambda: self.show_frame(Page_3))
  58. button3.pack(side=tk.LEFT)
  59. button4 = tk.Button(top,text="Einstellungen",command=lambda: self.show_frame(Page_4))
  60. button4.pack(side=tk.LEFT)
  61. button5 = tk.Button(top, text="QUIT", fg="red",command=quit)
  62. button5.pack(side=tk.LEFT)
  63. top.pack(side="top", expand=True, fill="both")
  64. self.frames = {}
  65. for F in (Page_1, Page_2, Page_3, Page_4):
  66. frame = F(container, self)
  67. self.frames[F] = frame
  68. frame.grid(row=0, column=0, sticky="nsew")
  69. self.show_frame(Page_1)
  70. self.after(5000,self.interval)
  71. print('program ready!')
  72. def show_frame(self, cont):
  73. self.currentFrame = self.frames[cont]
  74. self.currentFrame.tkraise()
  75. def popupmsg(self, msg=""):
  76. popup = tk.Toplevel(self.master)
  77. popup.wm_title("Error!")
  78. label = tk.Label(popup, text=msg, font=LARGE_FONT)
  79. label.pack(side="top", fill="x", pady=10)
  80. b1 = tk.Button(popup, text="Okay", command=popup.destroy)
  81. b1.pack()
  82. def interval(self):
  83. self.after(300,self.interval)
  84. start = time.time()
  85. adcValue = self.adc.getVoltage(0)
  86. windSpeed = adcValue * 1337
  87. pwmValue = self.pid.update(windSpeed)
  88. if not self.motorEnabled:
  89. pwmValue = 0
  90. self.motorController.setDutyCycle(pwmValue)
  91. i2cValues = self.pressureSensors.getValues()
  92. self.addRow(
  93. [windSpeed, pwmValue] +
  94. i2cValues +
  95. [adcValue] +
  96. self.forceSensors.getForces(0) +
  97. self.forceSensors.getForces(1) +
  98. self.forceSensors.getForces(2)
  99. )
  100. print("sensors: {:8.3f} ms".format((time.time() - start)*1000))
  101. start = time.time()
  102. for frame in self.frames:
  103. self.frames[frame].update(self.frames[frame] == self.currentFrame)
  104. print("draw: {:8.3f} ms".format((time.time() - start)*1000))