-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpva_kalman.py
69 lines (53 loc) · 1.48 KB
/
pva_kalman.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
from filterpy.kalman import KalmanFilter
from filterpy.stats import gaussian
from filterpy.common import Q_discrete_white_noise
import numpy as np
from numpy.random import randn
import matplotlib.pyplot as plt
import easygui
import json
# PATH = "/Users/Daniel-LT/Documents/WPI/MQP/Logs/circle_2_2019-11-10-20_19_37.txt"
PATH = easygui.fileopenbox()
mag_compass = []
kalman_live = []
pys = []
def load_log():
with open(PATH, "r") as log_file:
for line in log_file.readlines():
obj = json.loads(line)
mag_compass.append(float(obj['mag_compass']))
kalman_live.append(float(obj['kalman']))
def pva_kalman_filter():
kf = KalmanFilter(dim_x=3, dim_z=1)
# Time Step
dt = 0.1
# Control Inputs
kf.u = 0
# Measurement Function (we are only recording position)
kf.H = np.array([[1,0,0]])
# State Variable
kf.x = np.array([[0,0,0]]).T
# Measurement Noise
kf.R = 20
# Process/Motion Noise
kf.Q = np.eye(3) * .1
# Covariance Matrix
kf.P = np.eye(3)*1
# State Transition Function
kf.F = np.array([[1., dt, .5*dt*dt],
[0., 1., dt],
[0., 0., 1.]])
## --------------- PREDICT / UPDATE -----------##
for z in mag_compass:
kf.predict()
kf.update(z)
pys.append(kf.x[0])
## ---------------- PLOT -------------------------##
load_log()
pva_kalman_filter()
plt.plot(mag_compass, label = 'live compass')
plt.plot(pys, label = 'PVA Kalman', color='g')
plt.plot(kalman_live, label = 'P Kalman', color = 'k')
plt.legend(loc = 'lower right')
plt.grid()
plt.show()