magneticSensor.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import queue
  2. import time
  3. import threading
  4. import random
  5. from sensors.calibration import CalibrationStateMashine
  6. from sensors.connection import globalArduinoSlave
  7. import logHandler
  8. conn = globalArduinoSlave()
  9. class MagneticSensor:
  10. def __init__(self, conf):
  11. self.conf = conf
  12. self.queue = queue.Queue()
  13. self.calibration_state = CalibrationStateMashine()
  14. self.log_handler = logHandler.get_log_handler() # neu
  15. self.success = False
  16. self.field_height = float(conf["field"]["y"])
  17. self.field_width = float(conf["field"]["x"])
  18. self.n = 0
  19. #pass
  20. def start(self):
  21. self.log_handler.log_and_print("start acoustic sensor")
  22. if not conn.isConnected():
  23. conn.open()
  24. conn.addRecvCallback(self._readCb)
  25. self.dummyActive = True
  26. dummyThread = threading.Thread(target=self._readCb_dummy)
  27. dummyThread.start()
  28. def _readCb(self, raw):
  29. print("mag: ", conn.getMagneticField())
  30. def _readCb_dummy(self):
  31. self.log_handler.log_and_print("acoustic sensor: generating test values")
  32. while self.dummyActive:
  33. dummyValue = 250
  34. position = 20
  35. self.pass_to_gui(position + dummyValue)
  36. def calibrate(self, x, y):
  37. pass
  38. def read(self):
  39. return conn.getMagneticField()
  40. def stop(self): # neu
  41. self.log_handler.log_and_print("stop magnetic sensor")
  42. conn.close()
  43. def pass_to_gui(self, data):
  44. self.queue.put(("data", data))