def trackLocation(x1, y1, x2, y2, x3, y3, rs1, rs2, rs3): r1 = rs_to_dist.rssi_to_dist(0, rs1, 2) r2 = rs_to_dist.rssi_to_dist(0, rs2, 2) r3 = rs_to_dist.rssi_to_dist(0, rs3, 2) A = 2*x2 - 2*x1 B = 2*y2 - 2*y1 C = r1**2 - r2**2 - x1**2 + x2**2 - y1**2 + y2**2 D = 2*x3 - 2*x2 E = 2*y3 - 2*y2 F = r2**2 - r3**2 - x2**2 + x3**2 - y2**2 + y3**2 x = (C*E - F*B) / (E*A - B*D) y = (C*D - A*F) / (B*D - A*E) return x,y
def localization_with_rssi(json_data): # JSON data handling global img ext_pos = [] json_data = json.loads(json_data) device_id = json_data["dev_id"] sequence_number = json_data["seq_num"] receivers_MAC = json_data["MAC"] receiver_x, receiver_y = get_coords_receiver(receivers_MAC) ## collect data from database rssi = json_data["RSSI"] tx_pow = json_data["tx_pow"] # distance calculation with RSSI value distance = rssi_dis.rssi_to_dist(tx_pow, rssi, RSSI_CONST) acc_ary = [float(json_data["acc_x"]), float(json_data["acc_y"]), float(json_data["acc_z"])] gyr_ary = [float(json_data["gyr_x"]), float(json_data["gyr_y"]), float(json_data["gyr_z"])] mag_ary = [float(json_data["mag_x"]), float(json_data["mag_y"]), float(json_data["mag_z"])] # Adding the device into the device queue if device_id not in device_queue: device_queue[device_id] = {"s_1":[], "s_2":[], "s_3":[], "location" :[], "pos" : default_position, "speed" : [0.0, 0.0]} if sequence_number % 3 == 0: device_queue[device_id]["s_1"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) elif sequence_number % 3 == 1: device_queue[device_id]["s_2"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) else: device_queue[device_id]["s_3"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) # Calculation the Location else: if sequence_number % 3 == 0: # Append the data to the device que device_queue[device_id]["s_1"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_1"]) >= 3: # Do the trilateration if len(device_queue[device_id]["s_1"]) == 3: # calculate the location with the RSSI values calc_location(device_queue[device_id]["s_1"], device_id) # calculate the Earth reference accelration values earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) # print(device_queue[device_id]["speed"]) # Filter data with kalman filter device_queue[device_id]["pos"], device_queue[device_id]["speed"] = partical_filter(device_queue[device_id]["location"], device_queue[device_id]["pos"], device_queue[device_id]["speed"], earth_acc) # print(device_queue[device_id]["speed"]) # ext_pos = device_queue[device_id]["pos"] # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_1"], device_id, ) ) # thr.start() # Cleaning the other two data buckets device_queue[device_id]["s_2"] = [] device_queue[device_id]["s_3"] = [] ##save location in data base elif sequence_number % 3 == 1: device_queue[device_id]["s_2"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_2"]) >= 3: if len(device_queue[device_id]["s_2"]) == 3: calc_location(device_queue[device_id]["s_2"], device_id) earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) # print(device_queue[device_id]["speed"]) device_queue[device_id]["pos"], device_queue[device_id]["speed"] = partical_filter(device_queue[device_id]["location"], device_queue[device_id]["pos"], device_queue[device_id]["speed"], earth_acc) # print(device_queue[device_id]["speed"]) # ext_pos = device_queue[device_id]["pos"] # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_2"], device_id, ) ) # thr.start() device_queue[device_id]["s_1"] = [] device_queue[device_id]["s_3"] = [] ##save location in database else: device_queue[device_id]["s_3"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_3"]) >= 3: if len(device_queue[device_id]["s_3"]) == 3: calc_location(device_queue[device_id]["s_3"], device_id) earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) # print(device_queue[device_id]["speed"]) device_queue[device_id]["pos"], device_queue[device_id]["speed"] = partical_filter(device_queue[device_id]["location"], device_queue[device_id]["pos"], device_queue[device_id]["speed"], earth_acc) # print(device_queue[device_id]["speed"]) # ext_pos = device_queue[device_id]["pos"] # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_3"], device_id, ) ) # thr.start() device_queue[device_id]["s_1"] = [] device_queue[device_id]["s_2"] = []
mean_eay = 0 distance_vals = [] rssi_vals = [] mag_x_vals = [] mag_y_vals = [] mag_z_vals = [] with open('gen_data/stat_gen_data_com_1.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') for row in readCSV: seq_no.append(a) a += 1 # acc_ary.append(row[4:7]) rssi_vals.append(int(row[3])) distance_vals.append(rsd.rssi_to_dist(-75, int(row[3]), 1.8)) mag_x_vals.append(int(float(row[10]))) mag_y_vals.append(int(float(row[11]))) mag_z_vals.append(int(float(row[12]))) acc_ary = [round(float(i), 3) * G_A for i in row[4:7]] new_arr = [a + b for a, b in zip(acc_ary, prev_ar)] prev_ar = [number / 2 for number in new_arr] acc_ary = prev_ar # p_x_ary.append(float(row[4])) # p_y_ary.append(float(row[5])) # p_z_ary.append(float(row[6])) p_x_ary.append(acc_ary[0]) p_y_ary.append(acc_ary[1]) p_z_ary.append(acc_ary[2])
def localization_with_rssi(json_data): # JSON data handling json_data = json.loads(json_data) device_id = json_data["dev_id"] sequence_number = json_data["seq_num"] receivers_MAC = json_data["MAC"] receiver_x, receiver_y = get_coords_receiver( receivers_MAC) ## collect data from database rssi = json_data["RSSI"] tx_pow = -75 # json_data["tx_pow"] global temp_position global prev_earth_accelerations # distance calculation with RSSI value distance = rssi_dis.rssi_to_dist(tx_pow, rssi, RSSI_CONST) acc_ary = [ float(json_data["acc_x"]), float(json_data["acc_y"]), float(json_data["acc_z"]) ] gyr_ary = [ float(json_data["gyr_x"]), float(json_data["gyr_y"]), float(json_data["gyr_z"]) ] mag_ary = [ float(json_data["mag_x"]), float(json_data["mag_y"]), float(json_data["mag_z"]) ] # calculate the Earth reference accelration values earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) #Do some prev work temp_acc = earth_acc earth_acc = [(prev_earth_accelerations[0] + earth_acc[0]) / 2, (prev_earth_accelerations[1] + earth_acc[1]) / 2, (prev_earth_accelerations[2] + earth_acc[2]) / 2] prev_earth_accelerations = temp_acc # Adding the device into the device queue if device_id not in device_queue: device_queue[device_id] = { "s_1": [], "s_2": [], "s_3": [], "location": [], "pos": default_position, "speed": [0.0, 0.0], "var": default_variance, } if sequence_number % 3 == 0: device_queue[device_id]["s_1"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) elif sequence_number % 3 == 1: device_queue[device_id]["s_2"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) else: device_queue[device_id]["s_3"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) # Calculation the Location else: if sequence_number % 3 == 0: # Append the data to the device que device_queue[device_id]["s_1"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_1"]) == 1: device_queue[device_id]["pos"] = temp_position # Speed values update speed_S = device_queue[device_id]["speed"][ 0] + dt * earth_acc[0] speed_E = device_queue[device_id]["speed"][ 1] + dt * earth_acc[1] # print (earth_acc[0], earth_acc[1]) ## Prediction phase of the kalman filter # position prediction using IMU data pos_S_bar = device_queue[device_id]["pos"][0] + dt * speed_S pos_E_bar = device_queue[device_id]["pos"][1] + dt * speed_E temp_position = [pos_S_bar, pos_E_bar] if len(device_queue[device_id]["s_1"]) >= 3: # Do the trilateration if len(device_queue[device_id]["s_1"]) == 3: # calculate the location with the RSSI values calc_location(device_queue[device_id]["s_1"], device_id) # calculate the Earth reference accelration values # earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) # Filter data with kalman filter device_queue[device_id]["pos"], device_queue[device_id][ "speed"], device_queue[device_id][ "var"] = kalman_filter( device_queue[device_id]["location"], device_queue[device_id]["pos"], device_queue[device_id]["speed"], device_queue[device_id]["var"], earth_acc) temp_position = device_queue[device_id]["pos"] print(device_queue[device_id]["pos"]) # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_1"], device_id, ) ) # thr.start() # Cleaning the other two data buckets device_queue[device_id]["s_2"] = [] device_queue[device_id]["s_3"] = [] ##save location in data base elif sequence_number % 3 == 1: device_queue[device_id]["s_2"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_2"]) == 1: device_queue[device_id]["pos"] = temp_position # Speed values update speed_S = device_queue[device_id]["speed"][ 0] + dt * earth_acc[0] speed_E = device_queue[device_id]["speed"][ 1] + dt * earth_acc[1] # print (earth_acc[0], earth_acc[1]) ## Prediction phase of the kalman filter # position prediction using IMU data pos_S_bar = device_queue[device_id]["pos"][0] + dt * speed_S pos_E_bar = device_queue[device_id]["pos"][1] + dt * speed_E temp_position = [pos_S_bar, pos_E_bar] if len(device_queue[device_id]["s_2"]) >= 3: if len(device_queue[device_id]["s_2"]) == 3: calc_location(device_queue[device_id]["s_2"], device_id) # earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) device_queue[device_id]["pos"], device_queue[device_id][ "speed"], device_queue[device_id][ "var"] = kalman_filter( device_queue[device_id]["location"], device_queue[device_id]["pos"], device_queue[device_id]["speed"], device_queue[device_id]["var"], earth_acc) temp_position = device_queue[device_id]["pos"] print(device_queue[device_id]["pos"]) # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_2"], device_id, ) ) # thr.start() device_queue[device_id]["s_1"] = [] device_queue[device_id]["s_3"] = [] ##save location in database else: device_queue[device_id]["s_3"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_3"]) == 1: device_queue[device_id]["pos"] = temp_position # Speed values update speed_S = device_queue[device_id]["speed"][ 0] + dt * earth_acc[0] speed_E = device_queue[device_id]["speed"][ 1] + dt * earth_acc[1] # print (earth_acc[0], earth_acc[1]) ## Prediction phase of the kalman filter # position prediction using IMU data pos_S_bar = device_queue[device_id]["pos"][0] + dt * speed_S pos_E_bar = device_queue[device_id]["pos"][1] + dt * speed_E temp_position = [pos_S_bar, pos_E_bar] if len(device_queue[device_id]["s_3"]) >= 3: if len(device_queue[device_id]["s_3"]) == 3: calc_location(device_queue[device_id]["s_3"], device_id) # earth_acc = imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) device_queue[device_id]["pos"], device_queue[device_id][ "speed"], device_queue[device_id][ "var"] = kalman_filter( device_queue[device_id]["location"], device_queue[device_id]["pos"], device_queue[device_id]["speed"], device_queue[device_id]["var"], earth_acc) temp_position = device_queue[device_id]["pos"] print(device_queue[device_id]["pos"]) # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_3"], device_id, ) ) # thr.start() device_queue[device_id]["s_1"] = [] device_queue[device_id]["s_2"] = []
def localization_with_rssi(self, json_data, first_signal): # JSON data handling # print('jj') json_data = json.loads(json_data) device_id = json_data["dev_id"] sequence_number = json_data["seq_no"] receivers_MAC = json_data["mac"] # print('ff') receiver_x, receiver_y = self.get_coords_receiver( receivers_MAC) ## collect data from database rssi = json_data["rssi"] # print(rssi) tx_pow = self.tx_power # distance calculation with RSSI value distance = rssi_dis.rssi_to_dist(tx_pow, rssi, self.RSSI_CONST) acc_ary = [ float(json_data["acc_x"]), float(json_data["acc_y"]), float(json_data["acc_z"]) ] gyr_ary = [ float(json_data["gyr_x"]), float(json_data["gyr_y"]), float(json_data["gyr_z"]) ] mag_ary = [ float(json_data["mag_x"]), float(json_data["mag_y"]), float(json_data["mag_z"]) ] # Adding the device into the device queue if first_signal: if sequence_number % 3 == 0: self.device_data["s_1"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) elif sequence_number % 3 == 1: self.device_data["s_2"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) else: self.device_data["s_3"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) # Calculation the Location else: if sequence_number % 3 == 0: # Append the data to the device que self.device_data["s_1"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) if len(self.device_data["s_1"]) == 1: # calculate the Earth reference accelration values self.earth_accelerations = self.imu_earth_ref_accs( acc_ary, gyr_ary, mag_ary) # averaging the acceleration data temp_acc = self.earth_accelerations self.earth_accelerations = [ (self.prev_earth_accelerations[0] + self.earth_accelerations[0]) / 2, (self.prev_earth_accelerations[1] + self.earth_accelerations[1]) / 2, (self.prev_earth_accelerations[2] + self.earth_accelerations[2]) / 2 ] self.prev_earth_accelerations = temp_acc self.device_data["pos"] = self.temp_position # Speed values update speed_S = self.device_data["speed"][ 0] + self.dt * self.earth_accelerations[0] speed_E = self.device_data["speed"][ 1] + self.dt * self.earth_accelerations[1] # print (earth_acc[0], earth_acc[1]) ## Prediction phase of the kalman filter # position prediction using IMU data pos_S_bar = self.device_data["pos"][0] + self.dt * speed_S pos_E_bar = self.device_data["pos"][1] + self.dt * speed_E self.temp_position = [pos_S_bar, pos_E_bar] if len(self.device_data["s_1"]) >= 3: # Do the trilateration if len(self.device_data["s_1"]) == 3: # calculate the location with the RSSI values self.calc_location(self.device_data["s_1"], device_id) # Filter data with kalman filter self.device_data["pos"], self.device_data[ "speed"], self.device_data[ "var"] = self.kalman_filter( self.device_data["location"], self.device_data["pos"], self.device_data["speed"], self.device_data["var"], self.earth_accelerations) self.temp_position = self.device_data["pos"] print(self.device_data["pos"]) # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_1"], device_id, ) ) # thr.start() # Cleaning the other two data buckets self.device_data["s_2"] = [] self.device_data["s_3"] = [] #save location in data base sql = "UPDATE position SET building_id = %s, x_position = %s, y_position = %s, variance = %s WHERE device_id = %s" # sql = "UPDATE position SET building_id = %s, x_position = %s, y_position = %s WHERE device_id = %s" val = (str(self.building_id), str(self.device_data["pos"][0]), str(self.device_data["pos"][1]), str(self.device_data["var"][0] + self.device_data["var"][1]), str(device_id)) self.ils_cursor.execute(sql, val) self.ils_db.commit() elif sequence_number % 3 == 1: self.device_data["s_2"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) if len(self.device_data["s_2"]) == 1: # calculate the Earth reference accelration values self.earth_accelerations = self.imu_earth_ref_accs( acc_ary, gyr_ary, mag_ary) # averaging the acceleration data temp_acc = self.earth_accelerations self.earth_accelerations = [ (self.prev_earth_accelerations[0] + self.earth_accelerations[0]) / 2, (self.prev_earth_accelerations[1] + self.earth_accelerations[1]) / 2, (self.prev_earth_accelerations[2] + self.earth_accelerations[2]) / 2 ] self.prev_earth_accelerations = temp_acc self.device_data["pos"] = self.temp_position # Speed values update speed_S = self.device_data["speed"][ 0] + self.dt * self.earth_accelerations[0] speed_E = self.device_data["speed"][ 1] + self.dt * self.earth_accelerations[1] # print (earth_acc[0], earth_acc[1]) ## Prediction phase of the kalman filter # position prediction using IMU data pos_S_bar = self.device_data["pos"][0] + self.dt * speed_S pos_E_bar = self.device_data["pos"][1] + self.dt * speed_E self.temp_position = [pos_S_bar, pos_E_bar] if len(self.device_data["s_2"]) >= 3: if len(self.device_data["s_2"]) == 3: self.calc_location(self.device_data["s_2"], device_id) # earth_acc = self.imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) self.device_data["pos"], self.device_data[ "speed"], self.device_data[ "var"] = self.kalman_filter( self.device_data["location"], self.device_data["pos"], self.device_data["speed"], self.device_data["var"], self.earth_accelerations) self.temp_position = self.device_data["pos"] print(self.device_data["pos"]) # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_2"], device_id, ) ) # thr.start() self.device_data["s_1"] = [] self.device_data["s_3"] = [] #save location in database sql = "UPDATE position SET building_id = %s, x_position = %s, y_position = %s, variance = %s WHERE device_id = %s" # sql = "UPDATE position SET building_id = %s, x_position = %s, y_position = %s WHERE device_id = %s" val = (str(self.building_id), str(self.device_data["pos"][0]), str(self.device_data["pos"][1]), str(self.device_data["var"][0] + self.device_data["var"][1]), str(device_id)) self.ils_cursor.execute(sql, val) self.ils_db.commit() # print(str(self.device_data["var"][0] + self.device_data["var"][1])) # print(self.ils_cursor.rowcount, "record(s) affected") else: self.device_data["s_3"].append( [distance, receivers_MAC, (receiver_x, receiver_y)]) if len(self.device_data["s_3"]) == 1: # calculate the Earth reference accelration values self.earth_accelerations = self.imu_earth_ref_accs( acc_ary, gyr_ary, mag_ary) # averaging the acceleration data temp_acc = self.earth_accelerations self.earth_accelerations = [ (self.prev_earth_accelerations[0] + self.earth_accelerations[0]) / 2, (self.prev_earth_accelerations[1] + self.earth_accelerations[1]) / 2, (self.prev_earth_accelerations[2] + self.earth_accelerations[2]) / 2 ] self.prev_earth_accelerations = temp_acc self.device_data["pos"] = self.temp_position # Speed values update speed_S = self.device_data["speed"][ 0] + self.dt * self.earth_accelerations[0] speed_E = self.device_data["speed"][ 1] + self.dt * self.earth_accelerations[1] # print (earth_acc[0], earth_acc[1]) ## Prediction phase of the kalman filter # position prediction using IMU data pos_S_bar = self.device_data["pos"][0] + self.dt * speed_S pos_E_bar = self.device_data["pos"][1] + self.dt * speed_E self.temp_position = [pos_S_bar, pos_E_bar] if len(self.device_data["s_3"]) >= 3: if len(self.device_data["s_3"]) == 3: self.calc_location(self.device_data["s_3"], device_id) # earth_acc = self.imu_earth_ref_accs(acc_ary, gyr_ary, mag_ary) self.device_data["pos"], self.device_data[ "speed"], self.device_data[ "var"] = self.kalman_filter( self.device_data["location"], self.device_data["pos"], self.device_data["speed"], self.device_data["var"], self.earth_accelerations) self.temp_position = self.device_data["pos"] print(self.device_data["pos"]) # thr = Thread(target=calc_location, args=(device_queue[device_id]["s_3"], device_id, ) ) # thr.start() self.device_data["s_1"] = [] self.device_data["s_2"] = [] # save location in data sql = "UPDATE position SET building_id = %s, x_position = %s, y_position = %s, variance = %s WHERE device_id = %s" # sql = "UPDATE position SET building_id = %s, x_position = %s, y_position = %s WHERE device_id = %s" val = (str(self.building_id), str(self.device_data["pos"][0]), str(self.device_data["pos"][1]), str(self.device_data["var"][0] + self.device_data["var"][1]), str(device_id)) self.ils_cursor.execute(sql, val) self.ils_db.commit()
def localization_with_rssi(json_data): json_data = json.loads(json_data) device_id = json_data["dev_id"] sequence_number = json_data["seq_num"] receivers_MAC = json_data["MAC"] receiver_x, receiver_y = get_coords_receiver(receivers_MAC) ## collect data from database rssi = json_data["RSSI"] tx_pow = json_data["tx_pow"] distance = rssi_dis.rssi_to_dist(tx_pow, rssi, 1.8) # if receivers_MAC == "24:6F:28:A9:64:C8": # dist_esp1.append(distance) # if receivers_MAC == "24:6F:28:A9:83:C8": # dist_esp2.append(distance) # if receivers_MAC == "24:6F:28:A9:87:40": # dist_esp3.append(distance) if device_id not in device_queue: device_queue[device_id] = {"s_1":[], "s_2":[], "s_3":[], "location" :[]} if sequence_number % 3 == 0: device_queue[device_id]["s_1"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) elif sequence_number % 3 == 1: device_queue[device_id]["s_2"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) else: device_queue[device_id]["s_3"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) else: if sequence_number % 3 == 0: device_queue[device_id]["s_1"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_1"]) >= 3: if len(device_queue[device_id]["s_1"]) == 3: calc_location(device_queue[device_id]["s_1"], device_id) #thr = Thread(target=calc_location, args=(device_queue[device_id]["s_1"], device_id, ) ) #thr.start() # print("1") device_queue[device_id]["s_2"] = [] device_queue[device_id]["s_3"] = [] # print (device_queue[device_id]["s_1"]) ##save location in data base elif sequence_number % 3 == 1: device_queue[device_id]["s_2"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_2"]) >= 3: if len(device_queue[device_id]["s_2"]) == 3: calc_location(device_queue[device_id]["s_2"], device_id) #thr = Thread(target=calc_location, args=(device_queue[device_id]["s_2"], device_id, ) ) #thr.start() # print("2") device_queue[device_id]["s_1"] = [] device_queue[device_id]["s_3"] = [] ##save location in database else: device_queue[device_id]["s_3"].append([distance, receivers_MAC, (receiver_x, receiver_y)]) if len(device_queue[device_id]["s_3"]) >= 3: if len(device_queue[device_id]["s_3"]) == 3: calc_location(device_queue[device_id]["s_3"], device_id) #thr = Thread(target=calc_location, args=(device_queue[device_id]["s_3"], device_id, ) ) #thr.start() # print("3") device_queue[device_id]["s_1"] = [] device_queue[device_id]["s_2"] = []