本文介绍了使用CoreMotion/Accelerometer以编程方式检测iPhone是否掉落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一段代码,其中必须使用上述iOS 4.3中的加速计和陀螺仪来检测不同的移动手势.

So I am writing a piece of code where I have to detect different movement gestures using Accelerometer and gyroscope in iOS 4.3 above.

问题1:是否存在任何实现移动/手势检测的现有开源代码?

Q1: Is there any existing opensource code which has achieved any movement/gesture detection?

如果不是

第二季度:现在,我想检测iPhone是否掉落.

我到目前为止所取得的成就:CoreMotion API提供 userAcceleration (用户加速度),该强度(afaik)是用户给予设备的加速度或设备在某个方向(x,y或z)上的加速度,而无需考虑重力因此,我可以做的是:存储先前的5-6个加速参数值,并可以检查其中的任何一个达到较大的负值,这基本上代表了突然的减速.

What I have achieved so far:CoreMotion API gives userAcceleration, which (afaik) is the acceleration that the user is giving to the device or the acceleration of device in some direction (x, y or z) with out considering the gravity so what I can do is: store, let's say, previous 5-6 values of acceleration parameters and can check where any of them hits large negative value, which basically represents the sudden deceleration.

但是这种解决方案不是非常理想,我想我也需要以某种方式首先检测设备的自由落体/向下运动.知道如何解决这个问题吗?

But this solution is not very optimal, I think I need to somehow detect the freefall/downwards motion of the device too first. Any idea how to approach this problem?

更新:感谢Misch分享您的方法.我根本没有考虑全面加速.我完全按照你说的做:

UPDATE:Thanks Misch for sharing your approach. I was not at all thinking about total acceleration. I did exactly what you said:

加速度值实际上是G值,因此我测试了总加速度"值,范围为0.9-1.1.我检查了一段时间,最初将updateInterval设置为1/20.0时检查了四个连续值.现在,我已经稍微放松了连续性的条件.

The acceleration value is actually in G's so I tested the "total acceleration" values with in range of 0.9 - 1.1. And I checked them over some time, initially I checked four consecutive values when updateInterval is set to 1/20.0. For now I have relaxed the condition of consecutiveness a little.

以下是示例输出:

加速度= 0.090868
加速度= 0.074473
加速度= 0.159797
加速度= 1.157513
加速度= 1.224588
加速度= 1.036272
加速度= 0.914698
加速度= 0.904093
加速度= 0.941516
加速度= 0.046362
加速度= 0.045109
加速度= 0.060045

Acceleration = 0.090868
Acceleration = 0.074473
Acceleration = 0.159797
Acceleration = 1.157513
Acceleration = 1.224588
Acceleration = 1.036272
Acceleration = 0.914698
Acceleration = 0.904093
Acceleration = 0.941516
Acceleration = 0.046362
Acceleration = 0.045109
Acceleration = 0.060045

我认为,我仍然必须继续测试和调整值.如果您有任何优化的想法,请分享,我知道为了帮助您需要查看自由落体加速度值的许多示例.现在,我正在考虑:

I think, I still have to keep on testing and adjusting values. If you have any optimization in mind kindly do share, I know in order to help you'd need to see the many samples of freefall acceleration values. For now I am thinking to:

  1. 将加速度值四舍五入到小数点后三位,并按照我使用的加速度范围进行操作.
  2. 可以检查是否满足自由落体条件后总加速度值突然下降.
  3. 您认为我引用的加速度值有点吵吗?

推荐答案

第二季度:

检查较大的负值不会告诉您手机是否掉线.

Checking for large negative values does not tell you whether the phone is being dropped.

  • 首先,用户可以快速移动手机,这也将导致较大的值(可能为负值).
  • 第二,手机可能会以您想象不到的方向跌落,因此,尽管手机正在向地面移动,但加速度可能为正.

您可以只计算总加速度(a = sqrt(ax^2 + ay^2 + az^2))并检查该总加速度是否大致对应于地球加速度(9.81).如果加速度在一段时间内与地面加速度相对应,则手机处于跌落状态.

You could just calculate the total acceleration (a = sqrt(ax^2 + ay^2 + az^2)) and check whether this total acceleration corresponds approximately to earth acceleration (9.81). The phone is falling if the acceleration corresponds to earth acceleration for some time.

但是,您必须进行自我测试,在您的情况下,这意味着总加速度大约对应于地球加速度"和"一段时间".

You would however have to test yourself, what means "total acceleration corresponds approximately to earth acceleration" and "for some time" in your case.

其背后的物理原理:

让我们假设您以某种方式放下手机,即手机的y轴显示为向上.然后x和z加速度将为0,y加速度将如下所示:

Let's assume you drop your phone in a way, that the y axis of the phone shows upwards. Then the x and z accelerations will be 0 and the y acceleration will be like this:

开始时加速度将为0,然后在释放手机时将达到-9.81.然后它将撞到地面,您会在较小的加速度峰值中看到该加速度,然后加速度再次为零.

The acceleration will be 0 in the beginning, will then reach -9.81 the moment, you release your phone. Then it will hit the ground, which you see in the small acceleration peak, and then the acceleration is zero again.

但是,您不能仅使用y方向的加速度,因为您的手机可能会跌落到不同的角度.

However you can't use only the acceleration in y direction, because your phone may fall in a different angle.

因此,您必须注意手机的总加速度:

So you have to watch the total acceleration of your phone:

在这里,您不会再看到负加速度了.但是,这里手机的朝向不再重要,因为自由落体始终以9.81的加速度为特征.

Here you don't see a negative acceleration anymore. However, here it doesn't matter anymore in which direction your phone is facing, as a free fall will always be characterized by an acceleration of 9.81.

要进行
1.为什么要将加速度值四舍五入到小数点后三位?如果您测试0.9 < x < 1.1,则x0.914698还是0.915都没关系.
2.如果有人放下电话然后又接听电话,怎么办,因此总加速度不一定必须再次降低.此外,手机撞击地板时应该有较大的加速度值(突然减速).可能原因之一是您的值太短,落在两次连续的测量之间,这可能是您在值中看不到这一点的原因.但是,这可以测量,因此不要以为自由落体后立即加速度会再次降低.

To your edits:
1. Why would you want to round off the values of acceleration to 3 decimal places? If you test 0.9 < x < 1.1, it doesn't matter if x is 0.914698 or 0.915.
2. What if someone drops the phone but then catches it again, so the total acceleration does not necessarily have to go down again. Additionally, there should be a large acceleration value (a sudden deceleration) the moment the phone hits the floor. Maybe the reason one does not see this in your values is that it is so short, that it falls between two consecutive measurements. However this could be measured, so don't suppose that immediately after the free fall, the acceleration should decrease again.

这篇关于使用CoreMotion/Accelerometer以编程方式检测iPhone是否掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:54