FuseVelocity.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. function [...
  2. states, ... % state vector after fusion of measurements
  3. P, ... % state covariance matrix after fusion of corrections
  4. innovation,... % NED velocity innovations (m/s)
  5. varInnov] ... % NED velocity innovation variance ((m/s)^2)
  6. = FuseVelocity( ...
  7. states, ... % predicted states from the INS
  8. P, ... % predicted covariance
  9. measVel) % NED velocity measurements (m/s)
  10. R_OBS = 0.5^2;
  11. innovation = zeros(1,3);
  12. varInnov = zeros(1,3);
  13. % Fuse measurements sequentially
  14. for obsIndex = 1:3
  15. stateIndex = 4 + obsIndex;
  16. % Calculate the velocity measurement innovation
  17. innovation(obsIndex) = states(stateIndex) - measVel(obsIndex);
  18. % Calculate the Kalman Gain
  19. H = zeros(1,10);
  20. H(1,stateIndex) = 1;
  21. varInnov(obsIndex) = (H*P*transpose(H) + R_OBS);
  22. K = (P*transpose(H))/varInnov(obsIndex);
  23. % Calculate state corrections
  24. xk = K * innovation(obsIndex);
  25. % Apply the state corrections
  26. states = states - xk;
  27. % re-normalise the quaternion
  28. quatMag = sqrt(states(1)^2 + states(2)^2 + states(3)^2 + states(4)^2);
  29. states(1:4) = states(1:4) / quatMag;
  30. % Update the covariance
  31. P = P - K*H*P;
  32. % Force symmetry on the covariance matrix to prevent ill-conditioning
  33. P = 0.5*(P + transpose(P));
  34. % ensure diagonals are positive
  35. for i=1:10
  36. if P(i,i) < 0
  37. P(i,i) = 0;
  38. end
  39. end
  40. end
  41. end