java - How to know if Android device is Flat on table -
i'm using accelometer sensor detect whether device flat on table or not. weird thing when make phone flat or rotate on it's side value between 90 , 100! shouldn't correct! missing something? here code:
float[] values = event.values; // movement float x = values[0]; float y = values[1]; float z = values[2]; float norm_of_g =(float) math.sqrt(x * x + y * y + z * z); // normalize accelerometer vector x = (x / norm_of_g); y = (y / norm_of_g); z = (z / norm_of_g); int inclination = (int) math.round(math.todegrees(math.acos(y))); log.i("tag","incline is:"+inclination); if (inclination < 25 || inclination > 155) { // device flat toast.maketext(this,"device flat - beep!",toast.length_short); }
edit: i'm using code : how measure tilt of phone in xy plane using accelerometer in android
you're using y-axis instead of z-axis used in the answer linked.
the value of acos near-zero when argument near 1 (or near 180 degrees when near negative one), seen in picture:
as such, inclination near 0 (or 180) degrees when y axis normalized 1 or negative one, eg when parallel gravity, (thus, device "standing up").
if there's no other error, switching from:
int inclination = (int) math.round(math.todegrees(math.acos(y)));
to
int inclination = (int) math.round(math.todegrees(math.acos(z)));
should it.
Comments
Post a Comment