-
Notifications
You must be signed in to change notification settings - Fork 1
/
WidgetDashboard.py
221 lines (171 loc) · 6.65 KB
/
WidgetDashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
-----------------------------------------------------------
WidgetDashboard
-----------------------------------------------------------
"""
import math
import numpy as np
from math import exp
from kivy.clock import Clock, mainthread
from kivy.event import EventDispatcher
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.behaviors import DragBehavior
from kivy.uix.floatlayout import FloatLayout
from WidgetStation import WidgetStation
class WidgetDashboard(FloatLayout):
def __init__(self,**kwargs):
super(WidgetDashboard, self).__init__(**kwargs)
self.size_hint = (1,1)
self.orientation = "vertical"
beacon_coords = {}
station_coords = {}
_widthMeters = 41
px_scale_factor = 300
_power = -69
_A = -41
_N = 4
#
# on_ble_new_station
#
@mainthread
def on_ble_new_station(self, *args):
_key = args[1]
_obj = args[2]
if _key not in self.station_coords:
self.station_coords[_key] = WidgetStation( source='assets/icon_1.png', pos=self.pos)
self.add_widget( self.station_coords.get(_key) )
# расстановка станций по углам
_l = len(self.station_coords)
if "88" in _key:
self.station_coords[_key].pos = (20,20)
if "CC" in _key:
self.station_coords[_key].pos = (300,300)
if "4C" in _key:
self.station_coords[_key].pos = (300,20)
#
# on_ble_update_event
#
@mainthread
def on_ble_update_event(self, *args):
_station = args[1][0]
_beacons = args[1][1]
for key in _beacons:
if len(_beacons[key]) >= 3 and len(_station) >= 3:
# Если не наша метка пропускаем
if key != "9c:9c:1f:10:1b:46":
continue
# Отладка
# значения RSSI до маячка с 3 станций
#print("on_ble_data -> "+key+" :" + str(_beacons[key]) )
# Добавляем картинку если новый объект
if key not in self.beacon_coords:
self.beacon_coords[key] = WidgetStation( source='assets/icon_2.png', pos=self.pos, size=(10,10))
self.add_widget( self.beacon_coords.get(key) )
pass
# CALCULATE POSITION COORDINATES
coords = self.FindPosition( _beacons[key], _station, float(self.px_scale_factor / 1.85) )
#print(coords)
if coords != None:
self.beacon_coords.get(key).pos = coords
else:
print("Failed to locate:"+key+str(_beacons[key]));
pass
pass
#
# Трилатерация
#
def FindPosition(self, beacon, stations, px_meter):
_beacons = sorted( beacon.keys() )
_station = self.station_coords
# Станция 1
node_1_x = _station[_beacons[1]].pos[0]
node_1_y = _station[_beacons[1]].pos[1]
node_1_dst = self.CalculateDistance( beacon[_beacons[1]]['rssi'], px_meter )
# Станция 2
node_2_x = _station[_beacons[2]].pos[0]
node_2_y = _station[_beacons[2]].pos[1]
node_2_dst = self.CalculateDistance( beacon[_beacons[2]]['rssi'], px_meter )
# Станция 3
node_3_x = _station[_beacons[0]].pos[0]
node_3_y = _station[_beacons[0]].pos[1]
node_3_dst = self.CalculateDistance( beacon[_beacons[0]]['rssi'], px_meter )
_input =[
[ node_1_x, node_1_y, node_1_dst],
[ node_2_x, node_2_y, node_2_dst],
[ node_3_x, node_2_y, node_3_dst]
]
# Отладка
print("trilat_dist:",node_1_dst,node_2_dst,node_3_dst, self._A, self._N, self.px_scale_factor)
_output = self.Trilat(_input)
_coord = (
_output[0],
_output[1]
)
return _coord
#
# CalculateDistance
#
def CalculateDistance(self, rssi, px_meter):
# Вариант 1
# https://medium.com/beingcoders/convert-rssi-value-of-the-ble-bluetooth-low-energy-beacons-to-meters-63259f307283
_P = self._A # beacon broadcast power in dBm at 1 m (Tx Power)
_S = int(rssi) # measured signal value (RSSI) in dBm
_n = self._N # environmental factor
#_d = math.pow(10, (_P-_S) / (10*_n))
_d = math.pow(10,((_S-_P)/(-10*_n))) * px_meter
return _d
# Вариант 2
###расчет через опорную точку ( координаты расчитаны )
#A = self._A #-47.370
#N = self._N #-67.1
#return exp((A-int(rssi))/N) *px_meter
# Вариант 3
# От Android'a
# ratio = rssi*1.0/self._A;
#
# if ratio < 1.0 :
# return math.pow(ratio, (_P-_S) / (10*_n)) *px_meter #math.pow(ratio,10)
# else:
# return ((0.89976)*math.pow(ratio,7.7095) + 0.111)*px_meter
def Trilat(self, input):
try:
# Координаиты 1 станции
x1 = Xa = input[0][0]
y1 = Ya = input[0][1]
# Координаиты 2 станции
x2 = Xb = input[1][0]
y2 = Yb = input[1][1]
# Координаиты 3 станции
x3 = Xc = input[2][0]
y3 = Yc = input[2][1]
r1= dist_A = (input[0][2])
r2= dist_B = (input[1][2])
r3= dist_C = (input[2][2])
# ВАРИАНТ 1
Va = ((Xc**2 - Xb**2) + (Yc**2 - Yb**2) + (dist_B**2 - dist_C**2))/2
Vb = ((Xa**2 - Xb**2) + (Ya**2 - Yb**2) + (dist_B**2 - dist_A**2))/2
y = (Vb*(Xb-Xc)-Va*(Xb-Xa))/((Ya-Yb)*(Xb-Xc)-(Yc-Yb)*(Xb-Xc))
x = -1 * (Va+y*(Yb-Yc))/(Xb-Xc)
# ВАРИАНТ 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)
except:
print("Trilateration: divide_error")
return (0,0)
#
# Drag & Drop
#
def handle_drag_release(self, index, drag_widget):
print("handle_drag_release")
#self.add_widget(drag_widget, index)
pass