检测 iPhone 旋转?
我想创建一个应用程序可以检测用户旋转 iPhone 设备时的旋转次数。目前,我正在使用 Compass API 来获取角度并尝试多种方法来检测旋转。以下是我尝试过的解决方案列表: 1/ 在整轮上创建2个角度陷阱(整轮上的一块),以检测我们从指南针获得的角度是否通过它们。 2/ 计算罗盘更新时间之间的所有角度距离(在 updateHeading 函数中)。让我们尝试将总角除以 360 =>我们可以得到旋转数
问题是:当手机旋转太快时,指南针无法跟上手机的速度,它会返回给我们最新时间的角度(而不是像真实旋转那样连续)。
我们还尝试使用加速度计来检测旋转。但是,当您在平面上旋转手机时,此方法不起作用。
如果您对这个问题有任何解决方案或经验,请帮助我。
非常感谢。
I want to create an application could detect the number of spin when user rotates the iPhone device. Currently, I am using the Compass API to get the angle and try many ways to detect spin. Below is the list of solutions that I've tried:
1/ Create 2 angle traps (piece on the full round) on the full round to detect whether the angle we get from compass passed them or not.
2/ Sum all angle distance between times that the compass is updated (in updateHeading function). Let try to divide the sum angle to 360 => we could get the spin number
The problem is: when the phone is rotated too fast, the compass cannot catch up with the speed of the phone, and it returns to us the angle with latest time (not continuously as in the real rotation).
We also try to use accelerometer to detect spin. However, this way cannot work when you rotate the phone on a flat plane.
If you have any solution or experience on this issue, please help me.
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iPhone4 包含 MEMS 陀螺罗盘,因此这是最直接的路线。
正如您所注意到的,磁力计的响应缓慢。可以通过使用预期算法来减少这种情况,该算法利用缓慢性对当前的真正方向进行有根据的猜测。
首先,您需要确定传感器的实际性能。为此,您需要以多个旋转速度中的每一个以精确的速率旋转它,并记录罗盘的行为。旋转平台应具有读取瞬时位置的方法。
在较慢的速度下,您会看到不同程度的固定滞后。随着速度的增加,滞后会增加,直到接近 180 度,此时指南针会突然翻转。在较高的速度下,您将看到的只是翻转,尽管当以相同的值重复翻转时,它可能看起来不会翻转。在其中一些较高速度下,罗盘可能会出现向后旋转,与旋转方向相反。
获得旋转台可能很麻烦,并且确保它不会影响局部磁场(使指南针变得无用)是一个挑战。理想的桌子将由铝制成,如果您需要使用钢制桌子(最常见),则需要将手机安装在非磁性平台上,以使其尽可能远离钢。
当地的机械车间将是一个很好的起点:数控机床能够轻松完成所需的工作。
获得罗盘性能数据后,您将需要构建观察到的读数与实际方向和旋转速率的模型。反转模型并将其应用于读数以获得实际读数的猜测。
一个简单的算法实现是保留读数的历史记录,并保留顺序读数之间差异的列表。由于我们知道罗盘存在滞后,因此当差值非零时,我们就会知道当前值由于滞后而存在一定程度的不准确性。
下一步是创建“校正”读数列表,其中先前实际值的已知滞后用于生成更新值,该更新值用于创建添加到“校正”读数中的最后一个值的更新值列表,并存储为最新值。
当累积校正(实际值与校正列表中的最新值之间的差异超过 360 度时,这意味着我们基本上不知道指南针指向哪里。希望不会达到该点,因为大多数旋转运动应该 。
但是,由于您的目标只是计算旋转,因此您将偏离完整的旋转,直到累积误差达到相当高的值,我不确定这个值是多少 因为它取决于实际的罗盘滞后和实际的旋转速率,但如果您只关心少量的旋转(5 左右),您应该能够获得可用的结果。
The iPhone4 contains a MEMS gyrocompass, so that's the most direct route.
As you've noticed, the magnetometer has sluggish response. This can be reduced by using an anticipatory algorithm that uses the sluggishness to make an educated guess about what the current direction really is.
First, you need to determine the actual performance of the sensor. To do this, you need to rotate it at a precise rate at each of several rotational speeds, and record the compass behavior. The rotational platform should have a way to read the instantaneous position.
At slower speeds, you will see a varying degree of fixed lag. As the speed increases, the lag will grow until it approaches 180 degrees, at which point the compass will suddenly flip. At higher speeds, all you will see is flipping, though it may appear to not flip when the flips repeat at the same value. At some of these higher speeds, the compass may appear to rotate backwards, opposite to the direction of rotation.
Getting a rotational table can be a hassle, and ensuring it doesn't affect the local magnetic field (making the compass useless) is a challenge. The ideal table will be made of aluminum, and if you need to use a steel table (most common), you will need to mount the phone on a non-magnetic platform to get it as far away from the steel as possible.
A local machine shop will be a good place to start: CNC machines are easily capable of doing what is needed.
Once you get the compass performance data, you will need to build a model of the observed readings vs. the actual orientation and rotational rate. Invert the model and apply it to the readings to obtain a guess of the actual readings.
A simple algorithm implementation will be to keep a history of the readings, and keep a list of the difference between sequential readings. Since we know there is compass lag, when a difference value is non-zero, we will know the current value has some degree of inaccuracy due to lag.
The next step is to create a list of 'corrected' readings, where the know lag of the prior actual values is used to generate an updated value that is used to create an updated value that is added to the last value in the 'corrected' list, and is stored as the newest value.
When the cumulative correction (the difference between the latest values in the actual and corrected list exceed 360 degrees, that means we basically don't know where the compass is pointing. Hopefully, that point won't be reached, since most rotational motion should generally be for a fairly short duration.
However, since your goal is only to count rotations, you will be off by less than a full rotation until the accumulated error reaches a substantially higher value. I'm not sure what this value will be, since it depends on both the actual compass lag and the actual rate of rotation. But if you care only about a small number of rotations (5 or so), you should be able to obtain usable results.
您可以使用加速度来确定手机旋转的速度,并用它来填充空白,直到手机停止,此时您可以再次查询指南针。
You could use the velocity of the acceleration to determine how fast the phone is spinning and use that to fill in the blanks until the phone has stopped, at which point you could query the compass again.
如果您使用的是 iPhone 4,问题已解决,您可以使用 Core Motion 获取旋转数据。
对于早期的设备,我认为一个有趣的方法是尝试使用 UIAccelerometer 具有非常精细的报告间隔。您也许能够从与旋转平面成直角的运动中检测到一些合理的模式。
If you're using an iPhone 4, the problem has been solved and you can use Core Motion to get rotational data.
For earlier devices, I think an interesting approach would be to try to detect wobbling as the device rotates, using UIAccelerometer on a very fine reporting interval. You might be able to get some reasonable patterns detected from the motion at right angles to the plane of rotation.