loadCells.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from .GSV4BT import GSV4BT
  2. import time
  3. import bluetooth
  4. import threading
  5. class LoadCells():
  6. def __init__(self):
  7. self.cells = (
  8. GSV4BT("00:0B:CE:04:F6:66"),
  9. GSV4BT("00:0B:CE:04:F6:67"),
  10. GSV4BT("00:0B:CE:04:F6:68"),
  11. )
  12. def connect(self):
  13. success = True
  14. for cell in self.cells:
  15. if cell.isConnected():
  16. pass
  17. elif cell.connect():
  18. cell.requiresSetup = True
  19. else:
  20. success = False
  21. return success
  22. def reconnectThread(self):
  23. while True:
  24. self.connect()
  25. time.sleep(1)
  26. def start(self):
  27. self.thread = threading.Thread(target=self.reconnectThread)
  28. self.thread.start()
  29. def getForces(self, id):
  30. cell = self.cells[id]
  31. if not cell.isConnected():
  32. return None
  33. if cell.requiresSetup:
  34. cell.setNormalMode()
  35. cell.stopTransmission()
  36. cell.setFrequency(20) # Hz
  37. cell.setGain(0, '2mV')
  38. cell.setGain(1, '2mV')
  39. cell.setGain(2, '2mV')
  40. mode = cell.getMode()
  41. if mode and mode[0] == 0x01:
  42. cell.requiresSetup = False
  43. cell.getValue()
  44. return cell.getForces()
  45. def scan(self):
  46. nearby_devices = bluetooth.discover_devices(lookup_names=True)
  47. print("Found {} devices.".format(len(nearby_devices)))
  48. for addr, name in nearby_devices:
  49. print(" {} - {}".format(addr, name))
  50. if __name__ == "__main__":
  51. cells = LoadCells()
  52. cells.scan()
  53. cells.start()
  54. while True:
  55. vals = cells.getForces(0)
  56. if vals:
  57. print('cell 0: ' + ' '.join(["ch {}: {:8.3f} mV/V".format(i, vals[i]) for i in range(len(vals))]))
  58. time.sleep(.3)