Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect programmatically if iPhone is dropped using CoreMotion/Accelerometer

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: Is there any existing opensource code which has achieved any movement/gesture detection?

If not

Q2: For now I want to detect if the iPhone is dropped.

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?

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."

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.

Here's a sample output:

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. round off the values of acceleration to 3 decimal places and play with the acceleration range I am using.
  2. May be check if, after the freefall condition is met, total acceleration value suddenly goes down.
  3. Do you think the acceleration values I have quoted are a bit noisy?
like image 318
Asymptote Avatar asked Jan 21 '13 19:01

Asymptote


People also ask

How is acceleration measured on an iOS device?

All iOS devices have a three-axis accelerometer, which delivers acceleration values in each of the three axes shown in Figure 1. The values reported by the accelerometers are measured in increments of the gravitational acceleration, with the value 1.0 representing an acceleration of 9.8 meters per second (per second) in the given direction.

How does the accelerometer know if the phone is facing up?

The accelerometer measures this spring compression and uses that to determine the acceleration of the phone. With that, it will know if it is facing up or down. It also can estimate how far you move and use this along with the camera to find out where real world objects are, using ARKit.

How do I capture all the incoming accelerometer data?

When you want to capture all of the incoming accelerometer data, perhaps so you can analyze it for movement patterns, use the startAccelerometerUpdates (to:withHandler:) method of CMMotionManager. This method pushes each new set of accelerometer values to your app by executing your handler block on the specified queue.

How does the app display real-time accelerometer data?

The app displays a real-time graph of accelerometer data. The user configures the update frequency for the accelerometers using a slider, the changing of which results in a call to the startUpdatesWithSliderValue: method shown in the example.


1 Answers

To Q2:

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

  • First, the user could just move the phone with a rapid gesture, this would also result in a large (maybe negative) value.
  • Secondly, the phone could fall in another direction than you imagine, and therefore, the acceleration could be positive, although the phone is moving towards the ground.

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.


The physics behind this:

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:

Acceleration in y axis

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.

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:

Total acceleration

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.

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.

like image 196
Misch Avatar answered Oct 03 '22 01:10

Misch