瀏覽代碼

moved print to logging class

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

+ 0 - 2
raspberry-pi/gui/mainWindow.py

@@ -19,7 +19,6 @@ class MainWindow(tk.Frame):
     self.ac_sensor = ac_sensor
     self.ac_queue = ac_queue
     self.log_handler = logHandler.get_log_handler()
-    self.log_handler.add_item("start Main Window")
 
     tk.Frame.__init__(self, root)
     # data plot at left side
@@ -106,7 +105,6 @@ class MainWindow(tk.Frame):
 
   def calibrate(self):
     self.ac_sensor.start_calibration()
-    self.log_handler.add_item("start calibration")
     if not self.popup or not self.pu_root.winfo_exists():
       # create new window
       self.pu_root = tk.Toplevel(self.root)

+ 8 - 4
raspberry-pi/logHandler.py

@@ -5,14 +5,18 @@ class LogList():
     self.log_list = list()
     self.read_index = 0
 
-  def add_item(self,*args):
-    args = [str(i) for i in args]
-    item = " ".join(args)
-    item = str(datetime.datetime.now())+"   "+str(item)
+  def add_item(self, item):
     self.log_list.append(item)
     if len(self.log_list) > 100:
       self.log_list.pop(0)
 
+  def log_and_print(self, *args):
+    args = [str(i) for i in args]
+    item = " ".join(args)
+    item = str(datetime.datetime.now())+"   "+str(item)
+    self.add_item(item)
+    print(item)
+
   def get_log_list(self):
     return self.log_list
 

+ 12 - 14
raspberry-pi/sensors/acusticSensor.py

@@ -27,7 +27,7 @@ class AcusticSensor:
     self.overhead_right         = float(conf["ac_sensor"]["overhead_right"])
 
     self.log_handler = logHandler.get_log_handler()
-    self.log_handler.add_item("start ac_sensor")
+    self.log_handler.log_and_print("start acustic sensor")
 
     # temporary calibration variables
     self.time_vals = [[],[]]
@@ -52,11 +52,12 @@ class AcusticSensor:
     self.calibration_state.next_state()
 
   def stop(self):
-    print("stop acoustic sensor")
+    self.log_handler.log_and_print("stop acustic sensor")
     self.dummyActive = False
     conn.close()
 
   def _readCb_dummy(self):
+    self.log_handler.log_and_print("acustic sensor: generating test values")
     while self.dummyActive:
       value = (900+random.randint(0,300),900+random.randint(0,300))
       value = ((math.sin(self.n)+1)*400+900, (math.cos(self.n)+1)*400+900)
@@ -71,9 +72,11 @@ class AcusticSensor:
       self.calibrate(value)
       self.pass_to_gui(self.calculate_position(value) + value)
       time.sleep(0.01)
+    self.log_handler.log_and_print("acustic sensor: disabled test mode")
 
   def _readCb(self, raw):
-    self.dummyActive = False
+    if self.dummyActive == True:
+      self.dummyActive = False
     value = conn.getAcusticRTTs()
     # partially missing values will be ignored
     if value[0] >= 0 and value[1] >= 0:
@@ -102,8 +105,7 @@ class AcusticSensor:
         self.cal_values["right"][0] = statistics.mean(self.time_vals[1])
 
         # all values have been captured
-        print("calibration measurements:", self.cal_values)
-        self.log_handler.add_item("calibration measurements:", self.cal_values)
+        self.log_handler.log_and_print("calibration measurements:", self.cal_values)
         
         # calculate distances from config
         #          /|                               _.-|
@@ -132,15 +134,11 @@ class AcusticSensor:
         self.sonic_speed = statistics.mean((sonicspeed_1,sonicspeed_2))
         self.overhead_left  = overhead_1
         self.overhead_right = overhead_2
-        print("calibration results:")
-        print("  sonicspeed:     {:8.6f} mm/us".format(self.sonic_speed))
-        print("  overhead_left:  {:8.3f} us".format(self.overhead_left))
-        print("  overhead_right: {:8.3f} us".format(self.overhead_right))
-
-        self.log_handler.add_item("calibration results:")
-        self.log_handler.add_item("  sonicspeed:     {:8.6f} mm/us".format(self.sonic_speed))
-        self.log_handler.add_item("  overhead_left:  {:8.3f} us".format(self.overhead_left))
-        self.log_handler.add_item("  overhead_right: {:8.3f} us".format(self.overhead_right))
+
+        self.log_handler.log_and_print("calibration results:")
+        self.log_handler.log_and_print("  sonicspeed:     {:8.6f} mm/us".format(self.sonic_speed))
+        self.log_handler.log_and_print("  overhead_left:  {:8.3f} us".format(self.overhead_left))
+        self.log_handler.log_and_print("  overhead_right: {:8.3f} us".format(self.overhead_right))
 
         self.calibration_state.next_state()
 

+ 1 - 3
raspberry-pi/sensors/calibration.py

@@ -31,11 +31,9 @@ class CalibrationStateMashine():
   def next_state(self):
     if self.state < self.CALIBRATION_DONE:
       self.state += 1
-      print(self.state_clearname())
+      self.log_handler.log_and_print("calibration state: ",self.state_clearname())
 
   def next_state_gui(self):
-    print("next_state_gui",self.state_clearname())
-    self.log_handler.add_item("next_state_gui",self.state_clearname())
     if self.state == self.WAITING_POS_1 or self.state == self.WAITING_POS_2:
       self.next_state()
 

+ 10 - 20
raspberry-pi/sensors/connection.py

@@ -20,28 +20,23 @@ class SerialConnection:
     if self.port:
       try:
         self._ser = serial.Serial(self.port, baudrate)
-        print("SERIAL: connected to " + self.port)
-        self.log_handler.add_item("SERIAL: connected to " + self.port)
+        self.log_handler.log_and_print("SERIAL: connected to " + self.port)
 
       except serial.SerialException as e:
-        print("SERIAL:", e)
-        self.log_handler.add_item("SERIAL:", e)
+        self.log_handler.log_and_print("SERIAL:", e)
 
     else:
       for port, name, _ in serial.tools.list_ports.comports():
         try:
           self._ser = serial.Serial(port, baudrate)
-          print("SERIAL: connected to " + port +  ", name: " + name)
-          self.log_handler.add_item("SERIAL: connected to " + port +  ", name: " + name)
+          self.log_handler.log_and_print("SERIAL: connected to " + port +  ", name: " + name)
           self.port = port
           break
         except serial.SerialException as e:
-          print("SERIAL:", e)
-          self.log_handler.add_item("SERIAL:", e)
+          self.log_handler.log_and_print("SERIAL:", e)
 
     if not self._ser:
-        print("SERIAL: no device available")
-        self.log_handler.add_item("SERIAL: no device available")
+        self.log_handler.log_and_print("SERIAL: no device available")
     
   def send(self, bytes):
     if self._ser:
@@ -59,12 +54,10 @@ class SerialConnection:
       try:
         return self._ser.read(n)
       except AttributeError as e:
-        print("SERIAL:", e)
-        self.log_handler.add_item("SERIAL:", e)
+        self.log_handler.log_and_print("SERIAL:", e)
         return None
       except serial.SerialException as e:
-        print("SERIAL:", e)
-        self.log_handler.add_item("SERIAL:", e)
+        self.log_handler.log_and_print("SERIAL:", e)
         if self._ser.isOpen():
           self._ser.close()
         self._ser = None
@@ -79,12 +72,10 @@ class SerialConnection:
       try:
         return self._ser.readline()
       except AttributeError as e:
-        print("SERIAL:", e)
-        self.log_handler.add_item("SERIAL:", e)
+        self.log_handler.log_and_print("SERIAL:", e)
         return None
       except serial.SerialException as e:
-        print("SERIAL:", e)
-        self.log_handler.add_item("SERIAL:", e)
+        self.log_handler.log_and_print("SERIAL:", e)
         if self._ser.isOpen():
           self._ser.close()
         self._ser = None
@@ -162,8 +153,7 @@ class ArduinoSlave(SerialConnection):
           for cb in self._recvCbs:
             cb(self.sensorData)
         else:
-          print("SERIAL: ", data[:-2])
-          self.log_handler.add_item("SERIAL: ", data[:-2])
+          self.log_handler.log_and_print("SERIAL: ", data[:-2])
 
 # this allows the usage of a single instance of ArduinoSlave
 _conn = None