PredictStates.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. function [quat, states, Tbn, correctedDelAng, correctedDelVel] = PredictStates( ...
  2. quat, ... % previous quaternion states 4x1
  3. states, ... % previous states (3x1 rotation error, 3x1 velocity, 3x1 gyro bias)
  4. dAng, ... % IMU delta angles, 3x1 (rad)
  5. dVel, ... % IMU delta velocities 3x1 (m/s)
  6. dt) % time since last IMU measurement (sec)
  7. % Define parameters used for previous angular rates and acceleration shwich
  8. % are used for trapezoidal integration
  9. % define persistent variables for previous delta angle and velocity which
  10. % are required for sculling and coning error corrections
  11. persistent prevDelAng;
  12. if isempty(prevDelAng)
  13. prevDelAng = dAng;
  14. end
  15. persistent prevDelVel;
  16. if isempty(prevDelVel)
  17. prevDelVel = dVel;
  18. end
  19. % Remove sensor bias errors
  20. dAng = dAng - states(7:9);
  21. % Apply rotational and skulling corrections
  22. correctedDelVel= dVel + ...
  23. 0.5*cross(prevDelAng + dAng , prevDelVel + dVel) + 1/6*cross(prevDelAng + dAng , cross(prevDelAng + dAng , prevDelVel + dVel)) + ... % rotational correction
  24. 1/12*(cross(prevDelAng , dVel) + cross(prevDelVel , dAng)); % sculling correction
  25. % Apply corrections for coning errors
  26. correctedDelAng = dAng - 1/12*cross(prevDelAng , dAng);
  27. % Save current measurements
  28. prevDelAng = dAng;
  29. prevDelVel = dVel;
  30. % Convert the rotation vector to its equivalent quaternion
  31. deltaQuat = RotToQuat(correctedDelAng);
  32. % Update the quaternions by rotating from the previous attitude through
  33. % the delta angle rotation quaternion
  34. quat = QuatMult(quat,deltaQuat);
  35. % Normalise the quaternions
  36. quat = NormQuat(quat);
  37. % Calculate the body to nav cosine matrix
  38. Tbn = Quat2Tbn(quat);
  39. % transform body delta velocities to delta velocities in the nav frame
  40. delVelNav = Tbn * correctedDelVel + [0;0;9.807]*dt;
  41. % Sum delta velocities to get velocity
  42. states(4:6) = states(4:6) + delVelNav(1:3);
  43. end