sensors.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from connection import ArduinoSlave
  2. import markerDetection
  3. import time
  4. import statistics
  5. import math
  6. import threading, cv2
  7. conn = ArduinoSlave()
  8. class AcusticSensor:
  9. def __init__(self):
  10. self.sonic_speed_left = 0
  11. self.sonic_speed_right = 0
  12. self.sensor_distance = 450 #in mm
  13. def start(self):
  14. if not conn.isConnected():
  15. conn.open()
  16. conn.addRecvCallback(self._readCb)
  17. def _readCb(self, raw):
  18. print("acc: ", conn.getAcusticRTTs())
  19. def calibrate(self, distance):
  20. print("Move gondel to far left corner")
  21. input()
  22. speed = list()
  23. for i in range(100):
  24. time_val = [1.312,0]
  25. speed.append(distance/time_val[0])
  26. self.sonic_speed_left = statistics.mean(speed)
  27. print("sonic speed left:",self.sonic_speed_left)
  28. print("Move gondel to far right corner")
  29. input()
  30. speed = list()
  31. for i in range(100):
  32. time_val = [0,1.312]
  33. speed.append(distance/time_val[1])
  34. self.sonic_speed_right = statistics.mean(speed)
  35. print("sonic speed right:",self.sonic_speed_right)
  36. def read(self):
  37. return conn.getAcusticRTTs()
  38. def calculate_position(self):
  39. if not self.sonic_speed_right or not self.sonic_speed_left:
  40. print("sensor not calibrated! calibrate now!")
  41. return (0,0)
  42. else:
  43. time_val = self.read()
  44. distance_left = time_val[0] * self.sonic_speed_left / 1000 # seconds -> mseconds
  45. distance_right = time_val[1] * self.sonic_speed_right / 1000
  46. print(distance_left,distance_right)
  47. x = (self.sensor_distance**2 - distance_right**2 + distance_left**2) / (2*self.sensor_distance)
  48. y = math.sqrt(distance_left**2 - x**2)
  49. return (x,y)
  50. class MagneticSensor:
  51. def __init__(self):
  52. pass
  53. def start(self):
  54. if not conn.isConnected():
  55. conn.open()
  56. conn.addRecvCallback(self._readCb)
  57. def _readCb(self, raw):
  58. print("mag: ", conn.getMagneticField())
  59. def calibrate(self, x, y):
  60. pass
  61. def read(self):
  62. return conn.getMagneticField()
  63. class OpticalSensor():
  64. def __init__(self):
  65. self.cap = cv2.VideoCapture(0)
  66. self._t = None
  67. self.values = None
  68. def start(self):
  69. if not self._t:
  70. self._t = threading.Thread(target=self._getFrames, args=())
  71. self._t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
  72. self._t.start()
  73. def _getFrames(self):
  74. while True:
  75. success, image = self.cap.read()
  76. if success:
  77. self.values = markerDetection.measureDistances(image)
  78. print("opt:", self.values)
  79. def calibrate(self, x, y):
  80. pass
  81. def read(self):
  82. return self.values
  83. if __name__ == "__main__":
  84. acc = AcusticSensor()
  85. acc.start()
  86. mag = MagneticSensor()
  87. mag.start()
  88. opt = OpticalSensor()
  89. opt.start()
  90. while True:
  91. time.sleep(1)