forked from TauLabs/TauLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the altitude KF to use the updated measurements appropriately …
…and check in the relevant matlab code to check it and genererate it. Produces quite smooth traces.
- Loading branch information
1 parent
fbb0b11
commit 7d4582e
Showing
3 changed files
with
233 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
baro = fixTime(BaroAltitude); | ||
accel = fixTime(Accels); | ||
attitude = fixTime(AttitudeActual); | ||
|
||
|
||
Gamma = diag([1e-5 1e-5 0.00001 1e-20]); % state noise | ||
accel_sigma = 1; | ||
baro_sigma = 1; | ||
Nu = diag([10 10 10 10]); | ||
z = zeros(length(accel.z),4); | ||
Nu_n = zeros([4 4 length(accel.z)]); | ||
Nu_n(:,:,1) = Nu; | ||
|
||
t = max(accel.timestamp(1), baro.timestamp(1)); | ||
last_t = t-1; | ||
last_accel_idx = 1; | ||
last_baro_idx = 1; | ||
i = 1; | ||
timestamp = []; | ||
|
||
z(1) = baro.Altitude(1); | ||
z(1:5,4) = 0.4; | ||
timestamp(1) = t; | ||
log_accel = 0; | ||
while (last_accel_idx + 1) <= length(accel.z) && (last_baro_idx + 1) <= length(baro.Altitude) | ||
|
||
update_baro = baro.timestamp(last_baro_idx + 1) < t; | ||
update_accel = accel.timestamp(last_accel_idx + 1) < t; | ||
|
||
if 0 && update_accel | ||
[~,idx] = min(abs(attitude.timestamp - accel.timestamp(last_accel_idx+1))); | ||
Rbe = Quat2Rbe([attitude.q1(idx), attitude.q2(idx), attitude.q3(idx), attitude.q4(idx)]); | ||
idx = last_accel_idx + 1; | ||
accel_ned = Rbe * [accel.x(idx); accel.y(idx); accel.z(idx)]; | ||
accel_ned = accel_ned(3); | ||
% if(abs(accel_ned) < 1e-1) | ||
% keyboard | ||
% end | ||
else | ||
accel_ned = accel.z(last_accel_idx + 1); | ||
end | ||
log_accel(i) = accel_ned; | ||
if update_baro && update_accel; | ||
x = [baro.Altitude(last_baro_idx + 1); -accel_ned-9.81]; | ||
last_baro_idx = last_baro_idx + 1; | ||
last_accel_idx = last_accel_idx + 1; | ||
C = [1 0 0 0; 0 0 1 -1]; | ||
Sigma = diag([baro_sigma; accel_sigma]); | ||
elseif update_accel | ||
x = -accel_ned - 9.81; | ||
last_accel_idx = last_accel_idx + 1; | ||
C = [0 0 1 -1]; | ||
Sigma = [accel_sigma]; | ||
elseif update_baro | ||
x = [baro.Altitude(last_baro_idx + 1)]; | ||
last_baro_idx = last_baro_idx + 1; | ||
C = [1 0 0 0]; | ||
Sigma = [baro_sigma]; | ||
else | ||
% Take a timestep and look for advance | ||
t = t + 0.1; | ||
continue; | ||
end | ||
%[last_baro_idx last_accel_idx] | ||
t = max(baro.timestamp(last_baro_idx), accel.timestamp(last_accel_idx)); | ||
dT = (t - last_t) / 1000; | ||
if(dT == 0) | ||
dT = 1.5 / 1000; | ||
end | ||
assert(dT ~= 0,'WTF'); | ||
last_t = t; | ||
|
||
i = i + 1; | ||
|
||
% Zero out non-diag covariance | ||
Nu = diag(diag(Nu)); | ||
|
||
A = [1 dT 0 0; 0 1 dT 0; 0 0 1 0; 0 0 0 1]; | ||
|
||
P = A * Nu * A' + Gamma; | ||
K = P*C'*(C*P*C'+Sigma)^-1; | ||
|
||
z(i,:) = A * z(i-1,:)' + K * (x - C * A * z(i-1,:)'); | ||
timestamp(i) = t; | ||
Nu = (eye(4) - K * C) * P; | ||
Nu_n(:,:,i) = Nu; | ||
|
||
z(i,4) = 0; | ||
if mod(i,10000) == 0 | ||
subplot(311) | ||
plot(baro.timestamp, baro.Altitude, '.', timestamp(1:i),z(1:i,1),'r','LineWidth',5) | ||
subplot(312) | ||
plot(timestamp(1:i),z(1:i,2),'k') | ||
subplot(313) | ||
plot(timestamp(1:i),z(1:i,3),'k'); | ||
xlim(timestamp([1,i])) | ||
drawnow | ||
end | ||
end | ||
|
||
subplot(311) | ||
plot(baro.timestamp, baro.Altitude, '.', timestamp(1:i),z(1:i,1),'r','LineWidth',5) | ||
subplot(312) | ||
plot(timestamp(1:i),z(1:i,2),'k') | ||
subplot(313) | ||
plot(timestamp(1:i),z(1:i,3),'k'); | ||
xlim(timestamp([1,i])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
% Generate the symbolic code for the kalman filter on altitude | ||
|
||
dT = sym('dT','real'); | ||
A = [1 dT 0; 0 1 dT; 0 0 1]; | ||
Nu = diag([sym('V[1]') sym('V[2]') sym('V[3]')]); | ||
|
||
Gamma = diag([sym('G[1]') sym('G[2]') sym('G[3]')]); | ||
Sigma = diag([sym('S[1]') sym('S[2]')]); | ||
C = [1 0 0; 0 0 1]; | ||
state = [sym('z[1]'); sym('z[2]'); sym('z[3]')]; | ||
measure = [sym('x[1]'); sym('x[2]')]; | ||
|
||
P = simplify(A * Nu * A' + Gamma); | ||
K = simplify(P*C'*(C*P*C'+Sigma)^-1); | ||
|
||
P_mat = [sym('P[1][1]') sym('P[1][2]') sym('P[1][3]'); ... | ||
sym('P[2][1]') sym('P[2][2]') sym('P[2][3]'); ... | ||
sym('P[3][1]') sym('P[3][2]') sym('P[3][3]')]; | ||
K_mat = [sym('K[1][1]') sym('K[1][2]'); ... | ||
sym('K[2][1]') sym('K[2][2]'); ... | ||
sym('K[3][1]') sym('K[3][2]')]; | ||
|
||
z_new = A * state + K_mat * (measure - C * A * state); | ||
V = (eye(3) - K_mat * C) * P_mat; | ||
|
||
ccode(P) | ||
ccode(K) | ||
ccode(z_new) | ||
ccode(V) | ||
|
||
|
||
%% For when there is no baro update | ||
% Generate the symbolic code for the kalman filter on altitude | ||
|
||
dT = sym('dT','real'); | ||
A = [1 dT 0; 0 1 dT; 0 0 1]; | ||
Nu = diag([sym('V[1]') sym('V[2]') sym('V[3]')]); | ||
|
||
Gamma = diag([sym('G[1]') sym('G[2]') sym('G[3]')]); | ||
Sigma = sym('S[2]'); | ||
C = [0 0 1]; | ||
state = [sym('z[1]'); sym('z[2]'); sym('z[3]')]; | ||
measure = [sym('x[2]')]; | ||
|
||
P = simplify(A * Nu * A' + Gamma); | ||
K = simplify(P*C'*(C*P*C'+Sigma)^-1); | ||
|
||
P_mat = [sym('P[1][1]') sym('P[1][2]') sym('P[1][3]'); ... | ||
sym('P[2][1]') sym('P[2][2]') sym('P[2][3]'); ... | ||
sym('P[3][1]') sym('P[3][2]') sym('P[3][3]')]; | ||
K_mat = [sym('K[1][1]'); ... | ||
sym('K[2][1]'); ... | ||
sym('K[3][1]')]; | ||
|
||
z_new = A * state + K_mat * (measure - C * A * state); | ||
V = (eye(3) - K_mat * C) * P_mat; | ||
|
||
ccode(P) | ||
ccode(K) | ||
ccode(z_new) | ||
ccode(V) |