旋转加速度计输出至新的“中性”位置适用于 iOS(和其他)。
我这里的功能完全按照我想要的方式工作。我的问题是,它似乎过于复杂,是否有更快/更好的方法来进行我在下面显示的pitchAngleRadians 计算?
显示代码...(以及两张图片)
第一张图片是如何从 iPad 自然地得出 atan2 计算,“pitchAngleRadians = atan2(accel[0], accel2);" (加速度计通常输出的内容)
我想要的是将“零”移动到新的角度,在第二张图像中,我将其移动了 -135 度作为示例。 (代码中的所有内容都是弧度)因此例如本例中的变量“pitchNeutralAngle”是相当于(-135)度的弧度
此代码产生的正是我想要的变量“pitchAngleRadians”,这是第二个图像,但似乎太长了,而且这段代码处于紧密的运行循环中,所以如果我能让这段代码更干净/更快,它会加速。
我经历了一个中间步骤,将度数“范围”在0到360之间,这样跨越180度的边界就不会扰乱计算,但是我仍然必须将其恢复到0到180的形式,所以这就是下一个计算是什么。
我已经稍微测试了这段代码,它似乎确实有效。
//------------------------------------------------------------------------------------
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
accel[0] = (acceleration.x * kFilteringFactor) + accel[0] * (1.0 - kFilteringFactor);
accel[1] = (acceleration.y * kFilteringFactor) + accel[1] * (1.0 - kFilteringFactor);
accel[2] = (acceleration.z * kFilteringFactor) + accel[2] * (1.0 - kFilteringFactor);
pitchAngleRadians = (atan2(accel[0], accel[2]) - pitchNeutralAngle);
pitchAngleRadians = pitchAngleRadians - floorf(pitchAngleRadians/6.28318f)*6.28318f;
if (pitchAngleRadians > 3.14159) pitchAngleRadians = -6.28318f + pitchAngleRadians;
//float a = atan2(accel[0], accel[2]);
//NSLog(@"a = %f", a);
}
what i have here works exactly how i want it. My question is that it seems overly complex, is there a faster/better way of doing the pitchAngleRadians calculations i am showing below?
the code is shown... (along with two images)
the first image is how the atan2 calculation comes out of the iPad naturally, "pitchAngleRadians = atan2(accel[0], accel2);" (what the accelerometer puts out normally)
what i want is to shift "Zero" to a new degree angle, in the second image i've shifted it a -135 degrees as an example. (everything in code is radians) so for instance the variable "pitchNeutralAngle" in this example is the radian equivalent of (-135) degrees
this code produces exactly what i want for the variable "pitchAngleRadians", which is the second image, but seems too long, and this code is in a tight run loop, so it would speed up if i can get this cleaner/faster.
i go through an intermediate step of "ranging" the degrees between 0 and 360, so that crossing the boarder of 180 degrees does not mess up the calculation, however i still have to get it back to the 0 to 180 form, so that is what the next calculation is.
i've tested this code a little bit, and it does appear to work.
//------------------------------------------------------------------------------------
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
accel[0] = (acceleration.x * kFilteringFactor) + accel[0] * (1.0 - kFilteringFactor);
accel[1] = (acceleration.y * kFilteringFactor) + accel[1] * (1.0 - kFilteringFactor);
accel[2] = (acceleration.z * kFilteringFactor) + accel[2] * (1.0 - kFilteringFactor);
pitchAngleRadians = (atan2(accel[0], accel[2]) - pitchNeutralAngle);
pitchAngleRadians = pitchAngleRadians - floorf(pitchAngleRadians/6.28318f)*6.28318f;
if (pitchAngleRadians > 3.14159) pitchAngleRadians = -6.28318f + pitchAngleRadians;
//float a = atan2(accel[0], accel[2]);
//NSLog(@"a = %f", a);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否绑定了旧的 3.x 设备,还是可以依赖 iOS 4.x?一般来说,“旧的”UIAccelerometerDelegate 很快就会被弃用,因为它被新的 Core Motion 框架(s.1)所取代。
为什么accelerometer:didAccelerate:在IOS5中被弃用?)
接下来,你在说话关于具有 IMO 陀螺仪的 iPad(或者只是 iPad 2?)。如果您可以信赖拥有该设备的设备,那么您就可以使用 Core Motion DeviceMotion,它提供了随时可以使用的欧拉角,没有任何问题(请记住万向节锁)。看
简单的 iPhone 运动检测尤其是:
您可以查看 WWDC2010 示例代码:WWDC 2010 示例代码
如果没有,有机会说服您或您的业务合作伙伴吗?好吧,你还在那里 :-) 嗯,atan 和 atan2 有一些不连续性,你确定它适用于所有小 x 和 y 值的组合,即,如果设备躺在桌子上,没有或只有小运动,但一些传感器噪音?
关于性能:是的,atan 相当昂贵,因此您可以使用 FastMath.h< 等替代方案进行检查/a> (它是法语,但不用担心,请点击C++ en un simple fichier header FastMath.h content中的链接。
Are you bound to old 3.x devices or can you rely on iOS 4.x? In general the "old" UIAccelerometerDelegate will be deprecated soon because it is replaced by new Core Motion framework (s.
Why is accelerometer:didAccelerate: deprecated in IOS5?)
Next, you are talking about iPad which has IMO a gyroscope (or is it iPad 2 only?). If you can rely on devices having one, you are done with Core Motion DeviceMotion which provides Euler angles ready to use without any problems (OK bear in mind Gimbal lock). See
Simple iPhone motion detect especially:
You can check out WWDC2010 sample code: WWDC 2010 Sample Code
If not, any chance to convince you or your business partners? OK, you are still there :-) hmmh, atan and atan2 have some discontinuities, are you sure that it works out for all combinations of small x and y values i.e. if the device is lying on the table without or with just small motion but some sensor noise?
Regarding perfomance: Yes atan is quite expensive so you can check out using alternatives like FastMath.h (it's in French but no worries follow the link at C++ en un simple fichier header FastMath.h contenant.