-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2nodelay.m
339 lines (273 loc) · 6.82 KB
/
part2nodelay.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
%%
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(groot, 'defaultTextInterpreter', 'latex')
set(groot, 'defaultTextFontSize', 16), set(groot, 'defaultColorbarFontSize', 16)
clc, clear;
%%
load project_data
load secndnodelay.mat
[yaw, pitch, roll] = quat2angle(q_body2ned);
dcm = quat2dcm(q_body2ned);
acel_NED = zeros(13901,3);
for i = 1:13901
acel_NED(i,:) = dcm(:,:,i)'*acceleration_body(i,:)';
end
%%
sys1c = idss(secndordernodelay);
sys1d = c2d(sys1c,0.01,'zoh');
%% compare discretized system
sys1d.InputName = 'y_0';
sys1d.OutputName = {'y', 'a_y'};
sys1c.InputName = 'y_0';
sys1c.OutputName = {'y', 'a_y'};
step(sys1c,'b',sys1d,'r--')
legend('Continuous', 'Discrete')
%observability
Ob = obsv(sys1d);
unobs = rank(Ob) - length(sys1d.A);
%% kalman
sys1d.InputName = 'y_0';
sys1d.OutputName = {'y', 'a_y'};
Q =1000;
R = [0.00066004788240179 0; 0 0.0135502962738042];
%R = [.01^2 0; 0 0.0135502962738042];
Plant = ss(sys1d);
[kalmf,L,P,M] = kalman(Plant,Q,R);
%% RUNTHIS
u = setpoint_position_ned(:,2);
y = position_ned(:,2);
v = velocity_ned(:,2);
a = acel_NED(:,2);
measure = [y, a];
%%
x_kalm = lsim(kalmf, measure, t);
%%
figure
plot(t, v, 'k')
hold on
plot(t, x_kalm(:,4), 'r--')
%% CV
ve = x_kalm(:,4);
fit2 = goodnessOfFit(v, ve, 'NMSE')
fit2_love = (1-fit2)*100
vaf2 = (1-var(v-ve)/var(v))*100
%%
plot(t, v-ve)
%%
%plot(t,a)
%% adhoc kalman
%
% xhat = [position_ned(1,2);velocity_ned(1,2)];
% P = 0;
% A = sys1d.A;
% B = sys1d.B;
% C = sys1d.C;
% Q = 0.01^2;
% %R = [.01^2 0; 0 .036^2];
% R = [0.00066004788240179 0; 0 0.0135502962738042];
%
% xhatOut = zeros(2, 13901);
% for i=1:13901
% u=setpoint_position_ned(i,2);
% meas = measure(i,:)';
%
% % Propagate the state estimate and covariance matrix:
% xhat = A*xhat + B*u;
% P = A*P*A' + Q;
% % Calculate the Kalman gain
% K = P*C'/(C*P*C' + R);
% % Calculate the measurement residual
% resid = meas - C*xhat;
% % Update the state and error covariance estimate
% xhat = xhat + K*resid;
% P = (eye(size(K,1))-K*C)*P;
% % Post the results
% xhatOut(:,i) = xhat;
% yhatOut = C*xhatOut;
% end
%%
y0=setpoint_position_ned(:,2);
v = velocity_ned(:,2);
X = [0.099966628233391e-1 0.0999597602828939e-1 0.0999644859861622e-1*2];% 0.00066004788240179 0.0135502962738042];
sys1d=ss(sys1d);
[covari,FVAL]= fmincon(@(x) kalman_cost(x, sys1d, v, y0, measure), X, [],[],[],[],zeros(3,1),[]);
%% GA
% y0=setpoint_position_ned(:,2);
% v = velocity_ned(:,2);
% X = [.85 .84 .85 0.00066004788240179 0.0135502962738042];
% sys1d=ss(sys1d);
% [covari,FVAL]= ga(@(x) kalman_cost(x, sys1d, v, y0, measure), 5, [],[],[],[],zeros(5,1),ones(5,1));
%% adhoc kalman lovera notation
xhat = [position_ned(1,2);velocity_ned(1,2)];
xhat = [0;0];
F = sys1d.A;
G = sys1d.B;
H = sys1d.C;
%W = 0.01^2 *ones(2);
%covari= [9.99e-5 1e-4 9.99e-5];
W = [covari(1) covari(2) ; covari(2) covari(3)];
Pnew = 0;
%R = [.01^2 0; 0 .036^2];
V = [0.00066004788240179 0; 0 0.0135502962738042];
%V = [covari(4) 0;0 covari(5)];
y0=setpoint_position_ned(:,2);
states = zeros(2, 13901);
sigmayvec = zeros(1, 13901);
errorNorm = zeros(length(t),1);
innov = zeros(2, 13901);
innovcovar = zeros(4, 13901);
q = zeros(1, 13901);
for i=1:length(t)
u=y0(i);
meas = measure(i,:)';
% State estimate and error covariance extrapolation:
xhat = F*xhat + G*u;
Ppred = F*Pnew*F' + W;
% Innovation covariance
S = (H*Ppred*H' + V);
innovcovar(:,i) = reshape(S,[4,1]);
% Gain update:
K = Ppred*H'/S;
% Calculate the innovation
innov(:, i) = meas - H*xhat;
q(:,i) = innov(:,i)'*inv(S)*innov(:,i);
% State estimate and error covariance update:
xhat = xhat + K*innov(:,i);
Pnew = (Ppred-K*H*Ppred);
% Results
states(:,i) = xhat;
end
ve = states(2, :)';
%%
S = V + H*Ppred*H';
%%
ry = xcorr(innov(1,:));%, 'normalized');
ray = xcorr(innov(2,:));%, 'normalized');
sumQ =sum(q,2);
%%
plot(ry)
figure
plot(ray)
%%
figure
plot(t,q)
%%
N =length(t);
plot(t,ry(N:2*N-1)/ry(N),'b');
hold on
plot(-t,ry(N:2*N-1)/ry(N),'b');
%% innovation pos
figure
plot(t,3*sqrt(S(1,1))*ones(length(t),1), 'r--')
hold on
plot(t, innov(1,:), 'b')
plot(t,-3*sqrt(S(1,1))*ones(length(t),1), 'r--')
%% innovation postax
figure
subplot 211
plot(t,3*sqrt(S(1,1))*ones(length(t),1), 'r--')
title('Innovation Behaviour')
hold on
plot(t, innov(1,:), 'b')
plot(t,-3*sqrt(S(1,1))*ones(length(t),1), 'r--')
xlabel('Time [s]')
legend( '$\pm3\sigma$', '$y -\hat{y}$')
grid on
ylabel('Position')
subplot 212
plot(t, 3*sqrt(S(2,2))*ones(length(t),1), 'r--')
hold on
plot(t, innov(2,:), 'b')
plot(t, -3*sqrt(S(2,2))*ones(length(t),1), 'r--')
grid on
legend('$\pm3\sigma$','$a_y -\hat{a}_y $')
xlabel('Time [s]'), ylabel('Acceleration')
%% innovation acel
figure
plot(t, 3*sqrt(S(2,2))*ones(length(t),1), 'r--')
hold on
plot(t, innov(2,:), 'b')
plot(t, -3*sqrt(S(2,2))*ones(length(t),1), 'r--')
%% verify kalman
figure
plot(t, v, 'b')
hold on
plot(t, states(2, :), 'r--')
legend('Measured', 'Kalman')
grid on
ylabel('$v_y$ [$\frac{m}{s}$]')
xlabel('Time [s]')
title('Lateral velocity estimation')
%% animate kalman
%h = animatedline('Color', 'b', 'LineStyle', 'none', 'Marker','x');
%h1 = animatedline('Color', 'r', 'LineStyle', 'none', 'Marker', 'o');
h = animatedline('Color', 'b' );
h1 = animatedline('Color', 'r', 'LineStyle', '--');
%%
vplot=v(1:20:end);
veplot=ve(1:20:end);
tplot = t(1:20:end);
%%
axis([0,140,-1.6,1.6])
for k = 1:length(tplot)
addpoints(h, tplot(k), vplot(k));
addpoints(h1,tplot(k), veplot(k));
legend('Measured', 'Kalman')
grid on
ylabel('$v_y$ [$\frac{m}{s}$]')
xlabel('Time [s]')
title('Lateral velocity estimation')
drawnow limitrate
end
%% CV
ve = states(2, :)';
fit2 = goodnessOfFit(v, ve, 'NMSE')
fit2_love = (1-fit2)*100
vaf2 = (1-var(v-ve)/var(v))*100
%% measurement error
e = v-ve;
%e = e -mean (e);
sigmay = sqrt(Ppred(2,2));
covplus = ones(length(t),1)*3*sigmay;
covminus = -ones(length(t),1)*3*sigmay;
J1 = (var(v-ve)-Ppred(2,2))
%%
hist(e)
%%
figure
plot(t,e, 'b')
hold on
plot(t, covplus, 'r--')
plot(t, covminus, 'r--')
grid on
legend('$e=v_m-v_K$', '$\pm3\sigma$')
xlabel('Time [s]')
ylabel('$v_y$ [$\frac{m}{s}$]')
title('Estimation error')
%%
% figure
% plot(t,e, 'b')
% hold on
% plot(t, sqrt(sigmayvec)*3, 'r--')
% plot(t, -sqrt(sigmayvec)*3, 'r--')
% grid on
% legend('$e=v_m-v_K$', '$\pm3\sigma$')
% xlabel('Time [s]')
% ylabel('$v_y$ [$\frac{m}{s}$]')
% title('Estimation error')
%%
x = t;
y = ve;
xconf = [x x(end:-1:1)] ;
yconf = [y'+3*sigmay y(end:-1:1)'-3*sigmay];
figure
p = fill(xconf,yconf,'red');
p.FaceColor = [1 0.8 0.8];
p.EdgeColor = 'none';
hold on
plot(x,y,'b-')
grid on
legend('$\pm3\sigma$','Kalman')
title('99\% confidence interval')
xlabel('Time [s]')
ylabel('$v_y$ [$\frac{m}{s}$]')