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

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):
But, I expect a plot with a consistent average value for acceleration in the z axis like the following:
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?

Related

IMU fall detection

I am trying to detect a free fall scenario. I have accelerometer and gyroscope.
A simple fall I can detect by inspecting a total acceleration of 0g
However, my problem is when the IMU falls and rotates at the same time (centrifugal force). Any idea how to distinguish this scenario?
I don't have the solution, but let me state 2 points:
if the IMU is near the center of mass of your hardware, the centrifugal acceleration should be negligible
If you have a constant rotation you should read a constant rate on the gyroscope, and a constant acceleration too ( if the IMU is not in the center of mass). Morover, the constant rotation should be on an axis perpendicular to that of the constant rotation

Unity3D relative rotation of objects on an axis

In the project I am using IMU sensors to track real player's hand and apply the transformation values onto 3D hand inside Unity.
As IMU sets the Y axis orientation relative to magnetic north of the earth, in the game, hand initializes on specific direction.
What I want is to calculate the offset of IMU's given Y values and 3D hand's original Y rotation, so that I can subtract that value to the 3D model's Y rotation (that will seem like player's initial Y Rotation is the same as 3D hand's). Code would be: transform.Rotate(Vector3.up, offset, Space.World);
IMU sends Euler angles (does it well, as I wasn't able to get Gimbal lock)
As I understand, I need to find out angle difference between 3D Hand's initial rotation and IMU's given initial rotation on XZ plane (or through Y Axis)
How do I calculate the offset?
You can use Quaternion.FromToRotation to calculate offset, something like:
var offset = Quaternion.FromToRotation(Vector3.up, imuUp);
transform.rotation *= offset;

Remove gravity from raw accelerometer data of IMU--> please approve math and algo

I am using this device (http://www.sparkfun.com/products/10724) and have successfully implemented an quite well working orientation estimation based on a fusion of magnetometer, accelerometer and gyroscope data based on this http://www.x-io.co.uk/node/8#open_source_imu_and_ahrs_algorithms implementation. Now I want to calculate the dynamic acceleration (measures acceleration without static gravity acceleration). For doing this I came to the following idea.
Calculate a running average of the raw accelerometer data. If the raw acceleration is stable for some time (small difference between running average and current measured raw data) we assume the device does not move and we are measuring the raw gravity. Now save the gravity vector and also current orientation as quaternion. This approach assumes that our device could not be accelerated constantly without gravity.
For calculating the acceleration without gravity I am now doing following quaternion calculation:
RA = Quaternion with current x,y,z raw acceleration values
GA = Quaternion with x,y,z raw acceleration values of estimated gravity
CO = Quaternion of current orientation
GO = saved gravity orientation
DQ = GO^-1 * CO // difference of orientation between last gravity estimation and current orientation
DQ = DQ^-1 // get the inverse of the difference
SQ = DQ * GA * DQ^1 // rotate gravity vector
DA = RA - SQ // get the dynamic acceleration by subtracting the rotated gravity from the raw acceleration
Could someone check if this is correct? I am not sure because on testing it I get some high acceleration on rotating my sensor board, but I am able to get some acceleration data (but is is much smaller than the accelration during rotation) if the device is moved without rotating it.
Moreover I have the question if the accelerometer is also measuring acceleration if it is rotated on place or not!
Another way is to differentiate accel to give jerk (using finite difference, j = (a2 - a1) / dt). Run the jerk through a decay/leakage function (use a half life decay calc value rather than a simple multiplier). Then integrate the jerk (trapezoidal rule, a = dt * (j1 + j2) * 0.5) and it will remove the DC offset (gravity). Again run this signal through a decay function.
The decay functions avoid the value spiraling off but will reduce the magnitude of dynamic acceleration values that you see and will introduce some shaping to the signal. So you won't get values that are 'accurate' m/s/s readings any longer. But it is useful for short-time movements.
Of course you could just use a highpass filter instead but that generally requires a fixed sampling rate and is probably more computationally expensive if you are using convolution (finite impulse response filter).
It's easier than you think. You may wanna have a look at my post here about it:
http://www.varesano.net/blog/fabio/simple-gravity-compensation-9-dom-imus

Using the iPhone accelerometer in a car

I want to use the iPhones's accelerometer to detect motions while driving. I'm a bit confused what the accelerometer actually measures, especially when driving a curve.
As you can see in the picture, a car driving a curve causes two forces. One is the centripetal force and one is the velocity. Imagine the iPhone is placed on the dashboard with +y-axis is pointing to the front, +x-axis to the right and +z-axis to the top.
My Question is now what acceleration will be measured when the car drives this curve. Will it measure g-force on the -x-axis or will the g-force appear on the +y axis?
Thanks for helping!
UPDATE!
For thoses interested, as one of the answers suggested it measures both. The accelerometer is effected by centrifugal force and velocity resulting in an acceleration vector that is a combination of these two.
I think it will measure both. But don't forget that the sensor will measure gravity as well. So when your car is not moving, you will still get accelerometer readings. A nice talk on sensors in smartphones http://www.youtube.com/watch?v=C7JQ7Rpwn2k&feature=results_main&playnext=1&list=PL29AD66D8C4372129 (it's on android, but the same type of sensors are used in iphone).
Accelerometer measures acceleration of resultant force applied to it (velocity is not a force by the way). In this case force is F = g + w + c i.e. vector sum of gravity, centrifugal force (reaction to steering centripetal force, points from the center of the turn) and car acceleration force (a force changing absolute value of instantaneous velocity, points along the velocity vector). Providing Z axis of accelerometer always points along the gravity vector (which is rare case for actual car) values of g, w and c accelerations can be accessed in Z, X and Y coordinates respectively.
Unless you are in free fall the g-force (gravity) is always measured. If I understand your setup correctly, the g-force will appear on the z axis, the axis that is vertical in the Earth frame of reference. I cannot tell whether it will be +z or -z, it is partly convention so you will have to check it for yourself.
UPDATE: If the car is also going up/downhill then you have to take the rotation into account. In other words, there are two frames of reference: the iPhone's frame of reference and the Earth frame of reference. If you would like to deal with this situation, then please ask a new question.

Detecting the direction of the accelerometer movement on the y axis

I currently detect movement on the y axis. How does one calculate the direction it moved on the axis?
I get the same values when moving up or down.
Is the Gyro needed for this?
Do remember that the accelerometer will reflect the force of gravity. So movement up and down will generally be reflected as 9.81 m/s2 plus or minus the actual acceleration of the device relative to the Earth.