瀏覽代碼

changed file structure

subDesTagesMitExtraKaese 3 年之前
父節點
當前提交
c89bfd06b2

+ 3 - 48
raspberry-pi/main.py

@@ -1,8 +1,7 @@
-import sensors
+from sensors.acusticSensor import AcusticSensor
+from sensors.calibration import CalibrationStateMashine
 from gui.mainWindow import MainWindow
 
-import time
-import threading
 import queue
 import configparser
 import tkinter as tk
@@ -11,54 +10,10 @@ import traceback
 conf = configparser.ConfigParser()
 conf.read('config.ini')
 
-class CalibrationStateMashine():
-
-  def __init__(self):
-    self.state = 0
-    self.value_count = 0
-
-    self.NOT_CALIBRATED = 0
-    self.WAITING_POS_1  = 1
-    self.ACCUMULATING_1 = 2
-    self.WAITING_POS_2  = 3
-    self.ACCUMULATING_2 = 4
-    self.CALIBRATION_DONE = 5
-
-  def state_clearname(self):
-    if self.state == self.NOT_CALIBRATED:
-      return "not calibrated"
-    elif self.state == self.WAITING_POS_1:
-      return "Waiting for Position one"
-    elif self.state == self.ACCUMULATING_1:
-      return "gathering values on position one"
-    elif self.state == self.WAITING_POS_2:
-      return "Waiting for position two"
-    elif self.state == self.ACCUMULATING_2:
-      return "gathering values on position two"
-    elif self.state == self.CALIBRATION_DONE:
-      return "calibration done"
-    
-  def next_state(self):
-    if self.state < self.CALIBRATION_DONE:
-      self.state += 1
-      print(self.state_clearname())
-
-  def next_state_gui(self):
-    print("next_state_gui",self.state)
-    if self.state == self.WAITING_POS_1 or self.state == self.WAITING_POS_2:
-      self.next_state()
-
-  def get_state(self):
-    return self.state
-
-  def reset_state(self):
-    self.state = 0
-
-
 def main():
   ac_queue = queue.Queue()
   ac_calibration_state = CalibrationStateMashine()
-  ac_sensor = sensors.AcusticSensor(conf, ac_queue, ac_calibration_state)
+  ac_sensor = AcusticSensor(conf, ac_queue, ac_calibration_state)
 
   try:
     ac_sensor.start()

+ 3 - 58
raspberry-pi/sensors.py

@@ -1,14 +1,12 @@
-from connection import ArduinoSlave
-import markerDetection
+from sensors.connection import globalArduinoSlave
 import time
 import statistics
 import math
 import threading
 import random
 import traceback
-import cv2
 
-conn = ArduinoSlave()
+conn = globalArduinoSlave()
 
 class AcusticSensor:
   def __init__(self, conf, ac_queue, calibration_state):
@@ -140,57 +138,4 @@ class AcusticSensor:
       traceback.print_exc()
 
   def pass_to_gui(self, data):
-    self.ac_queue.put(("data", data))
-
-class MagneticSensor:
-  def __init__(self):
-    pass
-
-  def start(self):
-    if not conn.isConnected():
-      conn.open()
-    conn.addRecvCallback(self._readCb)
-
-  def _readCb(self, raw):
-    print("mag: ", conn.getMagneticField())
-
-  def calibrate(self, x, y):
-    pass
-
-  def read(self):
-    return conn.getMagneticField()
-
-class OpticalSensor():
-  def __init__(self):
-    self.cap = cv2.VideoCapture(0)
-    self._t  = None
-    self.values = None
-
-  def start(self):
-    if not self._t:
-      self._t = threading.Thread(target=self._getFrames, args=())
-      self._t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
-      self._t.start()
-
-  def _getFrames(self):
-    while True:
-      success, image = self.cap.read()
-      if success:
-        self.values = markerDetection.measureDistances(image)
-        print("opt:", self.values)
-
-  def calibrate(self, x, y):
-    pass
-
-  def read(self):
-    return self.values
-
-if __name__ == "__main__":
-  acc = AcusticSensor()
-  acc.start()
-  mag = MagneticSensor()
-  mag.start()
-  opt = OpticalSensor()
-  opt.start()
-  while True:
-    time.sleep(1)
+    self.ac_queue.put(("data", data))

+ 43 - 0
raspberry-pi/sensors/calibration.py

@@ -0,0 +1,43 @@
+
+class CalibrationStateMashine():
+
+  def __init__(self):
+    self.state = 0
+    self.value_count = 0
+
+    self.NOT_CALIBRATED = 0
+    self.WAITING_POS_1  = 1
+    self.ACCUMULATING_1 = 2
+    self.WAITING_POS_2  = 3
+    self.ACCUMULATING_2 = 4
+    self.CALIBRATION_DONE = 5
+
+  def state_clearname(self):
+    if self.state == self.NOT_CALIBRATED:
+      return "not calibrated"
+    elif self.state == self.WAITING_POS_1:
+      return "Waiting for Position one"
+    elif self.state == self.ACCUMULATING_1:
+      return "gathering values on position one"
+    elif self.state == self.WAITING_POS_2:
+      return "Waiting for position two"
+    elif self.state == self.ACCUMULATING_2:
+      return "gathering values on position two"
+    elif self.state == self.CALIBRATION_DONE:
+      return "calibration done"
+    
+  def next_state(self):
+    if self.state < self.CALIBRATION_DONE:
+      self.state += 1
+      print(self.state_clearname())
+
+  def next_state_gui(self):
+    print("next_state_gui",self.state)
+    if self.state == self.WAITING_POS_1 or self.state == self.WAITING_POS_2:
+      self.next_state()
+
+  def get_state(self):
+    return self.state
+
+  def reset_state(self):
+    self.state = 0

+ 7 - 0
raspberry-pi/connection.py

@@ -144,6 +144,13 @@ class ArduinoSlave(SerialConnection):
         else:
           print("SERIAL: ", data[:-2])
 
+_conn = None
+def globalArduinoSlave():
+  global _conn
+  if not _conn:
+    _conn = ArduinoSlave()
+  return _conn
+
 if __name__ == "__main__":
   arduino = ArduinoSlave()
   def cb(x):

+ 22 - 0
raspberry-pi/sensors/magneticSensor.py

@@ -0,0 +1,22 @@
+from sensors.connection import globalArduinoSlave
+import time
+
+conn = globalArduinoSlave()
+
+class MagneticSensor:
+  def __init__(self):
+    pass
+
+  def start(self):
+    if not conn.isConnected():
+      conn.open()
+    conn.addRecvCallback(self._readCb)
+
+  def _readCb(self, raw):
+    print("mag: ", conn.getMagneticField())
+
+  def calibrate(self, x, y):
+    pass
+
+  def read(self):
+    return conn.getMagneticField()

+ 32 - 0
raspberry-pi/markerDetection.py

@@ -1,5 +1,8 @@
 #!/bin/python3
 
+import time
+import threading
+import traceback
 import numpy as np
 import cv2
 import cv2.aruco as aruco
@@ -61,7 +64,36 @@ def measureDistances(image):
     else:
       return None
 
+class OpticalSensor():
+  def __init__(self):
+    self.cap = cv2.VideoCapture(0)
+    self._t  = None
+    self.values = None
+
+  def start(self):
+    if not self._t:
+      self._t = threading.Thread(target=self._getFrames, args=())
+      self._t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
+      self._t.start()
+
+  def _getFrames(self):
+    while True:
+      success, image = self.cap.read()
+      if success:
+        self.values = measureDistances(image)
+        print("opt:", self.values)
+
+  def calibrate(self, x, y):
+    pass
+
+  def read(self):
+    return self.values
+
 if __name__ == "__main__":
+  # opt = OpticalSensor()
+  # opt.start()
+  # while True:
+  #   time.sleep(1)
   cap = cv2.VideoCapture(0) 
   while True:
     success, image = cap.read()

+ 0 - 4
raspberry-pi/test.py

@@ -1,4 +0,0 @@
-import noise
-
-for i in range(100000):
-    print(noise.pnoise1(i/1000)*1000)