我正在使用 CoreMotion 在应用中获取设备运动测量结果。我可以获得加速度 x y z、横滚、俯仰、偏航、陀螺仪数据等。
但是我想要测量的是震动,即设备移动了多少,并设置设备在任何方向上移动较大量时的阈值。我发现我得到的测量结果非常敏感,我不知道如何正确使用它。
Note: I know about detecting the shake gesture and that is not what I'm asking - I'm asking for the correct way to measure the amount of shake and display it.
我使用的 DeviceMotion 代码示例:
if let deviceMotion = motionManager.deviceMotion {
let quat = deviceMotion.attitude.quaternion;
var qRoll = radiansToDegrees(radians: atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
var qPitch = radiansToDegrees(radians: atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
var qYaw = radiansToDegrees(radians: asin(2*quat.x*quat.y + 2*quat.w*quat.z));
let orientation = deviceMotion.attitude
var roll = radiansToDegrees(radians: orientation.roll)
var pitch = radiansToDegrees(radians: orientation.pitch)
var yaw = radiansToDegrees(radians: orientation.yaw)
}
并使用加速度计:
if motionManager.isAccelerometerAvailable {
motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: { accelerometerData, error in
if let acceleration = accelerometerData?.acceleration {
print("ACCELEROMETER X: " + acceleration.x.description)
print("ACCELEROMETER Y: " + acceleration.y.description)
print("ACCELEROMETER Z: " + acceleration.z.description)
})
}
使用 CoreMotion 的测量值来测量设备晃动程度的正确方法是什么?
最佳答案
这取决于您的具体用例,但如果您想确定震动量,请计算运动增量。确定给定时间段内设备运动属性之一的最大值和最小值,并使用该值作为“抖动”的近似值。
例如,您可以测量设备在 5 秒内的偏航
。跟踪最小值和最大值,并计算它们之间的差值。摇动越多,值越大。
这又取决于您的具体用例,但您也可以结合设备运动属性及其运动增量。如果您知道运动随着时间的推移是一致的(就像机器来回摇晃),您可以尝试找到局部最小/最大值并在一段时间内对它们进行平均。
关于ios - 如何用 Swift 测量 iOS 设备的震动量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46594523/