-
Notifications
You must be signed in to change notification settings - Fork 0
/
disturbance_observer.py
95 lines (60 loc) · 1.67 KB
/
disturbance_observer.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
import numpy as np
import model
import matplotlib.pyplot as plt
if __name__ == '__main__':
#model
model = model.SingleMassModel()
#param
dt = 1e-2
m = 1.0
A = np.array([[1, dt, 0],
[0, 1, 1/m * dt],
[0, 0, 1]])
B = np.array([0, 1/m * dt, 0])
C = np.array([[1, 0, 0],
[0, 1, 0]])
Q = np.diag((0, 0, 100))
R = np.diag((0.01, 0.01))
# variables
x = np.zeros(3) #x, x_dot, f_ext
P = np.zeros((3, 3))
#input
u = 0.2
#log
x_est_list = []
x_dot_est_list = []
x_gt_list = []
x_dot_gt_list = []
f_ext_list = []
for i in range(int(1e3)):
if i == 500:
model.f_ext = 1.0
#update model (simulation)
model.updateModel(u, dt)
z = model.getObservation()
#kalman filter
#prediction
x_ = A.dot(x) + B * u
P_ = A.dot(P).dot(A.transpose()) + Q
#update
G = P_.dot(C.transpose()).dot(C.dot(P_).dot(C.transpose()) + R)
x = x_ + G.dot(z - C.dot(x_))
P = (np.eye(3) - G.dot(C)).dot(P_)
gt = model.getGroundTruth()
x_est_list.append(x[0])
x_dot_est_list.append(x[1])
x_gt_list.append(gt[0])
x_dot_gt_list.append(gt[1])
f_ext_list.append(x[2])
x = np.linspace(0, 10, 1000)
plt.subplot(311)
plt.plot(x, x_est_list, label="x_est")
plt.plot(x, x_gt_list, label="x_gt")
plt.legend()
plt.subplot(312)
plt.plot(x, x_dot_est_list, label="x_dot_est")
plt.plot(x, x_dot_gt_list, label="x_dot_gt")
plt.legend()
plt.subplot(313)
plt.plot(x, f_ext_list)
plt.show()