1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import tkinter as tk
- import time
- import queue
- import numpy as np
- from gui.popup import CalibrationPopUp
- from gui.graph import Graph
- class MainWindow(tk.Frame):
- def __init__(self, root, ac_sensor, ac_queue, ac_cal_state):
- self.root = root
- self.popup = None
- self.ac_cal_state = ac_cal_state
- self.ac_sensor = ac_sensor
- self.ac_queue = ac_queue
- tk.Frame.__init__(self, root)
- self.graph = Graph(self)
- self.graph.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
- self.controls = tk.Frame(self, borderwidth=4)
- self.controls.pack(fill=tk.BOTH, side=tk.RIGHT)
- self.controlsUpdateTime = 0
- self.ac_dro_val_sums = np.ndarray((4), dtype=np.float)
- self.ac_dro_val_count = 0
- self.ac_dro_x = tk.StringVar()
- self.ac_dro_y = tk.StringVar()
- self.ac_dro_t1 = tk.StringVar()
- self.ac_dro_t2 = tk.StringVar()
- tk.Label(self.controls, text="Acustic Sensor", anchor="c", font=("Helvatica", 10, 'bold')).pack(side="top", fill="both", expand=False)
- tk.Label(self.controls, textvariable=self.ac_dro_x, anchor="nw").pack(side="top", fill="both", expand=False)
- tk.Label(self.controls, textvariable=self.ac_dro_y, anchor="nw").pack(side="top", fill="both", expand=False)
- tk.Label(self.controls, textvariable=self.ac_dro_t1, anchor="nw").pack(side="top", fill="both", expand=False)
- tk.Label(self.controls, textvariable=self.ac_dro_t2, anchor="nw").pack(side="top", fill="both", expand=False)
- calibrate_button = tk.Button(self.controls, text="Calibrate", command=self.calibrate, height=3)
- calibrate_button.pack(side="bottom", fill="both")
- clear_button = tk.Button(self.controls, text="Clear graph", command=self.graph.clear)
- clear_button.pack(side="bottom", fill="both")
- def update(self):
- if not self.root.winfo_exists():
- return
- ac_positions = []
- while self.ac_queue.qsize() > 0:
- name, data = self.ac_queue.get()
- if name == "data":
- ac_positions.append(data[0:2])
- self.ac_dro_val_sums += data
- self.ac_dro_val_count += 1
- if len(ac_positions) > 0:
- self.graph.update([ac_positions])
- if self.controlsUpdateTime + 0.4 < time.time():
- self.controlsUpdateTime = time.time()
- if self.ac_dro_val_count > 0:
- self.ac_dro_val_sums /= self.ac_dro_val_count
- else:
- self.ac_dro_val_sums.fill(0)
- self.ac_dro_x.set("X: {:.1f} mm".format(self.ac_dro_val_sums[0]))
- self.ac_dro_y.set("Y: {:.1f} mm".format(self.ac_dro_val_sums[1]))
- self.ac_dro_t1.set("t1: {:.3f} ms".format(self.ac_dro_val_sums[2]/1000))
- self.ac_dro_t2.set("t2: {:.3f} ms".format(self.ac_dro_val_sums[3]/1000))
- self.ac_dro_val_sums.fill(0)
- self.ac_dro_val_count = 0
- if self.popup:
- self.popup.update()
- self.root.after(30, self.update)
- def calibrate(self):
- self.ac_sensor.start_calibration()
- if not self.popup or not self.pu_root.winfo_exists():
- self.pu_root = tk.Toplevel(self.root)
- self.pu_root.wm_transient(self.root)
- self.pu_root.wm_title("Calibration")
- self.pu_root.geometry("500x200")
- self.pu_root.grab_set()
- self.popup = CalibrationPopUp(self.pu_root, self.ac_cal_state)
- self.popup.pack(side="top", fill="both", expand=True)
-
- if __name__ == "__main__":
- root = tk.Tk()
- up_queue = queue.Queue()
- down_queue = queue.Queue()
- root.title("Tracking System")
- view = MainWindow(root,up_queue,down_queue,list())
- view.pack(side="top", fill="both", expand=True)
- view.update()
- root.mainloop()
|