-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEKF.asv
114 lines (88 loc) · 1.69 KB
/
EKF.asv
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
function [q0, q1, q2, q3] = EKF(p, q, r, B, mx, my, mz, ax, ay, az, dt, N_Q, N_R, N_P)
%
%
global Q R
global x P
global firstRun
%Initialize
if isempty(firstRun)
Q = N_Q*eye(4);
R = N_R*eye(6);
x = [1 0 0 0]';
P = N_P*eye(4);
firstRun = 1;
end
%1.Predict xp and Pp
%Calculate F
F = Fjacob(p, q, r, dt);
xp = F*x;
Pp = F*P*F' + Q;
%2.Correct xp and P
%Calculate H
H = Hjacob(x(1),x(2),x(3),x(4), B);
S = (H*Pp*H' + R);
K = Pp*H'*(S\eye(6));
z = [ax;
ay;
az;
mx;
my;
mz];
x = xp + K*(z - H*xp);
P = Pp - K*H*Pp;
x_sc=(x(1)^2+x(2)^2+x(3)^2+x(4)^2)^0.5;
q0 = x(1) / x_sc;
q1 = x(2) / x_sc;
q2 = x(3) / x_sc;
q3 = x(4) / x_sc;
%------------------------------
function F = Fjacob(p, q, r, dt)
%
%
F = zeros(4);
F(1,1)=1;
F(1,2)= -p*dt/2;
F(1,3)= -q*dt/2;
F(1,4)= -r*dt/2;
F(2,1)= p*dt/2;
F(2,2)=1;
F(2,3)= r*dt/2;
F(2,4)=-q*dt/2;
F(3,1)=q*dt/2;
F(3,2)=-r*dt/2;
F(3,3)=1;
F(3,4)=p*dt/2;
F(4,1)=r*dt/2;
F(4,2)=q*dt/2;
F(4,3)=-p*dt/2;
F(4,4)=1;
%------------------------------
function H = Hjacob(qt0, qt1, qt2, qt3, B)
%
%
g=9.8;
H = zeros(6,4);
H(1,1) = -qt2;
H(1,2) = qt3;
H(1,3) = -qt0;
H(1,4) = qt1;
H(2,1) = qt1;
H(2,2) = qt0;
H(2,3) = qt3;
H(2,4) = qt2;
H(3,1) = qt0;
H(3,2) = -qt1;
H(3,3) = -qt2;
H(3,4) = qt3;
H(4,1) = qt0*B(1)+qt3*B(2)-qt2*B(3);
H(4,2) = qt1*B(1)+qt2*B(2)+qt3*B(3);
H(4,3) = -qt2*B(1)+qt1*B(2)-qt0*B(3);
H(4,4) = -qt3*B(1)+qt0*B(2)+qt1*B(3);
H(5,1) = -qt3*B(1)+qt0*B(2)+qt1*B(3);
H(5,2) = qt2*B(1)-qt1*B(2)+qt0*B(3);
H(5,3) = qt1*B(1)+qt2*B(2)+qt3*B(3);
H(5,4) = -qt0*B(1)-qt3*B(2)+qt2*B(3);
H(6,1) = qt2*B(1)-qt1*B(2)+qt0*B(3);
H(6,2) = qt3*B(1)-qt0*B(2)-qt1*B(3);
H(6,3) = qt0*B(1)+qt3*B(2)-qt2*B(3);
H(6,4) = qt1*B(1)+qt2*B(2)+qt3*B(3);