-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalman_sim_angle.m
125 lines (95 loc) · 2.33 KB
/
Kalman_sim_angle.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
clear all
find_optimal = 0;
complimentary = 0;
dt = 0.1; % Sampling delay
t = 0:dt:100; % time
holdN = 1; % hold reference for # accelrometer samples
% Kalman gains
Q = 0.1; % "Movement variation"
R = 0.3162; % "Location Measurement noise"
% Intinalize
F = [1, -dt; % State transistion
0, 1];
B = [dt ;
0 ];
H = [1, 0]; % Observation model
% Ground thurth
v = 10*cos(pi*t/10)+20*cos(pi*t); % Speed
loc = [cumsum(v*dt); % Location
v]; % Speed
% Measurments (added noise)
u = v + 50*(rand(1,length(v))-0.5);
z = loc(1,:) + 400*(rand(1,length(v))-0.5);
% Sample and hold z
z = sampleNhold(z,holdN);
figure(5)
subplot(2,1,1)
plot(z)
title('Angle measured')
subplot(2,1,2)
plot(u)
title('Speed measured')
if(find_optimal)
errorL = zeros(16);
errorV = zeros(16);
for i = 1:16;
for j = 1:16;
Q = 10^-(i/2);
R = 10*10^-(j/2-0.5);
X = Kalman1D_angle( Q, R, F, B, u, z, H, dt, t);
errorL(i,j) = sum([loc(1,500:end)-X(1,500:end)]'.^2);
errorV(i,j) = sum([loc(2,500:end)-X(2,500:end)]'.^2);
end
end
figure(2)
surf(log(errorL));
title('Location error');
figure(3)
surf(log(errorV));
title('Velocity error');
% Find optimal gains
[minerrK,ind] = min(errorL(:));
minerrK
minerrKV = errorV(ind)
[m,n] = ind2sub(size(errorL),ind);
Q = 10^-(m/2);
R = 10*10^-(n/2-0.5);
end
if(complimentary)
fact = 0:0.01:1;
errorCL = zeros(1,length(fact));
errorCV = errorCL;
for i = 1:length(fact);
X = compfilter(u, z, fact(i), dt);
errorCL(i) = sum([loc(1,:)-X(1,:)]'.^2);
errorCV(i) = sum([loc(2,:)-X(2,:)]'.^2);
end
figure(4)
plot(fact,[log(errorCL); log(errorCV)]');
title('Complimentary filter error')
[minerrC,ind] = min(errorCL);
minlocC = errorCL(ind)
minvelC = errorCV(ind)
Xc = compfilter(u, z, fact(ind), dt);
end
X = Kalman1D_angle( Q, R, F, B, u, z, H, dt, t);
figure(1)
subplot(2,1,1)
plot([loc(1,:);X(1,:)]')
hold on;
plot(z,'+');
hold off;
title('Position');
if(complimentary)
hold on;
plot(Xc(1,:)', 'black')
hold off;
end
subplot(2,1,2)
plot([loc(1,:)-X(1,:)]');
title('Error')
if(complimentary)
hold on;
plot(loc(1,:)-Xc(1,:), 'black');
hold off;
end