-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkalman_filter.py
53 lines (42 loc) · 1.63 KB
/
kalman_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
from filterpy.kalman import KalmanFilter
import numpy as np
class IP_KF():
def __init__(self, init_x, init_y):
# System and Measurement model
# x = [x_postition, x_velocity, y_position, y_velocity] : system states
# x = [x_postition, y_position] : Measurement states
self.dt = 0.001
self.F = np.array([ # system matrix
[1, self.dt, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, self.dt],
[0, 0, 0, 1]], dtype=np.float)
self.H = np.array([ # measurement matrix
[1, 0, 0, 0],
[0, 0, 1, 0]])
# self.H = np.array([ # measurement matrix
# [1, 0],
# [0, 1]])
self.Q = 0.9*np.eye(4, dtype=np.float) # system error matrix
self.R = np.array([ # measurement error matrix
[100, 0],
[0, 100]], dtype=np.float)
# Kalman filter using filterpy
self.kf = KalmanFilter (dim_x=4, dim_z=2)
self.kf.x = np.array([[init_x], [0], [init_y], [0]])
self.kf.F = self.F
self.kf.H = self.H
self.kf.Q = self.Q
self.kf.R = self.R
self.kf.P *= 1000.
def update(self, x, y):
# self.dt = 0.001
# self.F = np.array([ # system matrix
# [1, self.dt, 0, 0],
# [0, 1, 0, 0],
# [0, 0, 1, self.dt],
# [0, 0, 0, 1]], dtype=np.float)
z = np.array([[x], [y]])
self.kf.predict()
self.kf.update(z)
return self.kf.x