-
Notifications
You must be signed in to change notification settings - Fork 0
/
es7.py
93 lines (74 loc) · 2.85 KB
/
es7.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
import numpy as np
from plot import plot_trajectory, plot_point, plot_covariance_2d
class UserCode:
def __init__(self):
dt = 0.005
#State-transition model
self.A = np.array([
[1,0,dt,0],
[0,1,0,dt],
[0,0,1,0],
[0,0,0,1]
])
#Observation model
self.H = np.array([[1,0,0,0],[0,1,0,0]])
#TODO: Play with the noise matrices
#Process/State noise
vel_noise_std = 0.005
pos_noise_std = 0.005
self.Q = np.array([
[pos_noise_std*pos_noise_std,0,0,0],
[0,pos_noise_std*pos_noise_std,0,0],
[0,0,vel_noise_std*vel_noise_std,0],
[0,0,0,vel_noise_std*vel_noise_std]
])
#Sensor/Measurement noise
measurement_noise_std = 0.5
self.R = measurement_noise_std * measurement_noise_std * np.identity(2)
self.x = np.zeros((4,1)) #Initial state vector [x,y,vx,vy]
self.sigma = np.identity(4) #Initial covariance matrix
def predictState(self, A, x):
'''
:param A: State-transition model matrix
:param x: Current state vector
:return x_p: Predicted state vector as 4x1 numpy array
'''
x_p = np.dot(A, x)
return x_p
def predictCovariance(self, A, sigma, Q):
sigma_p = np.dot(np.dot(A, sigma), np.transpose(A))+Q
return sigma_p
def calculateKalmanGain(self, sigma_p, H, R):
k = np.dot(np.dot(sigma_p, np.transpose(H)), np.linalg.inv(np.dot(H, np.dot(sigma_p, np.transpose(H)))+R))
return k
def correctState(self, z, x_p, k, H):
'''
:param z: Measurement vector
:param x_p: Predicted state vector
:param k: Kalman gain
:param H: Observation model
:return x: Corrected state vector as 4x1 numpy array
'''
x = x_p + np.dot(k, z - np.dot(H, x_p))
return x
def correctCovariance(self, sigma_p, k, H):
sigma = np.dot((np.identity(4)-np.dot(k, H)), sigma_p)
return sigma
def state_callback(self):
self.x = self.predictState(self.A, self.x)
self.sigma = self.predictCovariance(self.A, self.sigma, self.Q)
# visualize position state
plot_trajectory("kalman", self.x[0:2])
plot_covariance_2d("kalman", self.sigma[0:2,0:2])
def measurement_callback(self, measurement):
'''
:param measurement: vector of measured coordinates
'''
# visualize measurement
plot_point("gps", measurement)
k = self.calculateKalmanGain(self.sigma, self.H, self.R)
self.x = self.correctState(measurement, self.x, k, self.H)
self.sigma = self.correctCovariance(self.sigma, k, self.H)
# visualize position state
plot_trajectory("kalman", self.x[0:2])
plot_covariance_2d("kalman", self.sigma[0:2,0:2])