magneticSensor.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from distutils.command.config import config
  2. import queue
  3. import statistics
  4. from struct import calcsize
  5. import numpy as np
  6. import time
  7. from configparser import ConfigParser
  8. from sensors.calibration import CalibrationStateMashine
  9. from sensors.connection import globalArduinoSlave
  10. import logHandler
  11. conn = globalArduinoSlave()
  12. class MagneticSensor:
  13. def __init__(self, conf):
  14. self.conf = conf
  15. self.queue = queue.Queue()
  16. self.calibration_state = CalibrationStateMashine()
  17. self.success = False
  18. self.offset_x = float(conf["mag_sensor"]["off_x"])
  19. self.offset_y = float(conf["mag_sensor"]["off_y"])
  20. self.scale_x = float(conf["mag_sensor"]["scale_x"])
  21. self.scale_y = float(conf["mag_sensor"]["scale_y"])
  22. self.max_x = float(conf["mag_sensor"]["max_x"])
  23. self.max_y = float(conf["mag_sensor"]["max_y"])
  24. self.log_handler = logHandler.get_log_handler()
  25. def start(self):
  26. if not conn.isConnected():
  27. conn.open()
  28. self.success = True
  29. conn.addRecvCallback(self._readCb)
  30. def _readCb(self, raw):
  31. value = conn.getMagneticField()
  32. if value[0] >= 0 and value[1] >= 0:
  33. self.calibrate(value)
  34. position = self.calculate_position(value)
  35. if position != None:
  36. self.pass_to_gui(position + value)
  37. def start_calibration(self): ###
  38. self.calibration_state.reset_state()
  39. self.time_vals = [[],[],[]]
  40. self.calibration_state.next_state()
  41. def calibrate(self, value): ### öffnet erstmal popup :) # how?
  42. if self.calibration_state.get_state() == self.calibration_state.ACCUMULATING_1:
  43. self.time_vals[0].append(value[0])
  44. self.time_vals[1].append(value[1])
  45. self.time_vals[2].append(value[2])
  46. self.calibration_state.progress = len(self.time_vals[0]) / 2
  47. if len(self.time_vals[0]) >= 100:
  48. self.cal_values["front"][0] = statistics.mean(self.time_vals[0])
  49. self.cal_values["front"][1] = statistics.mean(self.time_vals[1])
  50. self.cal_values["front"][2] = statistics.mean(self.time_vals[2])
  51. self.time_vals = [[],[],[]]
  52. self.calibration_state.next_state() # signal gui to get next position
  53. elif self.calibration_state.get_state() == self.calibration_state.ACCUMULATING_2:
  54. self.time_vals[0].append(value[0])
  55. self.time_vals[1].append(value[1])
  56. self.time_vals[2].append(value[2])
  57. self.calibration_state.progress = 50 + len(self.time_vals[0]) / 2
  58. if len(self.time_vals[0]) >= 100:
  59. self.cal_values["back"][0] = statistics.mean(self.time_vals[0])
  60. self.cal_values["back"][1] = statistics.mean(self.time_vals[1])
  61. self.cal_values["back"][2] = statistics.mean(self.time_vals[2])
  62. # all values have been captured
  63. # Hard iron distortion & Soft iron distortion
  64. self.offset_x = (self.cal_values["back"][0] - self.cal_values["front"][0]) / 2
  65. self.offset_y = (self.cal_values["back"][1] - self.cal_values["front"][1]) / 2
  66. avg_delta_x = (self.cal_values["back"][0] - self.cal_values["front"][0]) / 2
  67. avg_delta_y = (self.cal_values["back"][1] - self.cal_values["front"][1]) / 2
  68. avg_delta_z = (self.cal_values["back"][2] - self.cal_values["front"][2]) / 2
  69. avg_delta = (avg_delta_x + avg_delta_y + avg_delta_z) / 3
  70. self.scale_x = avg_delta / avg_delta_x
  71. self.scale_y = avg_delta / avg_delta_y
  72. self.scale_z = avg_delta / avg_delta_z
  73. # max values for placeholder algorithm
  74. self.max_x = (self.cal_values["back"][0] - self.offset_x) * self.scale_x
  75. self.max_y = (self.cal_values["back"][1] - self.offset_y) * self.scale_y
  76. self.calibration_state.next_state()
  77. return self.offset_x, self.offset_y, self.scale_x, self.scale_y, self.max_x, self.max_y
  78. def read(self):
  79. value = conn.getMagneticField()
  80. return value
  81. def calculate_position(self): ###
  82. corrected_x = (conn.getMagneticField[0] - self.offset_x) * self.scale_x
  83. corrected_y = (conn.getMagneticField[1] - self.offset_y) * self.scale_y
  84. # placeholder algorithm (to see if the sensor even works)
  85. x = (corrected_x * 400) / self.max_x
  86. y = (corrected_y * 400) / self.max_y
  87. return (x, y)
  88. def stop(self):
  89. self.log_handler.log_and_print("stop magnetic sensor")
  90. self.success = False
  91. conn.close()
  92. def pass_to_gui(self, data):
  93. self.queue.put(("data", data))