AlignTilt.m 938 B

1234567891011121314151617181920
  1. function quat = AlignTilt( ...
  2. quat, ... % quaternion state vector
  3. initAccel) % initial accelerometer vector
  4. % check length
  5. lengthAccel = sqrt(dot([initAccel(1);initAccel(2);initAccel(3)],[initAccel(1);initAccel(2);initAccel(3)]));
  6. % if length is > 0.7g and < 1.4g initialise tilt using gravity vector
  7. if (lengthAccel > 5 && lengthAccel < 14)
  8. % calculate length of the tilt vector
  9. tiltMagnitude = atan2(sqrt(dot([initAccel(1);initAccel(2)],[initAccel(1);initAccel(2)])),-initAccel(3));
  10. % take the unit cross product of the accel vector and the -Z vector to
  11. % give the tilt unit vector
  12. if (tiltMagnitude > 1e-3)
  13. tiltUnitVec = cross([initAccel(1);initAccel(2);initAccel(3)],[0;0;-1]);
  14. tiltUnitVec = tiltUnitVec/sqrt(dot(tiltUnitVec,tiltUnitVec));
  15. tiltVec = tiltMagnitude*tiltUnitVec;
  16. quat = [cos(0.5*tiltMagnitude); tiltVec/tiltMagnitude*sin(0.5*tiltMagnitude)];
  17. end
  18. end
  19. end