android - detect downward acceleration, specifically an elevator

点点圈 提交于 2019-12-04 19:14:00

Use some basic physics. Acceleration is a vector. The magnitude of a vector v is always equal to (v.v)^.5, or the square root of the dot product. Or in simpler terms (x^2+y^2+z^2)^.5. That will tell you the amount of acceleration, but not if its towards or away from the earth.

If you need to know if its going towards or away from earth- you can combine that with data from SensorManager.getOrientation. You may need to do that before they enter the elevator though- the orientation code uses gravity as one of its inputs, so it may be screwed up if you try to use it in an elevator. You'd need to test it out.

If you need to break it down to acceleration in terms of earth x, y, and z axes- simple geometry. Take the angle from the orientation result, and use trig properties to convert axes. If you don't know the formulas you need to read up on trig a bit or you'll get them wrong even if I tell them to you.

Lachlan

I also wanted to just be able to measure vertical movement. This is how I did it and it worked for me. First time posting on this site and I have no idea how to format correctly.

Use two different android Sensors: Type_linear_Acceleration and Type_Gravity

Linear acceleration will give you acceleration in the X, Y and Z axis of the phone, and Gravity will do the same, but just for gravity. You know that the sum of the gravity values should = 9.8, but this will be split between X, Y and Z coordinates depending on the phone orientation.

I wont go into the math of it too much, but the following will give you vertical acceleration without gravity. If you want to understand it a bit more run through some values as if the phone was held vertically, then horizontally, it works even if the phone is oblique.

vertical acceleration = (LinearAccelX * GravityX / 9.8)+ (LinearAccelY * GravityY / 9.8)+ (LinearAccelZ * GravityZ / 9.8).

See code below (irrelevant parts removed):

{public class MainActivity extends AppCompatActivity implements SensorEventListener {

SensorManager sm;
Sensor linearaccelerometer;
Sensor gravity;
double Yaccel;
double Xaccel;
double Zaccel;
double gravityY;
double gravityX;
double gravityZ;
double verticalAccel;

    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    linearaccelerometer = sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    gravity = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);

    sm.registerListener(this, linearaccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    sm.registerListener(this, gravity, SensorManager.SENSOR_DELAY_NORMAL);
}

public void onSensorChanged(SensorEvent event) {

    Sensor sensor = event.sensor;

    if (sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
        Xaccel = (double) event.values[0];
        Yaccel = (double) event.values[1];
        Zaccel = (double) event.values[2];
    }

    if (sensor.getType() == Sensor.TYPE_GRAVITY) {
        gravityX = (double) event.values[0];
        gravityY = (double) event.values[1];
        gravityZ = (double) event.values[2];
    }

    verticalAccel = (Xaccel * gravityX / 9.8) + (Yaccel * gravityY / 9.8) +  (Zaccel *gravityZ /9.8);

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