Thorsten
Thorsten

Reputation: 146

Detect movement regardless device rotation

I search a way to detect the movement of the phone regardless how the user holds the phone.

Some examples:

The background is that I want to recognize if the user pulls the phone out of the pocket (like pulls 30-40cm up and also some centimeters horizontal).

I think therefor I have to get a combination out of the userAcceleration and the attitude. But I did not find the correct maths..

Upvotes: 12

Views: 418

Answers (2)

Chipmuenk
Chipmuenk

Reputation: 697

Not sure what you mean by "correct maths". But I would read out the accelerometer and sum up the absolute value for all 3 directions (x/y/z). This would give you a single number for the acceleration, independent of the orientation of the phone or the direction of the movement.

Even better would be to calculate the RMS acceleration, i.e. sum the squares of x/y/z acceleration and calculate the square root of the sum.

The obvious drawback of this method is that it gives you no information on the direction of the movement. You might add some threshold or filtering to distinguish a single linear movement from shaking (e.g. by measuring the time between two acceleration events).

Additionally, you could determine the change of altitude (this is independent of the phones orientation) although I doubt whether a difference of 30 ... 40 cm can be measured precisely enough.

Upvotes: 1

Siriss
Siriss

Reputation: 3767

You might be able to do this if your app is in the foreground. You could detect which landscape the phone thinks it is in, using UIDeviceOrientationLandscapeLeft and UIDeviceOrientationLandscapeRight and the UIDeviceOrientationPortraitUpsideDown/UIDeviceOrientationPortrait, and from there adjust how you are calculating movement.

It would probably shift when the phone is picked up. That change, with the accelerometer might be able to give you a picture of how the phone is moving. This would require a ton of testing though. I would setup your practice app and get a large amount of data on screen orientation changes, and how the accelerometer behaves with each change and look for trends.

Upvotes: 0

Related Questions