-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering.py
246 lines (208 loc) · 8.47 KB
/
filtering.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
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
'''
Author: Chengkun Li
LastEditors: Chengkun Li
Date: 2021-11-25 00:32:13
LastEditTime: 2021-12-02 19:09:02
Description: All functions pertain to localization of the thymio using Kalman Filter.
FilePath: /Mobile_Soccer/filtering.py
'''
import numpy as np
import matplotlib.pyplot as plt
from loguru import logger
from geo import *
class KF:
"""Kalman Filter Class
The state of Thymio is encoded as [Px, Py, Rotation]
Reference paper: https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.1021.234&rep=rep1&type=pdf
"""
def __init__(self, init_state, init_cov, qx, qy, qtheta, rl, rr, b, linearize=False) -> None:
"""Initialization
Args:
init_state (State): initial state of thymio
init_cov (np.array): inital covariance of thymio
px (float): x position of thymio
py (float): y position of Thymio
theta (float): rotation angle of thymio (in rads)
qx (float): variance on x localization
qy (float): variance on y localization
qtheta (float): variance of rotation
rl (float): variance of left encoder
rr (float): variance of right encoder
b (float): distance between two wheels of robot
"""
self.b = b # constant: distance between wheels
# self.H is omitted since H here is an eye(3)
# Covariance matrix in the measurement stage
self.Q = np.array([
[qx, 0, 0],
[0, qy, 0],
[0, 0, qtheta]
])
# Covariance matrix in the action stage
self.R = np.array([
[rr, 0],
[0, rl]
])
self.linearize = linearize
# States and Covariance history
self.states = [init_state]
self.covs = [init_cov]
def update_params(self, qx ,qy, qtheta, rl, rr):
"""Set the params of covariance matrix on the fly
Args:
qx (float): variance on x localization
qy (float): variance on y localization
qtheta (float): variance of rotation
rl (float): variance of left encoder
rr (float): variance of right encoder
"""
# Covariance matrix in the measurement stage
self.Q = np.array([
[qx, 0, 0],
[0, qy, 0],
[0, 0, qtheta]
])
# Covariance matrix in the action stage
self.R = np.array([
[rr, 0],
[0, rl]
])
def __A_mat(self, theta, D, T):
"""Matrix A of state equation
Args:
theta (float): current orientation of thymio
D (float): distance displacement; calculated as the mean of left and right
T (float): rotation displacement; calculated by the difference between two
encoders divided by wheel distance b
Returns:
[type]: [description]
"""
A = np.array([
[1, 0, -D * np.sin(theta + 0.5*T)],
[0, 1, D * np.cos(theta + 0.5*T)],
[0, 0, 1]
])
return A
def __B_mat(self, theta, D, T):
"""Matrix B of state equation
Args:
theta (float): current orientation of thymio
D (float): distance displacement (calculated as the mean of left and right)
T (float): rotation displacement
Returns:
[type]: [description]
"""
B = np.array([
[0.5*np.cos(theta+0.5*T)-D/(2*self.b)*np.sin(theta+0.5*T), \
0.5*np.cos(theta+0.5*T)+D/(2*self.b)*np.sin(theta+0.5*T)],
[0.5*np.sin(theta+0.5*T)+D/(2*self.b)*np.cos(theta+0.5*T), \
0.5*np.sin(theta+0.5*T)-D/(2*self.b)*np.cos(theta+0.5*T)],
[1/self.b, -1/self.b],
])
return B
@logger.catch
def plot_gaussian(self, factor=100, dt=1e-2, xlim=[-2, 2], ylim=[-2, 2]):
def cov_ellipse(state, cov):
# Covariance matrix correspond to x and y position
Pxy = cov[0:2, 0:2]
eigval, eigvec = np.linalg.eig(Pxy)
x_idx = np.argmax(eigval)
y_idx = np.argmin(eigval)
T = np.arange(0, 2*np.pi+0.1, 0.1)
a = math.sqrt(eigval[x_idx])
b = math.sqrt(eigval[y_idx])
x = [a * math.cos(t) for t in T]
y = [b * math.sin(t) for t in T]
angle = math.atan2(eigvec[y_idx][1], eigvec[y_idx][0])
Rot = np.array([
[math.cos(angle), math.sin(angle)],
[-math.sin(angle), math.cos(angle)]
])
fx = np.matmul(Rot, np.array([x, y]))
px = np.array(fx[0, :] + state[0][0])
py = np.array(fx[1, :] + state[1][0])
return px, py
# Plot
fig, ax = plt.subplots()
ax.set_xlim(xlim)
ax.invert_yaxis()
ax.set_ylim(ylim)
ax.set_facecolor('green')
x = np.array(self.states)[:, 0, :]
y = np.array(self.states)[:, 1, :]
ax.scatter(y[0], x[0], s=90, c='r', marker='X', label='Start Point')
# ax.scatter(y[-1], x[-1], s=90, c='w', marker='*', label='Goal Point')
ax.plot(y, x, '-w', \
label='states')
# Plot
for i in range(0, len(self.states), int(1/dt)):
px, py = cov_ellipse(self.states[i], self.covs[i]/factor)
if i == 0:
ax.fill(py, px, alpha=0.4, facecolor='yellow', edgecolor='yellow', \
linewidth=1, zorder=1, label='covariances')
ax.fill(py, px, alpha=0.4, facecolor='yellow', edgecolor='yellow', \
linewidth=1, zorder=1)
# ax.plot(px, py, '--r', label='covariance at step {}'.format(i))
ax.set_title('State of Thymio')
ax.legend()
fig.tight_layout()
plt.show()
@logger.catch
def kalman_filter(self, dsr, dsl, measurement:State=None):
# Get previous state
pre_state = self.states[-1]
pre_cov = self.covs[-1]
theta = pre_state[-1][0] % (2*np.pi) # [x, y, theta]
D = (dsl + dsr) / 2
T = (dsr - dsl) / self.b
# x_{t+1} = Ax_t + Bu
A = self.__A_mat(theta, D, T)
B = self.__B_mat(theta, D, T)
pre_state = np.array(pre_state).reshape(-1, 1) # reshape to [3, 1]
# next state calculation
# State transition according to EKF state function
# Constraints on theta+T/2 to prevent overflow
if not self.linearize:
theta_post = theta + T/2
Fxu = np.array([D*np.cos(theta_post), D*np.sin(theta_post), T]).reshape(-1, 1)
est_state = pre_state + Fxu
else:
control = np.array([dsr, dsl]).reshape(-1, 1)
est_state = np.matmul(A, pre_state) + np.matmul(B, control)
# Constraints on T
est_state[2][0] = est_state[2][0] % (2*np.pi)
est_cov = np.matmul(A, np.matmul(pre_cov, A.T)) + np.matmul(B, np.matmul(self.R, B.T))
# if measurements exist, apply filter
if measurement:
measurement = np.array([measurement.pos.x, measurement.pos.y, measurement.ori])
# gain
I = np.array(measurement).reshape(-1, 1) - est_state
K = np.matmul(est_cov, np.linalg.inv(est_cov + self.Q))
est_state += np.matmul(K, I)
est_cov -= np.matmul(K, est_cov)
self.states.append(est_state)
self.covs.append(est_cov)
return est_state, est_cov
def get_state(self):
state = self.states[-1]
x, y, theta = state[0][0], state[1][0], state[2][0]
return State(Pos(x, y), theta)
if __name__ == '__main__':
# initial state
pre_state = np.array([1, 1, 0]).reshape(-1, 1)
# initial covariance
pre_cov = np.ones([3, 3]) * 0.03
# displacement in left and right wheels
dsl = [0.3, .2, .1, .11]
dsr = [.3, .2, .1, .11]
measurement = [None, None, None, None]
kf = KF(pre_state, pre_cov, qx=0.3, qy=0.3, qtheta=0.3, rl=0.1, rr=0.1, b=0.08, linearize=False)
for i in range(len(dsl)):
kf.kalman_filter(dsl[i], dsr[i], measurement[i])
# print(kf.kalman_filter(dsl[i], dsr[i], measurement[i]))
kf.plot_gaussian(dt=1)
kfn = KF(pre_state, pre_cov, qx=0.3, qy=0.3, qtheta=0.3, rl=0.1, rr=0.1, b=0.08)
for i in range(len(dsl)):
kfn.kalman_filter(dsl[i], dsr[i])
# print(kfn.kalman_filter(dsl[i], dsr[i]))
# kfn.plot_gaussian(dt=1)