将加速度数据调整到全局参考框架 [英] Adjust Acceleration Data to Global Frame of Reference

查看:44
本文介绍了将加速度数据调整到全局参考框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Core Motion 时,加速度似乎总是相对于设备返回.这意味着如果您向上摇动设备屏幕,它将在 +/-Z(取决于初始参考),如果然后将设备转向一侧并上下摇晃,则与以前相同,加速度将在 +/-X 或 Y.

Working with Core Motion the acceleration appears to always be returned with respect to the device. Meaning if you shake the device screen up it will be in the +/-Z (depending on initial reference) if you then turn the device to the side and shake up and down, the same as before, the acceleration will be in the +/-X or Y.

我想将此设备特定的加速度转换回全局"框架.这意味着设备的相同物理运动会导致加速度数据在同一轴上返回,而不管设备在晃动时的方向如何.

I want to convert this device specific acceleration back to a "global" frame. Meaning that the same physical movements of the device result in the acceleration data coming back in the same axis regardless of the devices orientation when shook.

例如,如果我们上下摇动设备,屏幕向上,它进入 z,然后如果你旋转设备,使屏幕朝前并做同样的摇动,z 轴仍然显示加速度即使相对于设备,它是 Y 轴.

For example, if we shake the device up and down, with the screen up, it comes in the z, and if you then rotate the device so the screen is facing forward and do the same shakes the z axis still shows acceleration even though with respect to the device it is the Y axis.

我一直在尝试将加速度旋转回初始姿态,看起来有时有效,有时无效.我不相信轮换是正确的方法.我不确定它是否是合理的数学,或者有时只是随机看起来是正确的.

I've been trying to rotate the acceleration back to the initial attitude and it looks like it sometimes works and sometimes doesn't. I'm not convinced that rotation is the correct approach. I'm not sure if it is sound math or just randomly looks right sometimes.

extension CMQuaternion {
    var toSIMD: simd_quatd {
        return simd_quatd(ix: x, iy: y, iz: z, r: w)
    }
}

extension CMAcceleration {
    var toSIMD: SIMD3<Double> {
        return SIMD3<Double>(x,y,z)
    }
}

然后像这样进行计算:

var reference: simd_quatd?
motionManager.startDeviceMotionUpdates(using: .xArbitraryZVertical, to: .main) { deviceMotion, error in
    guard let deviceMotion = deviceMotion else { return }

    let userAcceleration = deviceMotion.userAcceleration.toSIMD
    let adjusted: SIMD3<Double>
    if let ref = reference {
// rotate the userAccel by the inverse of the reference times the current attitude. 
      adjusted = simd_act(simd_mul( simd_inverse(ref.quaternion.toSIMD),deviceMotion.attitude.quaternion.toSIMD), userAcceleration)
    } else {
      reference = deviceMotion.attitude.quaternion.toSIMD
      adjusted = userAcceleration
    }
...
}

最终目标是将向上/向下"分量放入 FFT 中,以尝试找出运动的频率.

The end goal is to put the "up/down" component into a FFT to try and find the frequency of the movements.

旋转加速度矢量是正确的方法吗?有没有更好的方法来实现与姿态无关的加速度?或者更好的是,无论方向如何,都可以找到设备运动的频率!

Is rotating the acceleration vector the right approach? Is there a better way to achieve a attitude independent acceleration? Or better yet, find frequency of device motion regardless of orientation!

推荐答案

我能够验证这确实是一种旋转"加速度数据的方法.

I was able to verify that this indeed is a way to "rotate" the acceleration data.

作为注释而不是使用第一帧作为参考,我对其进行了简化并使用了四元数

As a note instead of using the first frame as the reference I simplified it and use the quaternion

let referenceFrame = simd_inverse(simd_quatd(ix: 0,iy: 0,iz: 0,r: 1))

然后处理调整后的加速度我做:

then for processing the adjusted acceleration I do:

let userAcceleration = motion.userAcceleration.toSIMD

let adjusted = simd_act(simd_mul(referenceFrame, motion.attitude.quaternion.toSIMD), userAcceleration)

adjusted 的 Z 分量将在全局框架中向上/向下.

adjusted's Z component will then be up/down in the global frame.

我一直在实时执行此操作,没有任何问题.

I've been doing this in realtime without any issues.

这篇关于将加速度数据调整到全局参考框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆