使用 CoreMotion/Accelerometer 以编程方式检测 iPhone 是否掉落 [英] Detect programmatically if iPhone is dropped using CoreMotion/Accelerometer

查看:29
本文介绍了使用 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.

Q1:是否有实现任何运动/手势检测的现有开源代码?

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

如果没有

Q2:现在我想检测 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:

然而你必须自己测试,什么意思"总加速度对应在您的情况下,大约为地球加速度"和一段时间"."

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

加速度值实际上以 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. 将加速度值四舍五入到小数点后 3 位,并使用我正在使用的加速度范围.
  2. 可以检查是否在满足自由落体条件后,总加速度值突然下降.
  3. 您是否认为我引用的加速度值有点嘈杂?

推荐答案

Q2:

检查大的负值并不能告诉您手机是否掉落.

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 <×<1.1x0.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 是否掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆