-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqekf.py
278 lines (206 loc) · 8.79 KB
/
qekf.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import local_packages
import pyquat as pq
import pyquat.wahba as wahba
import pyquat.wahba.qmethod as qmethod
import numpy as np
import scipy.linalg as spl
class Qekf(object):
tau = 5400.0 # time constant on the attitude bias, which
# is an exponentially-correlated random
# variable (ECRV)
q_w_psd = 5.4154e-10 # power spectral density on the noise coming
# out of the gyroscope
q_w_psd_tuning_factor = 1.0
def __init__(self, dt,
q_inrtl_to_body_init = pq.identity()):
"""Constructor for the q-method extended Kalman filter.
Args:
dt propagation time step (s)
q_inrtl_to_body_init initial attitude guess (of type
pyquat.Quat; defaults to pq.identity())
"""
self.dt = dt
self.time = 0.0
# Setup state estimate
self.x = np.zeros(6)
self.q = q_inrtl_to_body_init # attitude estimate
# Setup and initialize covariance
self.P = np.zeros((6,6))
for ii in range(0,3):
self.P[ii,ii] = (0.5 * np.pi)**2
for ii in range(3,6):
self.P[ii,ii] = 1e-8
# As we propagate, log data
self.log = { 't': [0.0],
'wm': [np.zeros(3)],
'qIB': [self.q],
'bg': [np.zeros(3)],
'Pdiag': [np.zeros(6)] }
@property
def Pqq(self):
return self.P[0:3,0:3]
@property
def Pqb(self):
return self.P[0:3,3:]
@property
def Pbq(self):
return self.P[3:,0:3]
@property
def Pbb(self):
return self.P[3:,3:]
@property
def T(self):
return self.q.to_matrix()
def update_attitude(self, *args,
sigma_y = [1e-3, 1e-3],
sigma_n = [1e-11, 1e-3],
w = None):
"""Accept observations and reference vectors and use them to update
the filter.
This method currently accepts four unnamed arguments, all of
which are length-3 vectors. The first two are the observed
vectors (e.g. to the sun and in the direction of the magnetic
field). The second two are the reference vectors (where the
model suggests these should be pointed).
The method, when complete, has updated the filter's estimate
of the attitude, the state estimate, and the covariance.
Args:
obs1 unit vector observation 1
obs2 unit vector observation 2
ref1 unit reference vector corresponding to obs1
ref2 unit reference vector corresponding to obs2
Kwargs:
sigma_y standard deviations for obs1 and obs2 in a list,
tuple, or numpy array
sigma_n standard deviations of ref1 and ref2 in a list,
tuple, or numpy array
w weights list, tuple, or numpy array for each
vector, which may be provided in lieu of sigma_y
and sigma_n (default: None)
Returns:
A tuple of the state update and the delta quaternion
reflecting the change in attitude.
"""
# Get prior information for qmethod
Nqq_prior = spl.inv(self.Pqq)
if w is None:
w = qmethod.compute_weights(sigma_y, sigma_n)
y = np.vstack(args[:2]).T
n = np.vstack(args[2:]).T
q_post = qmethod.qmethod(y, n, w, q_prior = self.q, N_prior = Nqq_prior)
T = self.T
# Compute the covariance of the vectors which are orthogonal to
# the observations and references.
Rzz = qmethod.qekf_measurement_covariance(T, y, n, w, sigma_y, sigma_n)
# Compute measurement model for attitude measurement
Htheta = qmethod.qekf_measurement_model(T, y, n, w)
Xi_prior = self.q.big_xi()
# 4. Calculate attitude covariance posterior using Joseph form: eq 68
Ktheta = spl.inv(-Nqq_prior*2 + Htheta)
I_minus_KH = np.identity(3) - Ktheta.dot(Htheta)
Pqq_post = I_minus_KH.dot(self.Pqq).dot(I_minus_KH.T) + Ktheta.dot(Rzz).dot(Ktheta.T)
# 5. Update nonattitude states
db = self.Pbq.dot(Nqq_prior).dot(Xi_prior.T.dot(q_post.to_vector().reshape((4)))) * 2
# 6. Calculate total covariance update: eqs 70, 71
Pbq_post = self.Pbq.dot(Nqq_prior).dot(Pqq_post)
Pbb_post = self.Pbb + self.Pbq.dot( Nqq_prior.dot(Pqq_post).dot(Nqq_prior) - Nqq_prior ).dot(self.Pqb)
Pqb_post = Pbq_post.T
# Perform the update in preparation for the next
self.P = np.vstack(( np.hstack((Pqq_post, Pqb_post)),
np.hstack((Pbq_post, Pbb_post)) ))
dq_body = q_post * self.q.conjugated()
self.q = q_post
dx = np.hstack((np.zeros(3), db))
self.x += dx
print("q = {}".format(self.q))
print("Pqq = {}".format(self.P[0:3,0:3]))
return dx, dq_body
def dynamics_matrix(self, omega):
"""Linearize the dynamics at the current time point, using the current
IMU measurements and the state.
Args:
omega angular velocity measurement (see propagate())
Returns:
A square matrix of the same size as the state vector.
"""
F = np.zeros((6,6))
F[0:3,0:3] = -pq.skew(omega)
F[0:3,3:6] = -np.identity(3)
F[3:6,3:6] = -np.identity(3)/self.tau
return F
def state_transition_matrix(self, omega):
"""Compute \Phi(t_{k}, t_{k-1}) using IMU measurements and aspects of
the filter state.
This uses the first-order approximation
\Phi \approx I + F|_{t_k} \Delta t
where t_k is the current time, and F is the Jacobian of the
state dynamics (from dynamics_matrix()).
Args:
omega angular velocity measurement (as passed to propagate())
Returns:
An NxN matrix, where N is the size of the state vector.
"""
Phi = np.identity(6) + self.dynamics_matrix(omega) * self.dt
Phi[3:6,3:6] = np.identity(3) * np.exp(-self.dt / self.tau)
return Phi
def process_noise(self):
"""Compute the Q matrix.
Returns:
A diagonal matrix the same size as the covariance P.
"""
q_w = self.q_w_psd * self.q_w_psd_tuning_factor * self.dt
return np.diag([0.0, 0.0, 0.0, q_w, q_w, q_w])
def propagate(self, omega):
"""Advance the state forward in time using measurements from the
gyroscope.
Args:
omega measured angular velocity (body with respect to the
inertial frame, expressed in the body frame)
"""
q_next = pq.propagate(self.q, omega, self.dt)
Phi = self.state_transition_matrix(omega)
P = self.P
Q = self.process_noise()
self.P = Phi.dot(P.dot(Phi.T)) + Q
self.q = q_next
# There are no dynamics on the state currently.
self.time += self.dt
self.log['t'].append(self.time)
self.log['qIB'].append(q_next)
self.log['wm'].append(omega)
self.log['bg'].append(np.array(self.x[3:6])) # copy! don't ref
self.log['Pdiag'].append(np.diag(self.P))
def finish(self):
"""Close out logs.
"""
self.log['t'] = np.hstack(self.log['t'])
self.log['wm'] = np.vstack(self.log['wm']).T
self.log['bg'] = np.vstack(self.log['bg']).T
self.log['sigma'] = np.sqrt(np.vstack(self.log['Pdiag']).T)
del self.log['Pdiag']
if __name__ == '__main__':
import pyquat.random as pqr
import numpy.random as npr
q_inrtl_to_body = pq.Quat(1.0, -2.0, 3.0, 4.0).normalized()
print("q_ib = {}".format(q_inrtl_to_body))
ref_misalign = npr.randn(3) * 1e-6
sun_obs_misalign = npr.randn(3) * 1e-5
mag_obs_misalign = npr.randn(3) * 1e-5
T_ref_err = np.identity(3) #- pq.skew(ref_misalign)
T_sun_obs_err = np.identity(3) - pq.skew(sun_obs_misalign)
T_mag_obs_err = np.identity(3) - pq.skew(mag_obs_misalign)
mag_truth = np.array([0.0, 0.1, 1.0])
mag_truth /= spl.norm(mag_truth)
sun_truth = np.array([0.5, 0.5, 0.02])
sun_truth /= spl.norm(sun_truth)
Tib = q_inrtl_to_body.to_matrix()
mag_ref = T_ref_err.dot(mag_truth)
mag_ref /= spl.norm(mag_ref)
sun_ref = T_ref_err.dot(sun_truth)
sun_ref /= spl.norm(sun_ref)
mag_obs = T_mag_obs_err.dot(Tib.dot(mag_ref))
mag_obs /= spl.norm(mag_obs)
sun_obs = T_sun_obs_err.dot(Tib.dot(sun_ref))
sun_obs /= spl.norm(sun_obs)
kf = Qekf(0.1)
kf.update_attitude(sun_obs, mag_obs, sun_ref, mag_ref)