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

偶尔善良 提交于 2019-12-12 12:29:51

问题


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 ?


回答1:


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.




回答2:


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]) 



回答3:


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.



来源:https://stackoverflow.com/questions/17051154/should-i-use-accelerometer-or-gyroscope-to-measure-angle-of-the-z-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!