Explorar o código

removed down_queue and sensor_thread

subDesTagesMitExtraKaese %!s(int64=3) %!d(string=hai) anos
pai
achega
f31af5dd1e
Modificáronse 4 ficheiros con 44 adicións e 47 borrados
  1. 1 3
      raspberry-pi/gui/Popup.py
  2. 22 19
      raspberry-pi/gui/mainWindow.py
  3. 6 8
      raspberry-pi/main.py
  4. 15 17
      raspberry-pi/sensors.py

+ 1 - 3
raspberry-pi/gui/Popup.py

@@ -5,11 +5,9 @@ import queue
 
 
 class CalibrationPopUp(tk.Frame):
-  def __init__(self, root, up_queue, down_queue,calibration_state):
+  def __init__(self, root, calibration_state):
     self.root = root
     self.calibration_state = calibration_state
-    self.down_queue = down_queue
-    self.up_queue = up_queue
     tk.Frame.__init__(self, root)
     self.killed = False
 

+ 22 - 19
raspberry-pi/gui/mainWindow.py

@@ -2,19 +2,19 @@ import tkinter as tk
 import time
 import queue
 
-import gui.Popup as Popup
-import gui.graph as Graph
+from gui.popup import CalibrationPopUp
+from gui.graph import Graph
 
 class MainWindow(tk.Frame):
-  def __init__(self, root, up_queue, down_queue,calibration_state):
+  def __init__(self, root, ac_sensor, ac_queue, ac_cal_state):
     self.root = root
     self.popup = None
-    self.calibration_state = calibration_state
-    self.down_queue = down_queue
-    self.up_queue = up_queue
+    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.Graph(self)
+    self.graph = Graph(self)
     self.graph.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
 
     self.controls = tk.Frame(self)
@@ -30,18 +30,21 @@ class MainWindow(tk.Frame):
     calibrate_button.pack(side="top", fill="both")
 
   def update(self):
-    ac_data = []
-    while self.up_queue.qsize() > 0:
-      name, data = self.up_queue.get()
-      if name == "ac_data":
-        ac_data.append(data)
-    if len(ac_data) > 0:
-      self.graph.update([ac_data])
-      self.ac_dro_x.set("X: {:3.1f} mm".format(ac_data[-1][0]))
-      self.ac_dro_y.set("Y: {:3.1f} mm".format(ac_data[-1][1]))
+    ac_positions = []
+    ac_raw_vals = (0, 0)
+    while self.ac_queue.qsize() > 0:
+      name, data = self.ac_queue.get()
+      if name == "data":
+        ac_positions.append(data[0:2])
+        ac_raw_vals = data[2:4]
+
+    if len(ac_positions) > 0:
+      self.graph.update([ac_positions])
+      self.ac_dro_x.set("X: {:3.1f} mm".format(ac_positions[-1][0]))
+      self.ac_dro_y.set("Y: {:3.1f} mm".format(ac_positions[-1][1]))
 
     if self.popup:
-      if self.calibration_state.get_state() == self.calibration_state.CALIBRATION_DONE and not self.popup.killed:
+      if self.ac_cal_state.get_state() == self.ac_cal_state.CALIBRATION_DONE and not self.popup.killed:
         self.popup.killed = True
         self.root.after(1500,self.kill_popup)
       self.popup.update()
@@ -52,12 +55,12 @@ class MainWindow(tk.Frame):
     self.popup = None
 
   def calibrate(self):
-    self.down_queue.put("calibrate")
+    self.ac_sensor.start_calibration()
     if not self.popup:
       self.pu_root = tk.Tk()
       self.pu_root.title("Calibration")
       self.pu_root.geometry("500x200")
-      self.popup = Popup.CalibrationPopUp(self.pu_root,self.up_queue,self.down_queue,self.calibration_state)
+      self.popup = CalibrationPopUp(self.pu_root, self.ac_cal_state)
       self.popup.pack(side="top", fill="both", expand=True)
     
     

+ 6 - 8
raspberry-pi/main.py

@@ -56,17 +56,15 @@ class CalibrationStateMashine():
 
 
 def main():
-  up_queue = queue.Queue()
-  down_queue = queue.Queue()
-  calibration_state = CalibrationStateMashine()
-  ac_sensor = sensors.AcusticSensor(conf,up_queue,down_queue,calibration_state)
-  sensor_thread = threading.Thread(target=ac_sensor.start)
+  ac_queue = queue.Queue()
+  ac_calibration_state = CalibrationStateMashine()
+  ac_sensor = sensors.AcusticSensor(conf, ac_queue, ac_calibration_state)
 
   try:
-    sensor_thread.start()
+    ac_sensor.start()
     root = tk.Tk()
     root.title("Tracking System")
-    view = MainWindow(root,up_queue,down_queue,calibration_state)
+    view = MainWindow(root, ac_sensor, ac_queue, ac_calibration_state)
     view.pack(side="top", fill="both", expand=True)
     view.update()
     root.mainloop()
@@ -77,7 +75,7 @@ def main():
     print("Error: ",e)
     traceback.print_exc()
   finally:
-    down_queue.put("stop")
+    ac_sensor.stop()
 
 main()
 

+ 15 - 17
raspberry-pi/sensors.py

@@ -11,9 +11,8 @@ import cv2
 conn = ArduinoSlave()
 
 class AcusticSensor:
-  def __init__(self,conf,up_queue,down_queue,calibration_state):
-    self.up_queue               = up_queue
-    self.down_queue             = down_queue
+  def __init__(self, conf, ac_queue, calibration_state):
+    self.ac_queue               = ac_queue
     self.calibration_state      = calibration_state
     self.field_height           = float(conf["field"]["y"])
     self.field_length           = float(conf["field"]["x"])
@@ -39,17 +38,15 @@ class AcusticSensor:
     conn.addRecvCallback(self._readCb)
     dummyThread = threading.Thread(target=self._readCb_dummy)
     dummyThread.start()
-    while True:
-      action = self.down_queue.get()
-      print("action",action)
-      if action == "calibrate":
-        self.calibration_state.reset_state()
-        self.time_vals = [[],[]]
-        self.calibration_state.next_state()
-      elif action == "stop":
-        print("exit Sensor")
-        self.dummyActive = False
-        break
+    
+  def start_calibration(self):
+    self.calibration_state.reset_state()
+    self.time_vals = [[],[]]
+    self.calibration_state.next_state()
+
+  def stop(self):
+    print("stop acoustic sensor")
+    self.dummyActive = False
     conn.close()
 
   def _readCb_dummy(self):
@@ -76,7 +73,8 @@ class AcusticSensor:
 
     if value[0] >= 0 and value[1] >= 0:
       self.calibrate(value)
-      self.pass_to_gui(self.calculate_position(value))
+      x, y = self.calculate_position(value)
+      self.pass_to_gui((x, y, *value))
 
   def calibrate(self, value):
 
@@ -141,8 +139,8 @@ class AcusticSensor:
       print(values)
       traceback.print_exc()
 
-  def pass_to_gui(self,data):
-    self.up_queue.put(("ac_data", data))
+  def pass_to_gui(self, data):
+    self.ac_queue.put(("data", data))
 
 class MagneticSensor:
   def __init__(self):