iDevice态度:稳定四元数
我试图使用iDevice的态度self.motionManager.deviceMotion.attitude.quaternion
设备搁在桌子上,但价值观是不一样的。 如何稳定这些值而不会导致不好的旋转? 我正在使用设备姿态旋转相机节点。
(x = -0.0055437298906573507, y = -0.0078092851375721247, z = -0.041180405121897398, w = 0.999105828407855) (x = -0.0061666945840810842, y = -0.0067849414785486747, z = -0.041464744435584115, w = 0.99909789881469635) (x = -0.0057767614213563457, y = -0.0075097232630314727, z = -0.041803806548186787, w = 0.99908091506248087) (x = -0.0054897030127900098, y = -0.0077124534854605253, z = -0.04219926058901851, w = 0.99906436410664467) (x = -0.0052714642886002782, y = -0.0078368692177714049, z = -0.042675299607953819, w = 0.99904435034111316) (x = -0.0050140321661910547, y = -0.0078121993123853265, z = -0.043041870709832147, w = 0.99903014288315972) (x = -0.0050055928591862253, y = -0.0077335374133680208, z = -0.043505056503552082, w = 0.99901073392523531) (x = -0.0049666831093899792, y = -0.007717140215596976, z = -0.043913562364765651, w = 0.99899318158145256) (x = -0.0047768022686966614, y = -0.0078160798819143576, z = -0.044274861496884942, w = 0.99897739098280313) (x = -0.0047310514463435671, y = -0.0078007790545880336, z = -0.044670164461142137, w = 0.9989601300379195) (x = -0.004618244779613233, y = -0.0078641769042355775, z = -0.045003270721875412, w = 0.99894520978936451) (x = -0.0049526705779270597, y = -0.0076127708990477733, z = -0.045411652395276572, w = 0.99892707371465494) (x = -0.0053219441444451315, y = -0.0074858231291817018, z = -0.045723089011719641, w = 0.99891192729581401) (x = -0.0056345717154138147, y = -0.0073613453524927486, z = -0.046005287981812061, w = 0.99889818083421522) (x = -0.0056064974434034201, y = -0.0074105820596984404, z = -0.046130222648849362, w = 0.99889221291304886) (x = -0.0053487406143112462, y = -0.0076910190187535112, z = -0.046331716108766401, w = 0.99888218088156866) self.motionManager = [[CMMotionManager alloc] init]; self.motionManager.deviceMotionUpdateInterval = 1.0/20.0; [self.motionManager startDeviceMotionUpdates];
你正在寻找的是一个低通滤波器 – 一种将传感器input中的高频信号(噪声,随机抖动)与低频信号(故意的用户运动)分开的方法。
实时filter的典型公式如下所示:
newValue = (sampledValue * factor) + (lastValue * (1 - factor))
也就是说,从采样值 (来自传感器的原始input)和lastValue (在上一个时间步中计算的新值)的线性插值中获得newValue (您要使用的滤波后的input);确保将其初始化为在第一次计算上是明智的),同时控制融合的程度。 您可以调整因子来控制通过您的filter得到多less信号。
你也许可以把这个公式分别应用到四元数的每个x , y , z和w分量上,并得到一个模糊的可通过的结果。 但是a)四次写相同的代码并不是那么有趣,b)四元数有一些特殊的math运算,会导致这个方法随着时间的推移而崩溃。
相反,您可以将CMQuaternion
为GLKQuaternion
并在一次调用GLKQuaternionSlerp
进行GLKQuaternionSlerp
。 (它为你介意特殊的math。)
filteredAttitude = GLKQuaternionSlerp(lastAttitude, sampledAttitude, factor)
然后从你的GLKQuaternion
创build一个SCNQuaternion
,你可以设置节点的方向。