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
|
close all;
clear all;
clc;
dt=0.1;
duration=1000;
measnoise1 = 50;
accelnoise1 = .125;
measnoise2=pi/180;
accelnoise2=.125;
a1 = [1 dt; 0 1]; % transition matrix
b1 = [dt^2/2; dt]; % input matrix
c1 = [1 0]; % measurement matrix
a2=a1;b2=b1;c2=c1;
x1 = [0; 200]; % initial state vector
x2= [0;300];
a = [a1 zeros(2,2); zeros(2,2) a2];
x = [x1;x2];
%---------------------------------------------------------------------
xhat = x; % initial state estimate
Sz=[measnoise1^2 0;0 measnoise2^2 ]%measurement error covariance
W = [accelnoise1*dt^2/2; accelnoise1*dt; accelnoise2*dt^2/2; accelnoise2*dt];
Sw = W*W';% process noise cov
P = Sw; % initial estimation covariance
% Initialize arrays for later plotting.
pos1 = [];pos2 = []; % true position array
poshat1 = [];poshat2 = []; % estimated position array
posmeas1 = [];posmeas2 = []; % measured position array
vel1 = [];vel2 = []; % true velocity array
velhat1 = [];velhat2 = []; % estimated velocity array
for t = 0 : dt: duration,
% Use a constant commanded acceleration of 1 m/sec^2.
u = [0; 0];
% Simulate the linear system.
ProcessNoise1 = accelnoise1 * [(dt^2/2)*randn; dt*randn];
ProcessNoise2 = accelnoise2 * [(dt^2/2)*randn; dt*randn];
ProcessNoise = [ ProcessNoise1 ; ProcessNoise2];
x = a * x + ProcessNoise;
% Simulate the noisy measurement
MeasNoise1 = measnoise1 * randn;
MeasNoise2 = measnoise2* randn ;
MeasNoise = [MeasNoise1 ; MeasNoise2];
y = [sqrt((x(1)^2)+(x(3)^2)) ; atan2(x(3),x(1))] + MeasNoise;
% Extrapolate the most recent state estimate to the present time.
xhat = a * xhat + b * u;
yc = [sqrt((xhat(1)^2)+(xhat(3)^2)) ; atan2(xhat(3),xhat(1))] + MeasNoise;
h = [xhat(1)/sqrt((xhat(1)^2)+(xhat(3)^2)) 0 xhat(3)/sqrt((xhat(1)^2)+(xhat(3)^2)) 0 ;
(-xhat(3)/((xhat(1)^2)+(xhat(3)^2))) 0 (xhat(1)/((xhat(1)^2)+(xhat(3)^2))) 0];%4X4
% Form the Innovation vector.
Inn = y -yc;
% Compute the covariance of the Innovation.
s = h * P * h' + Sz;
% Form the Kalman Gain matrix.
K = a * P * h' * inv(s);
% Update the state estimate.
xhat = xhat + K * Inn;
% Compute the covariance of the estimation error.
P = a * P * a' - a * P * h' * inv(s) * h * P * a' + Sw;
% Save some parameters for plotting later.
pos1 = [pos1; x(1)];
posmeas1 = [posmeas1; y(1)*cos(y(2))];
poshat1 = [poshat1; xhat(1)];
vel1 = [vel1; x(2)];
velhat1 = [velhat1; xhat(2)];
pos2 = [pos2; x(3)];
posmeas2 = [posmeas2; y(1)*sin(y(2))];
poshat2 = [poshat2; xhat(3)];
vel2 = [vel2; x(4)];
velhat2 = [velhat2; xhat(4)];
% plot(pos1,pos2,'o', posmeas1,posmeas2,'*', poshat1,poshat2,'+');
% grid;
% xlabel('X (m)');
% ylabel('Y (m)');
% title('Target Position (True, Measured, and Estimated)')
% legend('True','Measured','Estimated');
%
% pause();
end
t = 0 : dt : duration;
close all;
figure;
subplot(2,2,1);
plot(t,pos1, t,posmeas1, t,poshat1);
grid;
xlabel('Time (sec)');
ylabel('X (m)');
title('Target Position following X axis (True, Measured, and Estimated)')
legend('True','Measured','Estimated');
subplot(2,2,2);
plot(t,pos1-posmeas1, t,pos1-poshat1);
grid;
xlabel('Time (sec)');
ylabel('Position Error following X axis (m)');
title('Position Measurement Error and Position Estimation Error');
legend('Measurement Error','Estimation Error');
subplot(2,2,3);
plot(t,pos2, t,posmeas2, t,poshat2);
grid;
xlabel('Time (sec)');
ylabel('Y (m)');
title('Target Position following Y axis (True, Measured, and Estimated)')
legend('True','Measured','Estimated');
subplot(2,2,4);
plot(t,pos2-posmeas2, t,pos2-poshat2);
grid;
xlabel('Time (sec)');
ylabel('Position Error following Y axis (m)');
title('Position Measurement Error and Position Estimation Error');
legend('Measurement Error','Estimation Error');
figure;
subplot(2,2,1);
plot(t,vel1, t,velhat1);
grid;
xlabel('Time (sec)');
ylabel('Velocity (m/sec)');
title('Velocity following X axis (True and Estimated)');
legend('true','Estimated');
subplot(2,2,2);
plot(t,vel1-velhat1);
grid;
xlabel('Time (sec)');
ylabel('Velocity Error following X axis (m/sec)');
title('Velocity Estimation Error');
subplot(2,2,3);
plot(t,vel2, t,velhat2);
grid;
xlabel('Time (sec)');
ylabel('Velocity (m/sec)');
title('Velocity following Y axis (True and Estimated)');
legend('true','Estimated');
subplot(2,2,4);
plot(t,vel2-velhat2);
grid;
xlabel('Time (sec)');
ylabel('Velocity Error following Y axis (m/sec)');
title('Velocity Estimation Error');
figure;
%plot(pos1,pos2, posmeas1,posmeas2,'*', poshat1,poshat2);
plot(pos1,pos2,'o', posmeas1,posmeas2,'*', poshat1,poshat2,'+');
grid;
xlabel('X (m)');
ylabel('Y (m)');
title('Target Position (True, Measured, and Estimated)')
legend('True','Measured','Estimated'); |
Partager