Просмотр исходного кода

add Log window and log_handler

w.mueller 3 лет назад
Родитель
Сommit
c1d6e2ab40

+ 3 - 0
raspberry-pi/.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "python.pythonPath": "e:\\Data\\Hochschule\\tracking_system\\lern-tracking-system\\raspberry-pi\\.venv\\Scripts\\python.exe"
+}

+ 16 - 0
raspberry-pi/Log_handler.py

@@ -0,0 +1,16 @@
+import datetime
+
+class log_list():
+    def __init__(self):
+        self.log_list = list()
+
+    def add_item(self,*args):
+        args = [str(i) for i in args]
+        item = " ".join(args)
+        item = str(datetime.datetime.now())+" , "+str(item)
+        self.log_list.append(item)
+        if len(self.log_list) > 100:
+            self.log_list.pop(0)
+
+    def get_log_list(self):
+        return self.log_list

+ 31 - 0
raspberry-pi/gui/Logscreen.py

@@ -0,0 +1,31 @@
+import tkinter as tk
+from tkinter.ttk import Progressbar
+
+class LogScreen(tk.Frame):
+    def __init__(self, root, log_handler):
+        self.root = root
+        tk.Frame.__init__(self, root)
+
+        self.y_scroll = tk.Scrollbar(self.root)
+        self.y_scroll.pack(side="right", fill="y")
+
+        self.x_scroll = tk.Scrollbar(self.root, orient='horizontal')
+        self.x_scroll.pack(side="bottom", fill="x")
+
+        self.textfield = tk.Listbox(self.root, yscrollcommand=self.y_scroll.set, xscrollcommand=self.x_scroll.set)
+        self.textfield.pack(side="left",expand=True, fill=tk.BOTH)
+        for element in log_handler.get_log_list():
+            self.textfield.insert(tk.END, element)
+
+        self.y_scroll.config(command=self.textfield.yview)
+        self.x_scroll.config(command=self.textfield.xview)
+
+        root.bind('<Escape>', self.close)
+    
+    def update(self):
+        if not self.root.winfo_exists():
+            return
+
+    def close(self):
+        if self.root.winfo_exists():
+            self.root.destroy()

+ 27 - 1
raspberry-pi/gui/mainWindow.py

@@ -6,15 +6,20 @@ import numpy as np
 
 from gui.popup import CalibrationPopUp
 from gui.graph import Graph
+from gui.Logscreen import LogScreen
 
 
 class MainWindow(tk.Frame):
-  def __init__(self, root, ac_sensor, ac_queue, ac_cal_state):
+  def __init__(self, root, ac_sensor, ac_queue, ac_cal_state, log_handler):
     self.root = root
     self.popup = None
+    self.log_window = None
     self.ac_cal_state = ac_cal_state
     self.ac_sensor = ac_sensor
     self.ac_queue = ac_queue
+    self.log_handler = log_handler
+    self.log_handler.add_item("start Main Window")
+
     tk.Frame.__init__(self, root)
     # data plot at left side
     self.graph = Graph(self)
@@ -46,6 +51,9 @@ class MainWindow(tk.Frame):
     clear_button = tk.Button(self.controls, text="Clear graph", command=self.graph.clear, height=4)
     clear_button.pack(side="bottom", fill="both")
 
+    logscreen_button = tk.Button(self.controls, text="Log", command=self.open_log, height=2, foreground="red")
+    logscreen_button.pack(side="bottom", fill="both")
+
   def update(self):
     if not self.root.winfo_exists():
       return
@@ -89,11 +97,15 @@ class MainWindow(tk.Frame):
 
     if self.popup:
       self.popup.update()
+    
+    if self.log_window:
+      self.log_window.update()
 
     self.root.after(30, self.update)
 
   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)
@@ -108,3 +120,17 @@ class MainWindow(tk.Frame):
       self.popup = CalibrationPopUp(self.pu_root, self.ac_cal_state)
       self.popup.pack(side="top", fill="both", expand=True)
 
+  def open_log(self):
+    #create new window
+    self.log_root = tk.Toplevel(self.root)
+    self.log_root.wm_transient(self.root)
+    self.log_root.wm_title("Logs")
+    #center
+    x = (self.log_root.winfo_screenwidth()  - 1000) / 2
+    y = (self.log_root.winfo_screenheight() - 400) / 2
+    self.log_root.geometry(f'1000x400+{int(x)}+{int(y)}')
+    # deactivate mainWindow
+    self.log_root.grab_set()
+    self.log_window = LogScreen(self.log_root,self.log_handler)
+    self.log_window.pack(side="top", fill="both", expand=True)
+

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

@@ -1,14 +1,16 @@
 import tkinter as tk
 from tkinter.ttk import Progressbar
+import pyglet
 
 class CalibrationPopUp(tk.Frame):
   def __init__(self, root, calibration_state):
     self.root = root
+    self.font = pyglet.font.add_file("gui/SourceSansPro-Semibold.otf")
     self.calibration_state = calibration_state
     tk.Frame.__init__(self, root)
     self.pendingClose = False
 
-    self.instruction = tk.Label(self,text="Start Calibration", anchor="c",font=("Helvatica", 18, 'bold'))
+    self.instruction = tk.Label(self,text="Start Calibration", anchor="c",font=("SourceSansPro-Semibold"))
     self.instruction.pack(side="top", fill="both", expand=True)
     button = tk.Button(self,text="OK", command=self.calibration_state.next_state_gui, anchor="c",height=1,width=5)
     button.pack(side="top", fill="both", expand=True)

+ 5 - 3
raspberry-pi/main.py

@@ -6,21 +6,23 @@ import queue
 import configparser
 import tkinter as tk
 import traceback
+import Log_handler
 
 conf = configparser.ConfigParser()
 conf.read('config.ini')
 
 def main():
+  log_handler = Log_handler.log_list()
   ac_queue = queue.Queue()
-  ac_calibration_state = CalibrationStateMashine()
-  ac_sensor = AcusticSensor(conf, ac_queue, ac_calibration_state)
+  ac_calibration_state = CalibrationStateMashine(log_handler)
+  ac_sensor = AcusticSensor(conf, ac_queue, ac_calibration_state,log_handler)
 
   try:
     ac_sensor.start()
     root = tk.Tk()
     root.title("Tracking System")
     root.attributes('-fullscreen', True)
-    view = MainWindow(root, ac_sensor, ac_queue, ac_calibration_state)
+    view = MainWindow(root, ac_sensor, ac_queue, ac_calibration_state,log_handler)
     view.pack(side="top", fill="both", expand=True)
     view.update()
     root.mainloop()

+ 11 - 1
raspberry-pi/sensors/acusticSensor.py

@@ -11,7 +11,7 @@ conn = globalArduinoSlave()
 
 
 class AcusticSensor:
-  def __init__(self, conf, ac_queue, calibration_state):
+  def __init__(self, conf, ac_queue, calibration_state,log_handler):
     self.conf = conf
     self.ac_queue               = ac_queue
     self.calibration_state      = calibration_state
@@ -25,6 +25,9 @@ class AcusticSensor:
     self.overhead_left          = float(conf["ac_sensor"]["overhead_left"])
     self.overhead_right         = float(conf["ac_sensor"]["overhead_right"])
 
+    self.log_handler = log_handler
+    self.log_handler.add_item("start ac_sensor")
+
     # temporary calibration variables
     self.time_vals = [[],[]]
     self.cal_values = {
@@ -99,6 +102,7 @@ class AcusticSensor:
 
         # all values have been captured
         print("calibration measurements:", self.cal_values)
+        self.log_handler.add_item("calibration measurements:", self.cal_values)
         
         # calculate distances from config
         #          /|                               _.-|
@@ -131,6 +135,12 @@ class AcusticSensor:
         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.calibration_state.next_state()
 
   def read(self):

+ 5 - 2
raspberry-pi/sensors/calibration.py

@@ -1,7 +1,7 @@
 
 class CalibrationStateMashine():
 
-  def __init__(self):
+  def __init__(self,log_handler):
     self.state = 0
     self.progress = 0
 
@@ -12,6 +12,8 @@ class CalibrationStateMashine():
     self.ACCUMULATING_2 = 4
     self.CALIBRATION_DONE = 5
 
+    self.log_handler = log_handler
+    
   def state_clearname(self):
     if self.state == self.NOT_CALIBRATED:
       return "not calibrated"
@@ -32,7 +34,8 @@ class CalibrationStateMashine():
       print(self.state_clearname())
 
   def next_state_gui(self):
-    print("next_state_gui",self.state)
+    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()