-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
129 lines (106 loc) · 3.78 KB
/
filter.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
# ---------------------------------------------------------------------
# Project "Track 3D-Objects Over Time"
# Copyright (C) 2020, Dr. Antje Muntzinger / Dr. Andreas Haja.
#
# Purpose of this file : Kalman filter class
#
# You should have received a copy of the Udacity license together with this program.
#
# https://www.udacity.com/course/self-driving-car-engineer-nanodegree--nd013
# ----------------------------------------------------------------------
#
# imports
import numpy as np
# add project directory to python path to enable relative imports
import os
import sys
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
import misc.params as params
class Filter:
'''Kalman filter class'''
def __init__(self):
self.dim_state = params.dim_state# process model dimension
self.dt = params.dt # time increment
self.q = params.q # process noise variable for Kalman filter Q
def F(self):
############
# TODO Step 1: implement and return system matrix F
############
N = self.dim_state
F = np.identity(self.dim_state).reshape(N,N)
F[0, 3] = self.dt
F[1, 4] = self.dt
F[2, 5] = self.dt
return np.matrix(F)
############
# END student code
############
def Q(self):
############
# TODO Step 1: implement and return process noise covariance Q
############
q = params.q
dt = params.dt
q3 = (q/3)*dt**3
q2 = (q/2)*dt**2
q1 = q*dt
return np.matrix([[q3,0,0,q2,0,0],
[0,q3,0,0,q2,0],
[0,0,q3,0,0,q2],
[q2,0,0,q1,0,0],
[0,q2,0,0,q1,0],
[0,0,q2,0,0,q1]])
############
# END student code
############
def predict(self, track):
############
# TODO Step 1: predict state x and estimation error covariance P to next timestep, save x and P in track
############
F = self.F()
Q = self.Q()
x = F * track.x # state prediction
P = F * track.P * F.transpose() + Q # covariance prediction
track.set_x(x)
track.set_P(P)
############
# END student code
############
def update(self, track, meas):
############
# TODO Step 1: update state x and covariance P with associated measurement, save x and P in track
############
H = meas.sensor.get_H(track.x) # measurement matrix
gamma = self.gamma(track, meas) # residual
S = self.S(track, meas, H) # covariance of residual
K = track.P * H.transpose()* S.I # Kalman gain
x = track.x + K * gamma # state update
I = np.identity(self.dim_state)
P = (I - K * H) * track.P # covariance update
track.set_x(x)
track.set_P(P)
############
# END student code
############
track.update_attributes(meas)
def gamma(self, track, meas):
############
# TODO Step 1: calculate and return residual gamma
############
############
g = meas.z - meas.sensor.get_hx(track.x)# residual
return g
############
# END student code
############
def S(self, track, meas, H):
############
# TODO Step 1: calculate and return covariance of residual S
############
s = H * track.P * H.transpose() + meas.R # covariance of residual
return s
############
# END student code
############