appWindow.py 4.0 KB

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