loadCells.py 1.9 KB

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