ios - 将加速度数据调整为全局引用系

标签 ios accelerometer core-motion accelerate-framework cmattitude

使用 Core Motion 时,加速度似乎总是相对于设备返回。这意味着如果您向上摇动设备屏幕,它将位于 +/-Z(取决于初始引用值)如果您随后将设备转向侧面并上下摇动,与之前相同,加速度将位于 +/-Z/-X 或 Y。

我想将此设备特定的加速度转换回“全局”帧。这意味着无论设备在摇动时的方向如何,设备的相同物理运动都会导致加速度数据沿同一轴返回。

例如,如果我们上下摇动设备,屏幕朝上,它会出现在 z 轴上,然后如果您旋转设备使屏幕朝前并做同样的摇动,z 轴仍然显示加速度即使相对于设备它是 Y 轴。

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

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 中,以尝试找到运动的频率。

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

最佳答案

我能够验证这确实是一种“旋转”加速数据的方法。

请注意,我没有使用第一帧作为引用,而是对其进行了简化并使用了四元数

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

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

let userAcceleration = motion.userAcceleration.toSIMD

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

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

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

关于ios - 将加速度数据调整为全局引用系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61250601/

相关文章:

ios - 覆盖 canBecomeFirstResponder 导致 viewController 崩溃

屏幕关闭时非工作传感器的 Android 解决方法

ios - 在 3D 旋转中转换加速度计

gyroscope - 无法使用 watchOS 5 获取核心运动更新 : "[Gyro] Manually set gyro-interrupt-calibration to 800"

swift - Apple Watch 屏幕关闭时有没有办法记录 Action ?

ios - 在相机 UIImagePickerController 中收到内存警告

ios - 在 UIStackView 控件中调整 UIView 高度

objective-c - 创建一个包含多个 NSMutableDictionary 对象的 NSMutableArray

java - Android 传感器意外崩溃

iphone - 使用 Core Motion 构建计步器