El Guapo
El Guapo

Reputation: 5781

IOS Accelerometer/Gyroscope Question

I want to write an app that gives the degrees of position from some coordinate (bottom of the phone).

For example... If I'm holding the phone at a 45 degree angle, I want to display: 45 degrees on the screen. If the user holds the phone at 45 degrees and rotates the phone around an axis going from the ear piece to the home button, I want to display that angle (between 0-180degrees).

I've implemented the accelerometer and I get the x, y, z values, however, how do I convert them? I know they are in G's (1G, 0.9G, -0.5G on the respective axis), but what's the conversion? Am I even going on the correct track? Should I be using the gyroscope instead?

Thanks.

Upvotes: 2

Views: 3555

Answers (2)

user244343
user244343

Reputation:

This question has an example. You can use atan2(y, x) and convert from radians to degrees with * (180/M_PI).

For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it.

- Wikipedia article on atan2

Upvotes: 3

Kay
Kay

Reputation: 13146

If you can rely on gyroscope support I'd recommend to use it, because you can get the (Euler) angles directly without any calculations. See iOS - gyroscope sample and follow the links inside.

Don't use UIAccelerometer because it will be deprecated soon. The newer CoreMotion framework is always the better choice, even for old devices.

Upvotes: 0

Related Questions