Quellcode durchsuchen

improved ressource cleanup

subDesTagesMitExtraKaese vor 4 Jahren
Ursprung
Commit
22f700b6ab

+ 1 - 0
software/appWindow.py

@@ -157,4 +157,5 @@ class Main(tk.Tk, Table):
     self.forceSensors.stop()
     self.motorEnabled.set(0)
     self.motorController.setDutyCycle(0)
+    self.adc.close()
     self.quit()

+ 2 - 2
software/main.py

@@ -5,9 +5,9 @@ import sys
 from appWindow import Main
 from ui.globals import *
 
+app = Main()
+app.geometry("1280x720")
 try:
-  app = Main()
-  app.geometry("1280x720")
   app.mainloop()
   print('program closed!')
 except KeyboardInterrupt as e:

+ 9 - 3
software/wirelessLoadCell/GSV4BT.py

@@ -68,21 +68,27 @@ class GSV4BT():
     self.name = first_match["name"]
     self.host = first_match["host"]
 
-    self.printWarning("Connecting to \"{}\" on {} port {}".format(self.name, self.host, self.port))
-
     self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
     try:
       ret = self.sock.connect((self.addr, self.port))
     except bluetooth.btcommon.BluetoothError as e:
       self.printError(e)
+      self.sock.close()
+      self.sock = None
       return False
 
+    self.printWarning("Connected to \"{}\" on {} port {}".format(self.name, self.host, self.port))
     self.sock.settimeout(0.3)
     return True
   
   def isConnected(self):
-    return self.sock and True
+    return self.sock != None
   
+  def disconnect(self):
+    if self.sock:
+      self.sock.close()
+      self.sock == None
+
   def sendRaw(self, data):
     if self.sock:
       try:

+ 7 - 5
software/wirelessLoadCell/loadCells.py

@@ -52,6 +52,7 @@ class LoadCells():
   def start(self):
     self.running = True
     self.thread = threading.Thread(target=self.reconnectThread)
+    self.thread.daemon = True
     self.thread.start()
   
 
@@ -66,8 +67,9 @@ class LoadCells():
     self.tareValues[np.isnan(self.tareValues)] = 0
 
   def stop(self):
-    if self.running:
-      self.running = False
+    for cell in self.cells:
+      cell.disconnect()
+    self.running = False
 
   def getForces(self):
     res = np.ndarray(shape=(len(self.cells),3), dtype=float)
@@ -94,9 +96,9 @@ if __name__ == "__main__":
   try:
     cells.start()
     while True:
-      vals = cells.getForces(0)
-      if vals[0] != None:
-        print('cell 0: ' + ' '.join(["ch {}: {:8.3f} mV/V".format(i, vals[i]) for i in range(len(vals))]))
+      for vals in cells.getForces():
+        if not np.isnan(vals[0]):
+          print('cell: ' + ' '.join(["ch {}: {:8.3f} mV/V".format(i, vals[i]) for i in range(len(vals))]))
       time.sleep(.3)