Java – Should I use an accelerometer or gyroscope to measure the angle of the z-axis

Should I use an accelerometer or gyroscope to measure the angle of the z-axis… here is a solution to the problem.

Should I use an accelerometer or gyroscope to measure the angle of the z-axis

I’m writing an app in Android that needs to know the angle at which the rear camera is pointing relative to the horizon, not its last position. None of the definitions I’ve read about a compass and accelerometer says I can do this. Is it entirely possible to do this?

Solution

You should use an accelerometer.

The idea is as follows, in a still camera, the only force acting on the camera (seen by the accelerometer) is gravity. This should be able to give you which direction is “down”. Then what you need to do is calculate the angle between the direction the camera is facing (this is a constant depending on the direction of the accelerometer) and the direction of gravity.

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

By definition, the (

true) horizon is 90º downward (pi/2 radians), and you should subtract pi/2 from the result of the dot product, which should be between -pi/2 and +pi/2

The result of -x indicates that the camera is “facing down” and at an x angle to the horizon.
A result of +x indicates that the camera is “facing up” and at an angle of x to the horizon.

Related Problems and Solutions