Linear Acceleration Without Gravity

Linear Acceleration Without Gravity

andynovak12andynovak12 Member Posts: 8
Hello,

I am trying to get the user-generated acceleration, which is the linear acceleration without the gravity reading. Is there something in the library that returns that result, similar to Apple's <span>CMDeviceMotion</span> object that contains that info (https://developer.apple.com/documentation/coremotion/getting_processed_device-motion_data)? If not, is there a calculation I can do with the accessible sensor outputs?

Thanks in advance,
Andy

Best Answer

  • Accepted Answer
    andynovak12andynovak12 Member Posts: 8
    Hi @[email protected]

    Thank you for the response. I understand the way accelerometer readings are handled by the SDK. My question was more about how to eliminate the acceleration due to gravity from the accelerometer reading. For example, to do this when the device is lying flat on a table, we simply have to add 1 to the z component of the acceleration vector, resulting in an acceleration vector of [0,0,0]. However, when the device is lying at an angle, that acceleration due to gravity acts on multiple axes of the device, and the math to subtract gravity from the accelerometer reading becomes more difficult.

    I have spent some time looking into this and came up with a solution using the rotation and acceleration readings returned from the SDK.

    Referencing the links below, I put wrote a function in Swift that works close enough to what I was aiming for. When the device is at rest, the acceleration vector's values are within 0.05. You have to enable the rotation and 
    accelerometer sensors of the device, and then use the two reading as inputs for this function, and the output is the acceleration due to the user's movements:

    func userAcceleration(rawAcceleration: Vector, rotation: Quaternion) -> Vector {
       let gravityVectorInWorldCoordinates = Vector(0, 0, 1)
       let accelerationInWorldCoordinated = rotation.act(rawAcceleration)
       return accelerationInWorldCoordinated - gravityVectorInWorldCoordinates
    }

    In my opinion, userAcceleration is a useful property to have accessible to developers, so they don't have to read up on Quaternions. Perhaps Bose could include it in a future release of their SDK.

    References: 
    https://stackoverflow.com/questions/18252692/gravity-compensation-in-accelerometer-data
    https://developer.apple.com/documentation/accelerate/simd/working_with_quaternions

Answers

Sign In or Register to comment.