sensors.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from connection import ArduinoSlave
  2. import time
  3. import statistics
  4. import math
  5. import threading
  6. import noise
  7. conn = ArduinoSlave()
  8. class AcusticSensor:
  9. def __init__(self,conf,up_queue,down_queue,calibration_sate):
  10. self.sensor_distance = conf["ac_sensor"]["sensor_distance"]
  11. self.field_heigth = conf["field"]["y"]
  12. self.field_length = conf["field"]["x"]
  13. self.sensor_y_offset = conf["ac_sensor"]["y_offset"]
  14. self.left_sensor_x_offset = conf["ac_sensor"]["left_x_offset"]
  15. self.rigth_sensor_x_offset = conf["ac_sensor"]["rigth_x_offset"]
  16. self.sonic_speed = conf["ac_sensor"]["sonicspeed"]
  17. self.overhead = conf["ac_sensor"]["overhead"]
  18. self.up_queue = up_queue
  19. self.down_queue = down_queue
  20. self.calibration_sate = calibration_sate
  21. def start(self):
  22. self.n = 0.001
  23. self.running = True
  24. if not conn.isConnected():
  25. conn.open()
  26. conn.addRecvCallback(self._readCb)
  27. thread = threading.Thread(target=self._readCb_dummy)
  28. thread.start()
  29. while True:
  30. action = self.down_queue.get()
  31. if action == "calibrate":
  32. self.calibrate()
  33. elif action == "stop":
  34. print("exit Sensor")
  35. self.running = False
  36. thread.join()
  37. break
  38. conn.close()
  39. def _readCb_dummy(self):
  40. while self.running:
  41. value = (980,660)
  42. print("dummy acc: ", value)
  43. if self.calibration_sate >=1 and self.calibration_sate < 101: #first range of calibration values
  44. self.time_vals[0].append(value[0])
  45. self.time_vals[1].append(value[1])
  46. self.calibration_sate += 1
  47. elif self.calibration_sate >=103 and self.calibration_sate < 203: #second range of calibration values
  48. self.time_vals[0].append(value[0])
  49. self.time_vals[1].append(value[1])
  50. self.calibration_sate += 1
  51. else:
  52. self.pass_to_gui(self.calculate_position(value))
  53. time.sleep(1)
  54. self.n += 0.001
  55. def _readCb(self, raw):
  56. value = conn.getAcusticRTTs()
  57. print("acc: ", value)
  58. if self.calibration_sate >=1 and self.calibration_sate < 101: #first range of calibration values
  59. self.time_vals[0].append(value[0])
  60. self.time_vals[1].append(value[1])
  61. self.calibration_sate += 1
  62. elif self.calibration_sate >=103 and self.calibration_sate < 203: #second range of calibration values
  63. self.time_vals[0].append(value[0])
  64. self.time_vals[1].append(value[1])
  65. self.calibration_sate += 1
  66. else:
  67. self.pass_to_gui(self.calculate_position(value))
  68. def calibrate(self):
  69. if not self.calibration_sate == 1:
  70. print("current calibration state:",self.calibration_sate,"need to be 1 to start calibration!")
  71. return
  72. self.time_vals = [[],[]]
  73. while len(self.time_vals) < 100:
  74. print(len(self.time_vals))
  75. time.sleep(1) # wait until 100 values are gathered
  76. #todo add timeout
  77. left_time_1 = statistics.mean(self.time_vals[0])
  78. rigth_time_2 = statistics.mean(self.time_vals[1])
  79. self.calibration_sate += 1 # signal gui to get next position
  80. while self.calibration_sate != 103:
  81. time.sleep(1) # wait till ui is ready for second position
  82. #todo add timeout
  83. self.time_vals = [[],[]]
  84. while len(self.time_vals) < 100:
  85. print(len(self.time_vals))
  86. time.sleep(1) # wait until 100 values are gathered
  87. #todo add timeout
  88. left_time_2 = statistics.mean(self.time_vals[0])
  89. rigth_time_1 = statistics.mean(self.time_vals[1])
  90. self.calibration_sate += 1 # signal gui start of calculation
  91. timedif = left_time_2 - left_time_1
  92. distance_1 = math.sqrt(self.left_sensor_x_offset**2 + (self.sensor_y_offset + self.field_heigth)**2 )
  93. distance_2 = math.sqrt((self.left_sensor_x_offset + self.field_length)**2 + (self.sensor_y_offset + self.field_heigth)**2 )
  94. distancedif = distance_2 - distance_1
  95. sonicspeed_1 = distancedif / timedif
  96. overhead_1 = statistics.mean((left_time_1 - distance_1/sonicspeed_1,left_time_2 - distance_2/sonicspeed_1))
  97. timedif = rigth_time_2 - rigth_time_1
  98. distance_1 = math.sqrt(self.rigth_sensor_x_offset**2 + (self.sensor_y_offset + self.field_heigth)**2 )
  99. distance_2 = math.sqrt((self.rigth_sensor_x_offset + self.field_length)**2 + (self.sensor_y_offset + self.field_heigth)**2 )
  100. distancedif = distance_2 - distance_1
  101. sonicspeed_2 = distancedif / timedif
  102. overhead_2 = statistics.mean((rigth_time_1 - distance_1/sonicspeed_2,rigth_time_2 - distance_2/sonicspeed_2))
  103. self.sonic_speed = statistics.mean((sonicspeed_1,sonicspeed_2))
  104. self.overhead = statistics.mean((overhead_1,overhead_2))
  105. def read(self):
  106. value = conn.getAcusticRTTs()
  107. return value
  108. def calculate_position(self,values):
  109. val1, val2 = values
  110. val1 -= self.overhead
  111. val2 -= self.overhead
  112. distance_left = val1 * self.sonic_speed / 1000 # millisecond -> mikrosecond value of distance is in mm
  113. distance_rigth = val2 * self.sonic_speed / 1000
  114. print(distance_left,distance_rigth)
  115. x = (self.sensor_distance**2 - distance_rigth**2 + distance_left**2) / (2*self.sensor_distance) + self.left_sensor_x_offset
  116. y = math.sqrt(distance_left**2 - x**2) + self.sensor_y_offset
  117. return(x,y)
  118. def pass_to_gui(self,data):
  119. self.up_queue.put(data)
  120. class MagneticSensor:
  121. def __init__(self):
  122. pass
  123. def start(self):
  124. if not conn.isConnected():
  125. conn.open()
  126. conn.addRecvCallback(self._readCb)
  127. def _readCb(self, raw):
  128. print("mag: ", conn.getMagneticField())
  129. def calibrate(self, x, y):
  130. pass
  131. def read(self):
  132. return (0, 0)
  133. if __name__ == "__main__":
  134. acc = AcusticSensor()
  135. acc.start()
  136. mag = MagneticSensor()
  137. mag.start()
  138. while True:
  139. time.sleep(1)