Why does CMDeviceMotion attitude quaternion not always rotate to earth frame?

Viewed 33

I wrote an app that writes gravity, userAcceleration, and attitude quaternion to CSV while driving in a vehicle. The intent is to capture the dynamics of the vehicle (e.g. braking, accelerating, cornering) in the earth frame using an iPhone.

Then, I sum gravity and userAcceleration, and rotate the resulting raw acceleration vector by the quaternion provided by CMAttitude to get the acceleration in the earth frame. In about 60% of recording sessions, the average z values are not +9.81m/s^2 and jump to varying magnitudes besides +9.81m/s^2. For example (each tick mark in the y-axis represents 5m/s^2): enter image description here

But, I expect a plot with a consistent average value for acceleration in the z axis like the following: enter image description here

When I start device motion updates, I use the xMagneticNorthZVertical attitude reference frame, like so:

motionManager.startDeviceMotionUpdates(using: .xMagneticNorthZVertical, to: OperationQueue(), withHandler: didUpdateDeviceMotion)

The following computes raw acceleration in the global frame using the attitude quaternion:

   let accel = CMAcceleration(x: (motion.userAcceleration.x+motion.gravity.x), y: (motion.userAcceleration.y+motion.gravity.y), z: (motion.userAcceleration.z+motion.gravity.z))
   let a = SIMD3<Double>(accel.x, accel.y, accel.z)
   let a_vehicle = simd_act(attitude.quaternion, a)

I also have written the equivalent in MATLAB resulting with the same problem.

  • xMagneticNorthZVertical should result in an attitude that computes the direction of gravity. The direction of X or Y does not matter to me.
  • I do not have any magnets in the vicinity to skew the computed attitude.
  • In contrast: Android's rotationVector consistently rotates the accelerometer readings to the earth frame. Surely the quality of iPhone is better than Android.

What might be the cause of the attitude quaternion to not always rotate the device frame to the earth frame such that Z is in the direction of gravity?

0 Answers
Related