magneticSensor.py 1.7 KB

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