Browse Source

added sensor read callbacks

subDesTagesMitExtraKaese 3 years ago
parent
commit
4a71be1551
3 changed files with 102 additions and 12 deletions
  1. 2 0
      .gitignore
  2. 82 11
      raspberry-pi/connection.py
  3. 18 1
      raspberry-pi/sensors.py

+ 2 - 0
.gitignore

@@ -1 +1,3 @@
 .vscode/c_cpp_properties.json
+
+__pycache__

+ 82 - 11
raspberry-pi/connection.py

@@ -1,9 +1,11 @@
+import threading, time, serial
+
 class SerialConnection:
   def __init__(self):
     self._ser = None
     self.port = None
     
-  def open(self, port = None, baudrate = 115200):
+  def open(self, port = None, baudrate = 1000000):
     self.port = port
 
     if self.port == None:
@@ -31,8 +33,9 @@ class SerialConnection:
         print("connected to " + self.port)
       except serial.SerialException:
         pass
-    else:
-      print("connection to port {} failed".format(port))
+
+    if not self._ser:
+        print("connection failed")
     
   def send(self, bytes):
     if self._ser:
@@ -55,23 +58,91 @@ class SerialConnection:
     if not self._ser:
       self.open()
 
+  def readline(self):
+    if self._ser:
+      return self._ser.readline()
+    else:
+      return None
+
   def isConnected(self):
     return self._ser != None
 
   def close(self):
     if self._ser:
       self._ser.close()
+      self._ser = None
 
 class ArduinoSlave(SerialConnection):
   def __init__(self):
-    self.sensorData = (0) * 12
+    super().__init__()
+    self.sensorData = [0] * 12
+    self._recvCbs = []
+    self._t = None
+
+  def open(self, port = None, baudrate = 1000000):
+    super().open(port, baudrate)
+    if not self._t:
+      self._t = threading.Thread(target=self._readSensors, args=())
+      self._t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
+      self._t.start()
+
+  def close(self):
+    super().close()
+    if self._t:
+      self._t._stop()
+
+  def getAcusticRTTs(self): # in microseconds
+    return [
+      int(self.sensorData[0]),
+      int(self.sensorData[1])
+    ]
+
+  def getMagneticField(self): # in mT
+    return [
+      int(self.sensorData[2]) / 1000,
+      int(self.sensorData[3]) / 1000,
+      int(self.sensorData[4]) / 1000
+    ]
+  
+  def getAccelValues(self):
+    return [
+      int(self.sensorData[5]) / 1000,
+      int(self.sensorData[6]) / 1000,
+      int(self.sensorData[7]) / 1000
+    ]
+  
+  def getGyroValues(self):
+    return [
+      int(self.sensorData[8])  / 1000,
+      int(self.sensorData[9])  / 1000,
+      int(self.sensorData[10]) / 1000
+    ]
+  
+  def getTemperature(self): # in °C
+    return int(self.sensorData[11]) / 1000
+
+  def addRecvCallback(self, cb):
+    self._recvCbs.append(cb)
 
-  def open(self):
-    #thread: sensorData = read()
-    pass
+  def _readSensors(self):
+    while True:
+      data = super().readline()
+      if data:
+        data = str(data, "ASCII")
+        vals = data.split('\t')
+        if vals[0] == "DATA:":
+          self.sensorData = vals[1:]
+          for cb in self._recvCbs:
+            cb(self.sensorData)
+        else:
+          print("SERIAL: ", data[:-2])
 
-  def getAcusticRTTs(self):
-    return self.sensorData[0:2]
+if __name__ == "__main__":
+  arduino = ArduinoSlave()
+  def cb(x):
+    print(arduino.getAcusticRTTs(), arduino.getMagneticField(), arduino.getTemperature())
 
-  def getMagneticField(self):
-    return self.sensorData[2:5]
+  arduino.addRecvCallback(cb)
+  arduino.open()
+  while True:
+    time.sleep(1)

+ 18 - 1
raspberry-pi/sensors.py

@@ -1,4 +1,5 @@
 from connection import ArduinoSlave
+import time
 
 conn = ArduinoSlave()
 
@@ -9,6 +10,10 @@ class AcusticSensor:
   def start(self):
     if not conn.isConnected():
       conn.open()
+    conn.addRecvCallback(self._readCb)
+
+  def _readCb(self, raw):
+    print("acc: ", conn.getAcusticRTTs())
 
   def calibrate(self, x, y):
     pass
@@ -23,9 +28,21 @@ class MagneticSensor:
   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 (0, 0)
+    return (0, 0)
+
+if __name__ == "__main__":
+  acc = AcusticSensor()
+  acc.start()
+  mag = MagneticSensor()
+  mag.start()
+  while True:
+    time.sleep(1)