-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuaccel.py
More file actions
executable file
·254 lines (214 loc) · 5.7 KB
/
uaccel.py
File metadata and controls
executable file
·254 lines (214 loc) · 5.7 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import time
def file_source():
# nt = time.time() + 0.1
with open('minicom-12-05-ev2.cap', encoding='cp1251') as f:
for l in f:
kws = l.split(',')
t = kws[0]
if t == '$GNRMC':
time = kws[1]
h = int(time[0:2])
m = int(time[2:4])
s = int(time[4:6])
ms = int(time[7:9]) * 10
ltime = h * 3600 + m * 60 + s + ms/1000
status = kws[2]
svel = kws[7]
if not svel:
svel = '0'
vel = float(svel) * 1.85
yield ltime, vel
class plotterLine:
def __init__(self, subplot, min_count, max_count):
self.min_count = min_count
self.max_count = max_count
self.line, = subplot.plot([], [])
self.dataX = []
self.dataY = []
def push(self, x, y):
self.dataX.append(x)
self.dataY.append(y)
if self.max_count > 0 and len(self.dataX) > self.max_count:
self.dataX = self.dataX[1:]
self.dataY = self.dataY[1:]
if self.min_count <= len(self.dataX):
self.line.set_data(self.dataX, self.dataY)
def limits(self):
if self.min_count <= len(self.dataX):
return min(self.dataX), max(self.dataX), min(self.dataY), max(self.dataY)
else:
return None, None, None, None
class plotter:
def __init__(self, min_count, max_count):
self.min_count = min_count
self.max_count = max_count
self.lines = []
plt.ion()
self.fig = plt.figure(figsize=(13, 6))
self.ax = self.fig.add_subplot(111)
self.ax.set_xlim(0, 1)
self.ax.set_ylim(0, 1)
plt.show()
def redraw(self):
self.__update_limits__()
plt.pause(0.0001)
def __lmax__(self, a, b):
if a == None:
return b
if b == None:
return a
if a > b:
return a
return b
def __lmin__(self, a, b):
if a == None:
return b
if b == None:
return a
if a > b:
return b
return a
def __update_limits__(self):
g_xmin = None
g_xmax = None
g_ymin = None
g_ymax = None
for l in self.lines:
xmin, xmax, ymin, ymax = l.limits()
g_xmin = self.__lmin__(g_xmin, xmin)
g_xmax = self.__lmin__(g_xmax, xmax)
g_ymin = self.__lmin__(g_ymin, ymin)
g_ymax = self.__lmin__(g_ymax, ymax)
if g_xmin != None and g_xmax != None and g_ymin != None and g_ymax != None:
self.ax.set_xlim(g_xmin, g_xmax)
self.ax.set_ylim(g_ymin, g_ymax)
def add_line(self, ):
line = plotterLine(self.ax, self.min_count, self.max_count)
self.lines.append(line)
return line
class Kalman:
def __init__(self, K):
self.K = K
self.vPrev = None
self.vCur = None
self.vCurReal = None
self.vPrevReal = None
def next(self, value):
self.update(value)
return self.current()
def update(self, value):
if self.vCur == None:
self.vCur = value
self.vPrev = value
self.vPrevReal = value
self.vCurReal = value
return
vNext = self.K * value + (1 - self.K) * self.vPrev
self.vPrev = self.vCur
self.vCur = vNext
self.vPrevReal = self.vCurReal
self.vCurReal = value
def current(self):
return self.vCur
def real(self):
return self.vCurReal
def delta(self):
return self.vCur - self.vPrev
def deltaReal(self):
return self.vCurReal - self.vPrevReal
class delta:
def __init__(self):
self.prevX = None
self.curX = None
self.prevY = None
self.curY = None
def next(self, x, y):
self.update(x, y)
return self.current()
def update(self, x, y):
self.prevX = self.curX
self.curX = x
self.prevY = self.curY
self.curY = y
def current(self):
if self.prevX == None:
return 0
return (self.curY - self.prevY) / (self.curX - self.prevX)
class CMeasure:
def __init__(self, keys):
self.keys = keys
self.region = None
# def update(self, value):
# l, r = self.region(value)
# self.region
def region(self, value):
print(value, self.keys)
for i in range(1, len(self.keys)):
if value >= self.keys[i-1] and value < self.keys[i]:
return self.keys[i-1], self.keys[i]
return 0
p = plotter(10, 100)
line_orig_vel = p.add_line()
line_kalman1_vel = p.add_line()
#line_kalman2_vel = p.add_line()
line_delta1 = p.add_line()
line_kalman1_delta = p.add_line()
kalman1 = Kalman(0.5)
#kalman2 = Kalman(0.1)
delta1 = delta()
kalman1_delta = Kalman(0.3)
measure = CMeasure([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
source = file_source
for now, vel in source():
print(measure.region(vel))
if vel < 20:
continue
print(now, vel)
line_orig_vel.push(now, vel)
line_kalman1_vel.push(now, kalman1.next(vel))
# line_kalman2_vel.push(now, kalman2.next(vel))
# line_delta1.push(now, delta1.next(now, vel))
# line_kalman1_delta.push(now, kalman1_delta.next(delta1.current()))
p.redraw()
exit(1)
kKalman1 = 0.01
kKalman2 = 0.1
p = plotter(0, 1, 0, 1)
xvec = []
yvecRaw = []
yvecA = []
yvec3 = []
yvec4 = []
line1 = p.add_line()
line2 = p.add_line()
line3 = p.add_line()
line4 = p.add_line()
for now, vel in source():
print(now, vel)
if len(yvec2) == 0:
yK1 = vel
yK2 = vel
acc = 0
else:
yK1 = kKalman1 * vel + (1 - kKalman1) * yvec2[-1]
yK2 = kKalman2 * vel + (1 - kKalman2) * yvec2[-1]
acc1 = (yK1 - yvec2[-1]) / (now - xvec[-1])
acc2 = (yK2 - yvec4[-1]) / (now - xvec[-1])
xvec.append(now)
yvec.append(vel)
yvec2.append(yK1)
yvec3.append(acc)
yvec4.append(yK2)
if len(xvec) > 100:
xvec = xvec[1:]
yvec = yvec[1:]
yvec2 = yvec2[1:]
yvec3 = yvec3[1:]
if len(xvec) > 10:
p.update_limits(xvec[0], xvec[-1], min(yvec+yvec2+yvec3), max(yvec + yvec2 + yvec3))
line1.set_data(xvec, yvec)
line2.set_data(xvec, yvec2)
line3.set_data(xvec, yvec3)
p.refresh()