forked from zhangzhishan/particle-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathekf.m
159 lines (125 loc) · 3.75 KB
/
ekf.m
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function [xHatUpdated r S Ppost]=EKF(t,y,u,xinit,dt)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%% EXTENDED KF ROUTINE %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Tuning Parameters %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Cbn=20000; %current bias noise (Force)
Wdn=.8; %wave disturbance noise (Position)
Pn=.01; %Position noise
Vn=.0001; %Velocity noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Initialize memory %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent P xHat
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Initialize filter %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty (P)
%%% Wave Parameters%%%
P=zeros(15);
xHat=xinit;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Initialize system %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
M= 10^9*[0.007 0 0;
0 0.0110 -0.0130;
0 -0.0130 3.1930];
Minv=inv(M);
D=[200000 0 0;
0 100000 -700000;
0 -700000 63900000];
T1=0.0001;T2=0.0001;T3=0.0001;
T=diag([T1 T2 T3]);
w01=0.9; w02=0.9; w03=0.9;
z1=0.1; z2=0.1; z3=0.1;
sig1=50; sig2=50; sig3=50;
Omega=diag([w01 w02 w03]);
Z=diag([z1 z2 z3]);
Sigma=diag([sig1 sig2 sig3]);
Sigma2=[zeros(3,3);
Sigma];
Omega2=[zeros(3,3) eye(3);
-Omega^2 -2*Z*Omega];
Psi=eye(3);
psi0=xHat(3);
u0=xHat(4);
v0=xHat(5);
bx0=xHat(7);
by0=xHat(8);
a=Minv(1,1);
b=Minv(2,2);
c=Minv(3,3);
d=Minv(2,3);
A11=[0 0 -sin(psi0)*u0-cos(psi0)*v0;
0 0 cos(psi0)*u0-sin(psi0)*v0;
0 0 0];
A12=[cos(psi0) -sin(psi0) 0;
sin(psi0) cos(psi0) 0;
0 0 1];
A21=[0 0 -a*sin(psi0)*bx0+a*cos(psi0)*by0;
0 0 -b*cos(psi0)*bx0-b*sin(psi0)*by0;
0 0 -d*cos(psi0)*bx0-d*sin(psi0)*by0];
A23=[ a*cos(psi0) a*sin(psi0) 0;
-b*sin(psi0) b*cos(psi0) d;
-d*sin(psi0) d*cos(psi0) c];
A=[A11 A12 zeros(3,9);
A21 -Minv*D A23 zeros(3,6);
zeros(3,6) -T zeros(3,6);
zeros(6,9) Omega2]*dt+eye(15);
E=[zeros(6,6);
Psi zeros(3,3);
zeros(6,3) Sigma2];
C=[eye(3) zeros(3,9) eye(3);
zeros(3,3) eye(3) zeros(3,9)];
R=[0.1^2 0 0 0 0 0;
0 0.1^2 0 0 0 0;
0 0 (.01/180*pi)^2 0 0 0;
0 0 0 0.1^2 0 0;
0 0 0 0 0.1^2 0;
0 0 0 0 0 (.01/180*pi)^2];
Qstd2=diag([Cbn,Cbn,Cbn, Wdn,Wdn, Wdn/180*pi]);
Q2=Qstd2.^2;
Qstd1=diag([Pn,Pn,0,Vn,Vn,Vn/180*pi,0,0,0,0,0,0,0,0,0]);
Q1=Qstd1.^2;
Q=(E*Q2*E'+Q1)*dt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Propagate estimate %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xdot=SimVesselNLmodel(t,xHat,u,dt,0,0);
xPrio=xdot(1:15)*dt + xHat(1:15);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Propagate Apriori covar. %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Pprio=A*P*A'+Q;
S = C*Pprio*C'+R;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% Propagate meas. %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isnan(y)
xHat(1:15)=xPrio;
P=Pprio;
r=NaN*ones(size(y));
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Kalman Gain %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K=Pprio*C'/(S);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% State Update %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r = y-C*xPrio(1:15);
xHat=xPrio+K*(r);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Propagate Posteriori covar. %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P=(eye(15)-K*C)*Pprio;
P=(P+P')/2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% output %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ppost = P;
xHatUpdated=xHat;