-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEKFSLAM.py
More file actions
521 lines (425 loc) · 18.9 KB
/
EKFSLAM.py
File metadata and controls
521 lines (425 loc) · 18.9 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
from typing import Tuple
import numpy as np
from scipy.linalg import block_diag
import scipy.linalg as la
from utils import rotmat2d
from JCBB import JCBB
import utils
# import line_profiler
# import atexit
# profile = line_profiler.LineProfiler()
# atexit.register(profile.print_stats)
class EKFSLAM:
def __init__(
self,
Q,
R,
do_asso=False,
alphas=np.array([0.001, 0.0001]),
sensor_offset=np.zeros(2),
):
self.Q = Q
self.R = R
self.do_asso = do_asso
self.alphas = alphas
self.sensor_offset = sensor_offset
def f(self, x: np.ndarray, u: np.ndarray) -> np.ndarray:
"""Add the odometry u to the robot state x.
Parameters
----------
x : np.ndarray, shape=(3,)
the robot state
u : np.ndarray, shape=(3,)
the odometry
Returns
-------
np.ndarray, shape = (3,)
the predicted state
"""
head=utils.wrapToPi(x[2])
xpred = np.array([x[0]+u[0]*np.cos(head)+u[1]*np.sin(head),
x[1]+u[0]*np.sin(head)+u[1]*np.cos(head),
head+u[2]])
# TODO, eq (11.7). Should wrap heading angle between (-pi, pi), see utils.wrapToPi
assert xpred.shape == (3,), "EKFSLAM.f: wrong shape for xpred"
return xpred
def Fx(self, x: np.ndarray, u: np.ndarray) -> np.ndarray:
"""Calculate the Jacobian of f with respect to x.
Parameters
----------
x : np.ndarray, shape=(3,)
the robot state
u : np.ndarray, shape=(3,)
the odometry
Returns
-------
np.ndarray
The Jacobian of f wrt. x.
"""
head=utils.wrapToPi(x[2]) #remove?
Fx =np.array( [[1,0,-u[0]*np.sin(head)],
[0,1,u[0]*np.cos(head)-u[1]*np.sin(head)],
[0,0,1]])# TODO, eq (11.13)
assert Fx.shape == (3, 3), "EKFSLAM.Fx: wrong shape"
return Fx
def Fu(self, x: np.ndarray, u: np.ndarray) -> np.ndarray:
"""Calculate the Jacobian of f with respect to u.
Parameters
----------
x : np.ndarray, shape=(3,)
the robot state
u : np.ndarray, shape=(3,)
the odometry
Returns
-------
np.ndarray
The Jacobian of f wrt. u.
"""
head=utils.wrapToPi(x[2]) #remove?
Fu = np.array([[np.cos(head),-np.sin(head),0],
[np.sin(head),np.cos(head),0],
[0,0,1]])
# TODO, eq (11.14)
assert Fu.shape == (3, 3), "EKFSLAM.Fu: wrong shape"
return Fu
def predict(
self, eta: np.ndarray, P: np.ndarray, z_odo: np.ndarray
) -> Tuple[np.ndarray, np.ndarray]:
"""Predict the robot state using the zOdo as odometry the corresponding state&map covariance.
Parameters
----------
eta : np.ndarray, shape=(3 + 2*#landmarks,)
the robot state and map concatenated
P : np.ndarray, shape=(3 + 2*#landmarks,)*2
the covariance of eta
z_odo : np.ndarray, shape=(3,)
the measured odometry
Returns
-------
Tuple[np.ndarray, np.ndarray], shapes= (3 + 2*#landmarks,), (3 + 2*#landmarks,)*2
predicted mean and covariance of eta.
"""
# check inout matrix
assert np.allclose(P, P.T), "EKFSLAM.predict: not symmetric P input"
assert np.all(
np.linalg.eigvals(P) >= 0
), "EKFSLAM.predict: non-positive eigen values in P input"
assert (
eta.shape * 2 == P.shape
), "EKFSLAM.predict: input eta and P shape do not match"
etapred = np.empty_like(eta)
x = eta[:3]
etapred[:3] = self.f(x,z_odo) # TODO robot state prediction
etapred[3:] = eta[3:]# TODO landmarks: no effect
Fx =self.Fx(x,z_odo)# TODO
Fu =self.Fu(x,z_odo)# TODO
# evaluate covariance prediction in place to save computation
# only robot state changes, so only rows and colums of robot state needs changing
# cov matrix layout:
# [[P_xx, P_xm],
# [P_mx, P_mm]]
F=block_diag(Fx,np.eye(len(eta)-3,))
G=np.block([np.eye(3,3),np.zeros((len(eta)-3,3))])
P[:3, :3] = F@P[:3,:3]@F.T+G@Q@G.T# TODO robot cov prediction
#P[:3, 3:] = # TODO robot-map covariance prediction, Should be something??
#P[3:, :3] =P[:3, 3:].T # TODO map-robot covariance: transpose of the above
assert np.allclose(P, P.T), "EKFSLAM.predict: not symmetric P"
assert np.all(
np.linalg.eigvals(P) > 0
), "EKFSLAM.predict: non-positive eigen values"
assert (
etapred.shape * 2 == P.shape
), "EKFSLAM.predict: calculated shapes does not match"
return etapred, P
def h(self, eta: np.ndarray) -> np.ndarray:
"""Predict all the landmark positions in sensor frame.
Parameters
----------
eta : np.ndarray, shape=(3 + 2 * #landmarks,)
The robot state and landmarks stacked.
Returns
-------
np.ndarray, shape=(2 * #landmarks,)
The landmarks in the sensor frame.
"""
L=self.sensor_offset
# extract states and map
x = eta[0:3]
## reshape map (2, #landmarks), m[:, j] is the jth landmark
m = eta[3:].reshape((-1, 2)).T
Rot = rotmat2d(-x[2])
"""
# None as index ads an axis with size 1 at that position.
# Numpy broadcasts size 1 dimensions to any size when needed
delta_m =R@m# TODO, relative position of landmark to sensor on robot in world frame
#aka map in sensor frame ^
zpredcart = # TODO, predicted measurements in cartesian coordinates, beware sensor offset for VP
zpred_r = # TODO, ranges
zpred_theta = # TODO, bearings
zpred = # TODO, the two arrays above stacked on top of each other vertically like
# [ranges;
# bearings]
# into shape (2, #lmrk)
"""
z_c=m-x[:2]-Rot.T@L
z_b=Rot@z_c
zpred=[np.sqrt(z_b[0,:]**2+z_b[1]**2),np.arctan2(z_b[1,:],z_b[0,:])]
zpred = zpred.T.ravel() # stack measurements along one dimension, [range1 bearing1 range2 bearing2 ...]
assert (
zpred.ndim == 1 and zpred.shape[0] == eta.shape[0] - 3
), "SLAM.h: Wrong shape on zpred"
return zpred
def H(self, eta: np.ndarray) -> np.ndarray:
"""Calculate the jacobian of h.
Parameters
----------
eta : np.ndarray, shape=(3 + 2 * #landmarks,)
The robot state and landmarks stacked.
Returns
-------
np.ndarray, shape=(2 * #landmarks, 3 + 2 * #landmarks)
the jacobian of h wrt. eta.
"""
# extract states and map
x = eta[0:3]
## reshape map (2, #landmarks), m[j] is the jth landmark
m = eta[3:].reshape((-1, 2)).T
numM = m.shape[1]
Rot = rotmat2d(x[2])
delta_m = m-x[:2]# TODO, relative position of landmark to robot in world frame. m - rho that appears in (11.15) and (11.16)
zc = delta_m-Rot.T@L# TODO, (2, #measurements), each measured position in cartesian coordinates like
# [x coordinates;
# y coordinates]
zb=Rot.T@delta_m-L #if needed
zpred = self.h(eta)# TODO (2, #measurements), predicted measurements, like
# [ranges;
# bearings]
zr =zpred[0,:] # TODO, ranges
Rpihalf = rotmat2d(np.pi / 2)
# In what follows you can be clever and avoid making this for all the landmarks you _know_
# you will not detect (the maximum range should be available from the data).
# But keep it simple to begin with.
# Allocate H and set submatrices as memory views into H
# You may or may not want to do this like this
H = np.zeros((2 * numM, 3 + 2 * numM)) # TODO, see eq (11.15), (11.16), (11.17)
Hx = H[:, :3] # slice view, setting elements of Hx will set H as well
Hm = H[:, 3:] # slice view, setting elements of Hm will set H as well
# proposed way is to go through landmarks one by one
jac_z_cb = -np.eye(2, 3) # preallocate and update this for some speed gain if looping
for i in range(numM): # But this whole loop can be vectorized
ind = 2 * i # starting postion of the ith landmark into H
inds = slice(ind, ind + 2) # the inds slice for the ith landmark into H
# TODO: Set H or Hx and Hm here
Hx[slice(ind,ind+1),:]=zc[:,i].T/zr[i]@jac_z_cb
Hx[inds,:]=(zc[:,i]@Rpihalf.T)/zr[i]**2@jac_z_cb
Hm[inds+1:inds+2,inds+1:inds+2]=-Hx[inds,:2]
# TODO: You can set some assertions here to make sure that some of the structure in H is correct
return H
def add_landmarks(
self, eta: np.ndarray, P: np.ndarray, z: np.ndarray
) -> Tuple[np.ndarray, np.ndarray]:
"""Calculate new landmarks, their covariances and add them to the state.
Parameters
----------
eta : np.ndarray, shape=(3 + 2*#landmarks,)
the robot state and map concatenated
P : np.ndarray, shape=(3 + 2*#landmarks,)*2
the covariance of eta
z : np.ndarray, shape(2 * #newlandmarks,)
A set of measurements to create landmarks for
Returns
-------
Tuple[np.ndarray, np.ndarray], shapes=(3 + 2*(#landmarks + #newlandmarks,), (3 + 2*(#landmarks + #newlandmarks,)*2
eta with new landmarks appended, and its covariance
"""
n = P.shape[0]
assert z.ndim == 1, "SLAM.add_landmarks: z must be a 1d array"
numLmk = z.shape[0] // 2
lmnew = np.empty_like(z)
Gx = np.empty((numLmk * 2, 3))
Rall = np.zeros((numLmk * 2, numLmk * 2))
I2 = np.eye(2) # Preallocate, used for Gx
sensor_offset_world = rotmat2d(eta[2]) @ self.sensor_offset # For transforming landmark position into world frame
sensor_offset_world_der = rotmat2d(eta[2] + np.pi / 2) @ self.sensor_offset # Used in Gx
for j in range(numLmk):
ind = 2 * j
inds = slice(ind, ind + 2)
zj = z[inds]
rot =rotmat2d(eta[2]+zj[1]) # TODO, rotmat in Gz
lmnew[inds] =zj[0]*rot[:,0]+eta[:2]+rot@self.sensor_offset # TODO, calculate position of new landmark in world frame
Gx[inds, :2] = np.eye(2)# TODO
Gx[inds, 2] = zj[0]*np.array([-np.sin(zj[1]+eta[2]),np.cos(zj[1]+eta[2])]).T# TODO
Gz =rot@np.diag([1,zj[1]]) # TODO
Rall[inds, inds] = Gz@self.R@Gz.T# TODO, Gz * R * Gz^T, transform measurement covariance from polar to cartesian coordinates
assert len(lmnew) % 2 == 0, "SLAM.add_landmark: lmnew not even length"
etaadded = np.append(eta,lmnew)# TODO, append new landmarks to state vector
Padded = block_diag(P,Gx@P[:3,:3]@Gx.T+Rall)# TODO, block diagonal of P_new, see problem text in 1g) in graded assignment 3
Padded[n:,:n] = Gx@P[:3,:]# TODO, top right corner of P_new
Padded[:n, n:] = Padded[n:,:n].T# TODO, transpose of above. Should yield the same as calcualion, but this enforces symmetry and should be cheaper
assert (
etaadded.shape * 2 == Padded.shape
), "EKFSLAM.add_landmarks: calculated eta and P has wrong shape"
assert np.allclose(
Padded, Padded.T
), "EKFSLAM.add_landmarks: Padded not symmetric"
assert np.all(
np.linalg.eigvals(Padded) >= 0
), "EKFSLAM.add_landmarks: Padded not PSD"
return etaadded, Padded
def associate(
self, z: np.ndarray, zpred: np.ndarray, H: np.ndarray, S: np.ndarray,
): # -> Tuple[*((np.ndarray,) * 5)]:
"""Associate landmarks and measurements, and extract correct matrices for these.
Parameters
----------
z : np.ndarray,
The measurements all in one vector
zpred : np.ndarray
Predicted measurements in one vector
H : np.ndarray
The measurement Jacobian matrix related to zpred
S : np.ndarray
The innovation covariance related to zpred
Returns
-------
Tuple[*((np.ndarray,) * 5)]
The extracted measurements, the corresponding zpred, H, S and the associations.
Note
----
See the associations are calculated using JCBB. See this function for documentation
of the returned association and the association procedure.
"""
if self.do_asso:
# Associate
a = JCBB(z, zpred, S, self.alphas[0], self.alphas[1])
# Extract associated measurements
zinds = np.empty_like(z, dtype=bool)
zinds[::2] = a > -1 # -1 means no association
zinds[1::2] = zinds[::2]
zass = z[zinds]
# extract and rearange predicted measurements and cov
zbarinds = np.empty_like(zass, dtype=int)
zbarinds[::2] = 2 * a[a > -1]
zbarinds[1::2] = 2 * a[a > -1] + 1
zpredass = zpred[zbarinds]
Sass = S[zbarinds][:, zbarinds]
Hass = H[zbarinds]
assert zpredass.shape == zass.shape
assert Sass.shape == zpredass.shape * 2
assert Hass.shape[0] == zpredass.shape[0]
return zass, zpredass, Hass, Sass, a
else:
# should one do something her
pass
def update(
self, eta: np.ndarray, P: np.ndarray, z: np.ndarray
) -> Tuple[np.ndarray, np.ndarray, float, np.ndarray]:
"""Update eta and P with z, associating landmarks and adding new ones.
Parameters
----------
eta : np.ndarray
[description]
P : np.ndarray
[description]
z : np.ndarray, shape=(#detections, 2)
[description]
Returns
-------
Tuple[np.ndarray, np.ndarray, float, np.ndarray]
[description]
"""
numLmk = (eta.size - 3) // 2
assert (len(eta) - 3) % 2 == 0, "EKFSLAM.update: landmark lenght not even"
if numLmk > 0:
# Prediction and innovation covariance
zpred = self.h(eta)
H = self.H(eta)# TODO
# Here you can use simply np.kron (a bit slow) to form the big (very big in VP after a while) R,
# or be smart with indexing and broadcasting (3d indexing into 2d mat) realizing you are adding the same R on all diagonals
S = H@P@H.T+self.R# TODO,
assert (
S.shape == zpred.shape * 2
), "EKFSLAM.update: wrong shape on either S or zpred"
z = z.ravel() # 2D -> flat
# Perform data association
za, zpred, Ha, Sa, a = self.associate(z, zpred, H, S)
# No association could be made, so skip update
if za.shape[0] == 0:
etaupd = eta#+W@v
Pupd = P # TODO
NIS = 1 # TODO: beware this one when analysing consistency.
else:
# Create the associated innovation
v = za.ravel() - zpred # za: 2D -> flat
v[1::2] = utils.wrapToPi(v[1::2])
# Kalman mean update
# S_cho_factors = la.cho_factor(Sa) # Optional, used in places for S^-1, see scipy.linalg.cho_factor and scipy.linalg.cho_solve
W = P@H.T@np.linalg.inv(S)# TODO, Kalman gain, can use S_cho_factors
etaupd =eta+W@v # TODO, Kalman update
# Kalman cov update: use Joseph form for stability
jo = -W @ Ha
jo[np.diag_indices(jo.shape[0])] += 1 # same as adding Identity mat
Pupd = jo@P# TODO, Kalman update. This is the main workload on VP after speedups
# calculate NIS, can use S_cho_factors
NIS = v.T@np.linalg.inv(S)@v # TODO
# When tested, remove for speed
assert np.allclose(Pupd, Pupd.T), "EKFSLAM.update: Pupd not symmetric"
assert np.all(
np.linalg.eigvals(Pupd) > 0
), "EKFSLAM.update: Pupd not positive definite"
else: # All measurements are new landmarks,
a = np.full(z.shape[0], -1)
z = z.flatten()
NIS = 0 # TODO: beware this one, you can change the value to for instance 1
etaupd = eta
Pupd = P
# Create new landmarks if any is available
if self.do_asso:
is_new_lmk = a == -1
if np.any(is_new_lmk):
z_new_inds = np.empty_like(z, dtype=bool)
z_new_inds[::2] = is_new_lmk
z_new_inds[1::2] = is_new_lmk
z_new = z[z_new_inds]
etaupd, Pupd = self.add_landmarks(eta,P,z_new)# TODO, add new landmarks.
assert np.allclose(Pupd, Pupd.T), "EKFSLAM.update: Pupd must be symmetric"
assert np.all(np.linalg.eigvals(Pupd) >= 0), "EKFSLAM.update: Pupd must be PSD"
return etaupd, Pupd, NIS, a
@classmethod
def NEESes(cls, x: np.ndarray, P: np.ndarray, x_gt: np.ndarray,) -> np.ndarray:
"""Calculates the total NEES and the NEES for the substates
Args:
x (np.ndarray): The estimate
P (np.ndarray): The state covariance
x_gt (np.ndarray): The ground truth
Raises:
AssertionError: If any input is of the wrong shape, and if debug mode is on, certain numeric properties
Returns:
np.ndarray: NEES for [all, position, heading], shape (3,)
"""
assert x.shape == (3,), f"EKFSLAM.NEES: x shape incorrect {x.shape}"
assert P.shape == (3, 3), f"EKFSLAM.NEES: P shape incorrect {P.shape}"
assert x_gt.shape == (3,), f"EKFSLAM.NEES: x_gt shape incorrect {x_gt.shape}"
d_x = x - x_gt
d_x[2] = utils.wrapToPi(d_x[2])
assert (
-np.pi <= d_x[2] <= np.pi
), "EKFSLAM.NEES: error heading must be between (-pi, pi)"
d_p = d_x[0:2]
P_p = P[0:2, 0:2]
assert d_p.shape == (2,), "EKFSLAM.NEES: d_p must be 2 long"
d_heading = d_x[2] # Note: scalar
assert np.ndim(d_heading) == 0, "EKFSLAM.NEES: d_heading must be scalar"
P_heading = P[2, 2] # Note: scalar
assert np.ndim(P_heading) == 0, "EKFSLAM.NEES: P_heading must be scalar"
# NB: Needs to handle both vectors and scalars! Additionally, must handle division by zero
NEES_all = d_x @ (np.linalg.solve(P, d_x))
NEES_pos = d_p @ (np.linalg.solve(P_p, d_p))
try:
NEES_heading = d_heading ** 2 / P_heading
except ZeroDivisionError:
NEES_heading = 1.0 # TODO: beware
NEESes = np.array([NEES_all, NEES_pos, NEES_heading])
NEESes[np.isnan(NEESes)] = 1.0 # We may divide by zero, # TODO: beware
assert np.all(NEESes >= 0), "ESKF.NEES: one or more negative NEESes"
return NEESes