magneticSensor.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import queue
  2. import time
  3. <<<<<<< HEAD
  4. import threading
  5. import random
  6. =======
  7. >>>>>>> de61b5b54174156bc78823188e70ac7b94fdde66
  8. from sensors.connection import globalArduinoSlave
  9. import logHandler
  10. conn = globalArduinoSlave()
  11. class MagneticSensor:
  12. def __init__(self, conf):
  13. self.conf = conf
  14. self.queue = queue.Queue()
  15. <<<<<<< HEAD
  16. self.calibration_state = CalibrationStateMashine()
  17. self.log_handler = logHandler.get_log_handler() # neu
  18. self.success = False
  19. self.field_height = float(conf["field"]["y"])
  20. self.field_width = float(conf["field"]["x"])
  21. self.n = 0
  22. #pass
  23. =======
  24. self.log_handler = logHandler.get_log_handler()
  25. self.gyro_x = []
  26. self.gyro_y = []
  27. self.gyro_z = []
  28. self.offset_x = 0 #
  29. self.offset_y = 0 #
  30. self.offset_z = 0 #
  31. >>>>>>> de61b5b54174156bc78823188e70ac7b94fdde66
  32. def start(self):
  33. if not conn.isConnected():
  34. conn.open()
  35. conn.addRecvCallback(self._readCb)
  36. <<<<<<< HEAD
  37. self.dummyActive = True
  38. dummyThread = threading.Thread(target=self._readCb_dummy)
  39. dummyThread.start()
  40. def _readCb(self, raw):
  41. print("mag: ", conn.getMagneticField())
  42. def _readCb_dummy(self):
  43. self.log_handler.log_and_print("acoustic sensor: generating test values")
  44. while self.dummyActive:
  45. dummyValue = 250
  46. position = 20
  47. self.pass_to_gui(position + dummyValue)
  48. def calibrate(self, x, y):
  49. pass
  50. =======
  51. self.calibrate() #
  52. def _readCb(self, raw):
  53. #print("mag: ", conn.getMagneticField())
  54. #print("accel: ", conn.getAccelValues())
  55. print("gyro: ", conn.getGyroValues())
  56. def calibrate(self):
  57. # Gyroscope Calibration
  58. i = 0
  59. while i < 500:
  60. self.gyro_x.append(conn.getGyroValues()) #
  61. self.gyro_y.append(conn.getGyroValues()) #
  62. self.gyro_z.append(conn.getGyroValues()) #
  63. print("gyro_x: %d", self.gyro_x)
  64. i += 1
  65. if i == 500:
  66. self.offset_x = sum(self.gyro_x) / len(self.gyro_x)
  67. self.offset_y = sum(self.gyro_y) / len(self.gyro_y)
  68. self.offset_z = sum(self.gyro_z) / len(self.gyro_z)
  69. #pass
  70. # Accelerometer Calibration
  71. # Magnetometer Calibration
  72. >>>>>>> de61b5b54174156bc78823188e70ac7b94fdde66
  73. def read(self):
  74. return conn.getMagneticField()
  75. def stop(self):
  76. self.log_handler.log_and_print("stop magnetic sensor")
  77. conn.close()
  78. def pass_to_gui(self, data):
  79. self.queue.put(("data", data))