PredictStates.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. angRate, ... % IMU rate gyro measurements, 3x1 (rad/sec)
  5. accel, ... % IMU accelerometer measurements 3x1 (m/s/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. persistent prevAngRate;
  10. if isempty(prevAngRate)
  11. prevAngRate = angRate;
  12. end
  13. persistent prevAccel;
  14. if isempty(prevAccel)
  15. prevAccel = accel;
  16. end
  17. % define persistent variables for previous delta angle and velocity which
  18. % are required for sculling and coning error corrections
  19. persistent prevDelAng;
  20. if isempty(prevDelAng)
  21. prevDelAng = prevAngRate*dt;
  22. end
  23. persistent prevDelVel;
  24. if isempty(prevDelVel)
  25. prevDelVel = prevAccel*dt;
  26. end
  27. % Convert IMU data to delta angles and velocities using trapezoidal
  28. % integration
  29. dAng = 0.5*(angRate + prevAngRate)*dt;
  30. dVel = 0.5*(accel + prevAccel )*dt;
  31. prevAngRate = angRate;
  32. prevAccel = accel;
  33. % Remove sensor bias errors
  34. dAng = dAng - states(7:9);
  35. % Apply rotational and skulling corrections
  36. correctedDelVel= dVel + ...
  37. 0.5*cross(prevDelAng + dAng , prevDelVel + dVel) + 1/6*cross(prevDelAng + dAng , cross(prevDelAng + dAng , prevDelVel + dVel)) + ... % rotational correction
  38. 1/12*(cross(prevDelAng , dVel) + cross(prevDelVel , dAng)); % sculling correction
  39. % Apply corrections for coning errors
  40. correctedDelAng = dAng - 1/12*cross(prevDelAng , dAng);
  41. % Save current measurements
  42. prevDelAng = dAng;
  43. prevDelVel = dVel;
  44. % Convert the rotation vector to its equivalent quaternion
  45. deltaQuat = RotToQuat(correctedDelAng);
  46. % Update the quaternions by rotating from the previous attitude through
  47. % the delta angle rotation quaternion
  48. quat = QuatMult(quat,deltaQuat);
  49. % Normalise the quaternions
  50. quat = NormQuat(quat);
  51. % Calculate the body to nav cosine matrix
  52. Tbn = Quat2Tbn(quat);
  53. % transform body delta velocities to delta velocities in the nav frame
  54. delVelNav = Tbn * correctedDelVel + [0;0;9.807]*dt;
  55. % Sum delta velocities to get velocity
  56. states(4:6) = states(4:6) + delVelNav(1:3);
  57. end