Android sudden upstream detection

I am trying to detect upward movement on my Android device, but find it quite complicated. I wrote the following code and it works for all sudden movements (i.e. Fast acceleration) in all directions. However, I would like this to work only for a sudden upward movement. Any help is appreciated.

public void onSensorChanged(SensorEvent event) { // alpha is calculated as t / (t + dT) // with t, the low-pass filter time-constant // and dT, the event delivery rate final float alpha = 0.8f; gravity = new float[3]; linear_acceleration = new float[3]; linear_acceleration_old = new float[3]; gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; linear_acceleration[0] = event.values[0] - gravity[0]; linear_acceleration[1] = event.values[1] - gravity[1]; linear_acceleration[2] = event.values[2] - gravity[2]; System.out.println(linear_acceleration[0] + " " + linear_acceleration[1]+" " +linear_acceleration[2]); //detects swift movements, but I want to detect only upward ones if((Math.abs(linear_acceleration[1] - linear_acceleration_old[1]) > 10.0f && Math.abs(linear_acceleration[2] - linear_acceleration_old[2]) > 5.0f) || isFirstEvent) { if(!isFirstEvent) //do something linear_acceleration_old[0] = linear_acceleration[0]; linear_acceleration_old[1] = linear_acceleration[1]; linear_acceleration_old[2] = linear_acceleration[2]; isFirstEvent= false; } } 
+6
source share
2 answers

I'm not quite sure what you meant by "up", however:

  • so that the phone remains on the surface of the table and lifts it (make it above the table), there will be a change in the Z axis.

  • holding the phone vertically as if you were talking, and then pull it up (lift it higher), change the Y axis.

If you are interested in detecting these movements, you should change your code to calculate deltas only on the corresponding axis.

+3
source

You should not only work with the accelerometer sensors x, y, z, but also consider orientation sensors: SensorManager.getOrientation

This way you can provide an upstream direction.

0
source

Source: https://habr.com/ru/post/900893/


All Articles