magneticSensor.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import queue
  2. import statistics
  3. from struct import calcsize
  4. import numpy as np
  5. import time
  6. import threading # ?
  7. import random # ?
  8. from configparser import ConfigParser
  9. from sensors.calibration import CalibrationStateMashine
  10. from sensors.connection import globalArduinoSlave
  11. import logHandler
  12. conn = globalArduinoSlave()
  13. class MagneticSensor:
  14. def __init__(self, conf):
  15. self.conf = conf
  16. self.queue = queue.Queue()
  17. self.calibration_state = CalibrationStateMashine()
  18. self.success = False
  19. self.mag_offset_x = float(conf["mag_sensor"]["mag_offset_x"])
  20. self.mag_offset_y = float(conf["mag_sensor"]["mag_offset_y"])
  21. self.log_handler = logHandler.get_log_handler()
  22. def start(self):
  23. if not conn.isConnected():
  24. conn.open()
  25. conn.addRecvCallback(self._readCb)
  26. def _readCb(self, raw):
  27. mag_values = conn.getMagneticField()
  28. print("Magnetic Offsets:", conn.getMagneticOffsets())
  29. if mag_values[0] >= 0 and mag_values[1] >= 0:
  30. pass
  31. #position = self.calculate_position(mag_values) ### MUSS AUF MAG. SENSOR ANGEPASST WERDEN!!!
  32. #if position != None:
  33. # self.pass_to_gui(position + value)
  34. def setOffsets(self):
  35. # Read config file
  36. config_object = ConfigParser()
  37. config_object("config.ini")
  38. # Get mag_sensor section
  39. mag_sensor = config_object["mag_sensor"]
  40. # Update Offsets
  41. mag_sensor["offset_x"] = conn.getMagneticOffsets(0)
  42. mag_sensor["offset_y"] = conn.getMagneticOffsets(1)
  43. def start_calibration(self): ###
  44. self.calibration_state.reset_state()
  45. self.time_vals = [[],[]]
  46. self.calibration_state.next_state()
  47. def calibrate(self, value): ### öffnet erstmal popup :)
  48. #pass
  49. if self.calibration_state.get_state() == self.calibration_state.ACCUMULATING_1:
  50. self.time_vals[0].append(value[0])
  51. self.time_vals[1].append(value[1])
  52. self.calibration_state.progress = len(self.time_vals[0]) / 2
  53. if len(self.time_vals[0]) >= 100:
  54. self.cal_values["front"][0] = statistics.mean(self.time_vals[0])
  55. self.cal_values["front"][1] = statistics.mean(self.time_vals[1])
  56. self.time_vals = [[],[]]
  57. self.calibration_state.next_state() # signal gui to get next position
  58. def read(self):
  59. return conn.getMagneticField()
  60. def stop(self):
  61. self.log_handler.log_and_print("stop magnetic sensor")
  62. conn.close()
  63. def pass_to_gui(self, data):
  64. self.queue.put(("data", data))