浏览代码

added basic classes

subDesTagesMitExtraKaese 3 年之前
父节点
当前提交
51ef22860c
共有 3 个文件被更改,包括 111 次插入0 次删除
  1. 77 0
      raspberry-pi/connection.py
  2. 3 0
      raspberry-pi/main.py
  3. 31 0
      raspberry-pi/sensors.py

+ 77 - 0
raspberry-pi/connection.py

@@ -0,0 +1,77 @@
+class SerialConnection:
+  def __init__(self):
+    self._ser = None
+    self.port = None
+    
+  def open(self, port = None, baudrate = 115200):
+    self.port = port
+
+    if self.port == None:
+      for port in ["/dev/ttyUSB{}".format(p) for p in range(4)]:
+        try:
+          self._ser = serial.Serial(port, baudrate)
+          print("connected to " + port)
+          self.port = port
+          break
+        except serial.SerialException:
+          pass
+      
+    if self.port == None:
+      for port in ["COM{}".format(p) for p in range(3,20)]:
+        try:
+          self._ser = serial.Serial(port, baudrate)
+          print("connected to " + port)
+          self.port = port
+          break
+        except serial.SerialException:
+          pass
+    else:
+      try:
+        self._ser = serial.Serial(self.port, baudrate)
+        print("connected to " + self.port)
+      except serial.SerialException:
+        pass
+    else:
+      print("connection to port {} failed".format(port))
+    
+  def send(self, bytes):
+    if self._ser:
+      try:
+        return self._ser.write(bytes)
+      except serial.SerialException:
+        self._ser = None
+    if not self._ser:
+      self.open()
+      if not self._ser:
+        time.sleep(30)
+          
+  def read(self, n=64):
+    if self._ser:
+      try:
+        if self._ser.in_waiting:
+          return self._ser.read(n)
+      except serial.SerialException:
+        self._ser = None
+    if not self._ser:
+      self.open()
+
+  def isConnected(self):
+    return self._ser != None
+
+  def close(self):
+    if self._ser:
+      self._ser.close()
+
+class ArduinoSlave(SerialConnection):
+  def __init__(self):
+    self.sensorData = (0) * 12
+
+  def open(self):
+    #thread: sensorData = read()
+    pass
+
+  def getAcusticRTTs(self):
+    return self.sensorData[0:2]
+
+  def getMagneticField(self):
+    return self.sensorData[2:5]

+ 3 - 0
raspberry-pi/main.py

@@ -0,0 +1,3 @@
+from sensors import *
+
+

+ 31 - 0
raspberry-pi/sensors.py

@@ -0,0 +1,31 @@
+from connection import ArduinoSlave
+
+conn = ArduinoSlave()
+
+class AcusticSensor:
+  def __init__(self):
+    pass
+
+  def start(self):
+    if not conn.isConnected():
+      conn.open()
+
+  def calibrate(self, x, y):
+    pass
+
+  def read(self):
+    return (0, 0)
+
+class MagneticSensor:
+  def __init__(self):
+    pass
+
+  def start(self):
+    if not conn.isConnected():
+      conn.open()
+
+  def calibrate(self, x, y):
+    pass
+
+  def read(self):
+    return (0, 0)