mainWindow.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import tkinter as tk
  2. import time
  3. import queue
  4. import numpy as np
  5. from gui.popup import CalibrationPopUp
  6. from gui.graph import Graph
  7. class MainWindow(tk.Frame):
  8. def __init__(self, root, ac_sensor, ac_queue, ac_cal_state):
  9. self.root = root
  10. self.popup = None
  11. self.ac_cal_state = ac_cal_state
  12. self.ac_sensor = ac_sensor
  13. self.ac_queue = ac_queue
  14. tk.Frame.__init__(self, root)
  15. self.graph = Graph(self)
  16. self.graph.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
  17. self.controls = tk.Frame(self, borderwidth=4)
  18. self.controls.pack(fill=tk.BOTH, side=tk.RIGHT)
  19. self.controlsUpdateTime = 0
  20. self.ac_dro_val_sums = np.ndarray((4), dtype=np.float)
  21. self.ac_dro_val_count = 0
  22. self.ac_dro_x = tk.StringVar()
  23. self.ac_dro_y = tk.StringVar()
  24. self.ac_dro_t1 = tk.StringVar()
  25. self.ac_dro_t2 = tk.StringVar()
  26. tk.Label(self.controls, text="Acustic Sensor", anchor="c", font=("Helvatica", 10, 'bold')).pack(side="top", fill="both", expand=False)
  27. tk.Label(self.controls, textvariable=self.ac_dro_x, anchor="nw").pack(side="top", fill="both", expand=False)
  28. tk.Label(self.controls, textvariable=self.ac_dro_y, anchor="nw").pack(side="top", fill="both", expand=False)
  29. tk.Label(self.controls, textvariable=self.ac_dro_t1, anchor="nw").pack(side="top", fill="both", expand=False)
  30. tk.Label(self.controls, textvariable=self.ac_dro_t2, anchor="nw").pack(side="top", fill="both", expand=False)
  31. calibrate_button = tk.Button(self.controls, text="Calibrate", command=self.calibrate, height=4)
  32. calibrate_button.pack(side="bottom", fill="both")
  33. clear_button = tk.Button(self.controls, text="Clear graph", command=self.graph.clear, height=4)
  34. clear_button.pack(side="bottom", fill="both")
  35. def update(self):
  36. if not self.root.winfo_exists():
  37. return
  38. ac_positions = []
  39. while self.ac_queue.qsize() > 0:
  40. name, data = self.ac_queue.get()
  41. if name == "data":
  42. ac_positions.append(data[0:2])
  43. self.ac_dro_val_sums += data
  44. self.ac_dro_val_count += 1
  45. if len(ac_positions) > 0:
  46. self.graph.update([ac_positions])
  47. if self.controlsUpdateTime + 0.4 < time.time():
  48. self.controlsUpdateTime = time.time()
  49. if self.ac_dro_val_count > 0:
  50. self.ac_dro_val_sums /= self.ac_dro_val_count
  51. else:
  52. self.ac_dro_val_sums.fill(0)
  53. self.ac_dro_x.set("X: {:.1f} mm".format(self.ac_dro_val_sums[0]))
  54. self.ac_dro_y.set("Y: {:.1f} mm".format(self.ac_dro_val_sums[1]))
  55. self.ac_dro_t1.set("t1: {:.3f} ms".format(self.ac_dro_val_sums[2]/1000))
  56. self.ac_dro_t2.set("t2: {:.3f} ms".format(self.ac_dro_val_sums[3]/1000))
  57. self.ac_dro_val_sums.fill(0)
  58. self.ac_dro_val_count = 0
  59. if self.popup:
  60. self.popup.update()
  61. self.root.after(30, self.update)
  62. def calibrate(self):
  63. self.ac_sensor.start_calibration()
  64. if not self.popup or not self.pu_root.winfo_exists():
  65. self.pu_root = tk.Toplevel(self.root)
  66. self.pu_root.wm_transient(self.root)
  67. self.pu_root.wm_title("Calibration")
  68. self.pu_root.geometry("500x200")
  69. self.pu_root.grab_set()
  70. self.popup = CalibrationPopUp(self.pu_root, self.ac_cal_state)
  71. self.popup.pack(side="top", fill="both", expand=True)
  72. if __name__ == "__main__":
  73. root = tk.Tk()
  74. up_queue = queue.Queue()
  75. down_queue = queue.Queue()
  76. root.title("Tracking System")
  77. view = MainWindow(root,up_queue,down_queue,list())
  78. view.pack(side="top", fill="both", expand=True)
  79. view.update()
  80. root.mainloop()