connection.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import threading, time, serial
  2. class SerialConnection:
  3. def __init__(self):
  4. self._ser = None
  5. self.port = None
  6. def open(self, port = None, baudrate = 1000000):
  7. self.port = port
  8. if self.port == None:
  9. for port in ["/dev/ttyUSB{}".format(p) for p in range(4)]:
  10. try:
  11. self._ser = serial.Serial(port, baudrate)
  12. print("connected to " + port)
  13. self.port = port
  14. break
  15. except serial.SerialException:
  16. pass
  17. if self.port == None:
  18. for port in ["COM{}".format(p) for p in range(3,20)]:
  19. try:
  20. self._ser = serial.Serial(port, baudrate)
  21. print("connected to " + port)
  22. self.port = port
  23. break
  24. except serial.SerialException:
  25. pass
  26. else:
  27. try:
  28. self._ser = serial.Serial(self.port, baudrate)
  29. print("connected to " + self.port)
  30. except serial.SerialException:
  31. pass
  32. if not self._ser:
  33. print("connection failed")
  34. def send(self, bytes):
  35. if self._ser:
  36. try:
  37. return self._ser.write(bytes)
  38. except serial.SerialException:
  39. self._ser = None
  40. if not self._ser:
  41. self.open()
  42. if not self._ser:
  43. time.sleep(30)
  44. def read(self, n=64):
  45. if self._ser:
  46. try:
  47. if self._ser.in_waiting:
  48. return self._ser.read(n)
  49. except serial.SerialException:
  50. self._ser = None
  51. if not self._ser:
  52. self.open()
  53. def readline(self):
  54. if self._ser:
  55. return self._ser.readline()
  56. else:
  57. return None
  58. def isConnected(self):
  59. return self._ser != None
  60. def close(self):
  61. if self._ser:
  62. self._ser.close()
  63. self._ser = None
  64. class ArduinoSlave(SerialConnection):
  65. def __init__(self):
  66. super().__init__()
  67. self.sensorData = [0] * 13
  68. self._recvCbs = []
  69. self._t = None
  70. def open(self, port = None, baudrate = 1000000):
  71. super().open(port, baudrate)
  72. if not self._t:
  73. self._t = threading.Thread(target=self._readSensors, args=())
  74. self._t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
  75. self._t.start()
  76. def close(self):
  77. super().close()
  78. if self._t:
  79. self._t._stop()
  80. def getAcusticRTTs(self): # in microseconds
  81. return [
  82. int(self.sensorData[0]) / 16,
  83. int(self.sensorData[1]) / 16
  84. ]
  85. def getMagneticField(self): # in mT
  86. return [
  87. int(self.sensorData[2]) / 1000,
  88. int(self.sensorData[3]) / 1000,
  89. int(self.sensorData[4]) / 1000
  90. ]
  91. def getAccelValues(self):
  92. return [
  93. int(self.sensorData[5]) / 1000,
  94. int(self.sensorData[6]) / 1000,
  95. int(self.sensorData[7]) / 1000
  96. ]
  97. def getGyroValues(self):
  98. return [
  99. int(self.sensorData[8]) / 1000,
  100. int(self.sensorData[9]) / 1000,
  101. int(self.sensorData[10]) / 1000
  102. ]
  103. def getTemperature(self): # in °C
  104. return int(self.sensorData[11]) / 1000
  105. def addRecvCallback(self, cb):
  106. self._recvCbs.append(cb)
  107. def _readSensors(self):
  108. while True:
  109. data = super().readline()
  110. if data:
  111. data = str(data, "ASCII")
  112. vals = data.split('\t')
  113. if vals[0] == "DATA:" and len(vals) >= 13:
  114. self.sensorData = vals[1:]
  115. for cb in self._recvCbs:
  116. cb(self.sensorData)
  117. else:
  118. print("SERIAL: ", data[:-2])
  119. if __name__ == "__main__":
  120. arduino = ArduinoSlave()
  121. def cb(x):
  122. print(arduino.getAcusticRTTs(), arduino.getMagneticField(), arduino.getTemperature())
  123. arduino.addRecvCallback(cb)
  124. arduino.open()
  125. while True:
  126. time.sleep(1)