|
@@ -4,6 +4,8 @@ import statistics
|
|
|
import math
|
|
|
import threading
|
|
|
import noise
|
|
|
+import random
|
|
|
+import traceback
|
|
|
|
|
|
conn = ArduinoSlave()
|
|
|
|
|
@@ -22,87 +24,101 @@ class AcusticSensor:
|
|
|
self.calibration_sate = calibration_sate
|
|
|
|
|
|
def start(self):
|
|
|
- self.n = 0.001
|
|
|
self.running = True
|
|
|
if not conn.isConnected():
|
|
|
conn.open()
|
|
|
conn.addRecvCallback(self._readCb)
|
|
|
-
|
|
|
thread = threading.Thread(target=self._readCb_dummy)
|
|
|
thread.start()
|
|
|
-
|
|
|
while True:
|
|
|
action = self.down_queue.get()
|
|
|
+ print("action",action)
|
|
|
if action == "calibrate":
|
|
|
- self.calibrate()
|
|
|
+ calibration_thread = threading.Thread(target= self.calibrate)
|
|
|
+ calibration_thread.start()
|
|
|
elif action == "stop":
|
|
|
print("exit Sensor")
|
|
|
self.running = False
|
|
|
thread.join()
|
|
|
+ calibration_thread.join()
|
|
|
break
|
|
|
conn.close()
|
|
|
|
|
|
def _readCb_dummy(self):
|
|
|
while self.running:
|
|
|
- value = (980,660)
|
|
|
- print("dummy acc: ", value)
|
|
|
- if self.calibration_sate >=1 and self.calibration_sate < 101:
|
|
|
+ value = (900+random.randint(0,300),900+random.randint(0,300))
|
|
|
+
|
|
|
+ if self.calibration_sate.return_state() == 2:
|
|
|
+ value = (1541+random.randint(-50,50),2076+random.randint(-50,50))
|
|
|
self.time_vals[0].append(value[0])
|
|
|
self.time_vals[1].append(value[1])
|
|
|
- self.calibration_sate += 1
|
|
|
+ self.calibration_sate.add_value()
|
|
|
|
|
|
- elif self.calibration_sate >=103 and self.calibration_sate < 203:
|
|
|
+ elif self.calibration_sate.return_state() == 5:
|
|
|
+ value = (2076+random.randint(-50,50),1541+random.randint(-50,50))
|
|
|
self.time_vals[0].append(value[0])
|
|
|
self.time_vals[1].append(value[1])
|
|
|
- self.calibration_sate += 1
|
|
|
+ self.calibration_sate.add_value()
|
|
|
|
|
|
else:
|
|
|
self.pass_to_gui(self.calculate_position(value))
|
|
|
- time.sleep(1)
|
|
|
- self.n += 0.001
|
|
|
+ time.sleep(0.01)
|
|
|
|
|
|
def _readCb(self, raw):
|
|
|
value = conn.getAcusticRTTs()
|
|
|
print("acc: ", value)
|
|
|
- if self.calibration_sate >=1 and self.calibration_sate < 101:
|
|
|
- self.time_vals[0].append(value[0])
|
|
|
- self.time_vals[1].append(value[1])
|
|
|
- self.calibration_sate += 1
|
|
|
+ if self.calibration_sate.return_state() == 2:
|
|
|
+ self.time_vals[0].append(value[0])
|
|
|
+ self.time_vals[1].append(value[1])
|
|
|
+ self.calibration_sate.add_value()
|
|
|
|
|
|
- elif self.calibration_sate >=103 and self.calibration_sate < 203:
|
|
|
- self.time_vals[0].append(value[0])
|
|
|
- self.time_vals[1].append(value[1])
|
|
|
- self.calibration_sate += 1
|
|
|
+ elif self.calibration_sate.return_state() == 5:
|
|
|
+ self.time_vals[0].append(value[0])
|
|
|
+ self.time_vals[1].append(value[1])
|
|
|
+ self.calibration_sate.add_value()
|
|
|
|
|
|
else:
|
|
|
self.pass_to_gui(self.calculate_position(value))
|
|
|
|
|
|
def calibrate(self):
|
|
|
- if not self.calibration_sate == 1:
|
|
|
- print("current calibration state:",self.calibration_sate,"need to be 1 to start calibration!")
|
|
|
+ if not self.calibration_sate.return_state() == 1:
|
|
|
+ print("current calibration state:",self.calibration_sate.return_state(),"need to be 1 to start calibration!")
|
|
|
return
|
|
|
+ else:
|
|
|
+ print("start calibration")
|
|
|
self.time_vals = [[],[]]
|
|
|
- while len(self.time_vals) < 100:
|
|
|
- print(len(self.time_vals))
|
|
|
+ self.calibration_sate.next_state()
|
|
|
+ while self.calibration_sate.return_value_count() < 100:
|
|
|
+ print("value count",self.calibration_sate.return_value_count())
|
|
|
time.sleep(1)
|
|
|
|
|
|
+ if not self.running:
|
|
|
+ self.calibration_sate.reset_state()
|
|
|
+ return
|
|
|
left_time_1 = statistics.mean(self.time_vals[0])
|
|
|
rigth_time_2 = statistics.mean(self.time_vals[1])
|
|
|
- self.calibration_sate += 1
|
|
|
+ self.calibration_sate.next_state()
|
|
|
|
|
|
- while self.calibration_sate != 103:
|
|
|
+ while self.calibration_sate.return_state() != 4:
|
|
|
time.sleep(1)
|
|
|
|
|
|
+ if not self.running:
|
|
|
+ self.calibration_sate.reset_state()
|
|
|
+ return
|
|
|
|
|
|
self.time_vals = [[],[]]
|
|
|
- while len(self.time_vals) < 100:
|
|
|
- print(len(self.time_vals))
|
|
|
+ self.calibration_sate.reset_value_count()
|
|
|
+ self.calibration_sate.next_state()
|
|
|
+ while self.calibration_sate.return_value_count() < 100:
|
|
|
+ print("value count",self.calibration_sate.return_value_count())
|
|
|
time.sleep(1)
|
|
|
|
|
|
-
|
|
|
+ if not self.running:
|
|
|
+ self.calibration_sate.reset_state()
|
|
|
+ return
|
|
|
left_time_2 = statistics.mean(self.time_vals[0])
|
|
|
rigth_time_1 = statistics.mean(self.time_vals[1])
|
|
|
- self.calibration_sate += 1
|
|
|
+ self.calibration_sate.next_state()
|
|
|
|
|
|
timedif = left_time_2 - left_time_1
|
|
|
distance_1 = math.sqrt(self.left_sensor_x_offset**2 + (self.sensor_y_offset + self.field_heigth)**2 )
|
|
@@ -110,6 +126,7 @@ class AcusticSensor:
|
|
|
distancedif = distance_2 - distance_1
|
|
|
sonicspeed_1 = distancedif / timedif
|
|
|
overhead_1 = statistics.mean((left_time_1 - distance_1/sonicspeed_1,left_time_2 - distance_2/sonicspeed_1))
|
|
|
+ print(left_time_1,distance_1,sonicspeed_1,left_time_2,distance_2,sonicspeed_1)
|
|
|
|
|
|
timedif = rigth_time_2 - rigth_time_1
|
|
|
distance_1 = math.sqrt(self.rigth_sensor_x_offset**2 + (self.sensor_y_offset + self.field_heigth)**2 )
|
|
@@ -120,21 +137,26 @@ class AcusticSensor:
|
|
|
|
|
|
self.sonic_speed = statistics.mean((sonicspeed_1,sonicspeed_2))
|
|
|
self.overhead = statistics.mean((overhead_1,overhead_2))
|
|
|
+ print("calibration result",self.sonic_speed,self.overhead)
|
|
|
+ self.calibration_sate.next_state()
|
|
|
|
|
|
def read(self):
|
|
|
value = conn.getAcusticRTTs()
|
|
|
return value
|
|
|
|
|
|
def calculate_position(self,values):
|
|
|
- val1, val2 = values
|
|
|
- val1 -= self.overhead
|
|
|
- val2 -= self.overhead
|
|
|
- distance_left = val1 * self.sonic_speed / 1000
|
|
|
- distance_rigth = val2 * self.sonic_speed / 1000
|
|
|
- print(distance_left,distance_rigth)
|
|
|
- x = (self.sensor_distance**2 - distance_rigth**2 + distance_left**2) / (2*self.sensor_distance) + self.left_sensor_x_offset
|
|
|
- y = math.sqrt(distance_left**2 - x**2) + self.sensor_y_offset
|
|
|
- return(x,y)
|
|
|
+ try:
|
|
|
+ val1, val2 = values
|
|
|
+ val1 -= self.overhead
|
|
|
+ val2 -= self.overhead
|
|
|
+ distance_left = val1 * self.sonic_speed
|
|
|
+ distance_rigth = val2 * self.sonic_speed
|
|
|
+ x = (self.sensor_distance**2 - distance_rigth**2 + distance_left**2) / (2*self.sensor_distance) + self.left_sensor_x_offset
|
|
|
+ y = math.sqrt(distance_left**2 - x**2) + self.sensor_y_offset
|
|
|
+ return(x,y)
|
|
|
+ except Exception as e:
|
|
|
+ print(values)
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
def pass_to_gui(self,data):
|
|
|
self.up_queue.put(data)
|