FutureSci
FutureSci

Reputation: 1750

Should i use Accelerometer or Gyroscope to measure angle of the z-axis

I am writing an An app in Android that needs to know the angle the rear camera is pointing with respect to the horizon and not with respect to it's last position. All of the definitions i have read for the compass and accelerometer don't say that i can do this. Is it at all possible to do this ?

Upvotes: 3

Views: 1651

Answers (3)

user829755
user829755

Reputation: 1588

if the camera is known to be static, then gravity could be used and this must be possible with an app since there's spirit level apps that do this. Cedric Garcias answer and Hoan Nguyens answer above explain this in more detail.

if on the other hand the camera might be moving, then I think you have to track all the movements (using the gyroscope) and integrate them up from a point in time where the camera was fixed until the current time. that's probably quite challenging.

Upvotes: 0

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

What you want to calculate is the angle between the surface of the screen and the surface of the earth (locally flat). This can be calculate using TYPE_GRAVITY or TYPE_ACCELEROMETER (less accurate). The angle is just the angle between the normal to the surface of the earth and the normal to the screen (z-value)

angle = (float) Math.acos(normalize z value);

where

normalize z value = event.values[2] / Math.sqrt(event.values[0] * event.values[0] + event.values[1] * event.values[1] + event.values[2] * event.values[2]) 

Upvotes: 2

Cedric Garcia
Cedric Garcia

Reputation: 31

You should use the Accelerometer.

The idea is the following, in a static camera, the only force (which is seen by the accelerometer) acting on the camera is gravity. This should be able to give you which direction is "down". What you would then need to do, is calculate the angle between the direction the camera is facing, (which is a constant, depending on which way your accelerometer is oriented) and the direction of gravity.

The dot product (http://en.wikipedia.org/wiki/Dot_product) of the two vectors should give you the cosine of the vector (if the two vectors are unit vectors), then using arccos you should be able to get the angle in radians.

Since by definition, (true) horizon is 90º (pi/2 in radians) from down, you should subtract pi/2 from the result of the dot product, the result should be between -pi/2 and +pi/2.

A result of -x means the camera is facing "down" and making an angle of x with the horizon. A result of +x means the camera is facing "up" and making an angle of x with the horizon.

Upvotes: 3

Related Questions