magneticSensor.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import queue
  2. from struct import calcsize
  3. import numpy
  4. #import time
  5. import threading # ?
  6. import random # ?
  7. from sensors.connection import globalArduinoSlave
  8. import logHandler
  9. conn = globalArduinoSlave()
  10. class MagneticSensor:
  11. def __init__(self, conf):
  12. self.conf = conf
  13. self.queue = queue.Queue()
  14. self.log_handler = logHandler.get_log_handler() # neu
  15. self.success = False
  16. self.mpu_array = []
  17. self.mpu_gyro_offsets = []
  18. self.log_handler = logHandler.get_log_handler()
  19. def start(self):
  20. if not conn.isConnected():
  21. conn.open()
  22. conn.addRecvCallback(self._readCb)
  23. #self.dummyActive = True
  24. #dummyThread = threading.Thread(target=self._readCb_dummy)
  25. #dummyThread.start()
  26. def _readCb(self, raw):
  27. #print("mag: ", conn.getMagneticField())
  28. #print("accel: ", conn.getAccelValues())
  29. print("gyro: ", conn.getGyroValues())
  30. def calibrate(self, conf):
  31. # Gyroscope Calibration
  32. gyro_x, gyro_y, gyro_z = conn.getGyroValues()
  33. self.mpu_array.append([gyro_x,gyro_y,gyro_z])
  34. # Accelerometer Calibration
  35. accel_x, accel_y, accel_z = conn.getAccelValues()
  36. # Magnetometer Calibration
  37. magneto_x, magneto_y, magneto_z = conn.getMagneticField()
  38. def read(self):
  39. return conn.getMagneticField()
  40. def stop(self):
  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))