Read x y z coordinates of android phone using accelerometer

后端 未结 2 1792
既然无缘
既然无缘 2020-12-13 07:49

I am going to develop Android application which needs to read x,y,z coordinates of phone on 3D space.

I would like to write a simple code and test o

相关标签:
2条回答
  • 2020-12-13 08:22

    To get position from acceleration you need to integrate it twice.

    Integrating acceleration gives you velocity and integrating the velocity gives you the position.

    Keep in mind that integrating noise creates drift and integrating drift creates A LOT of drift, the android sensors tend to generate quite a lot of noise.

    On my Galaxy S3 I have been able to get the drift in position down to 0.02 m in 5 seconds using Google's Linear Accelerometer composite sensor.

    I am not sure if you can use the linear accelerometer sensor on gingerbread. If you can't you will have to remove the gravity before integrating.

    If you haven't already, read everything here http://developer.android.com/guide/topics/sensors/sensors_motion.html

    A great talk about the motion sensors in android

    http://www.youtube.com/watch?v=C7JQ7Rpwn2k

    Code:

    static final float NS2S = 1.0f / 1000000000.0f;
    float[] last_values = null;
    float[] velocity = null;
    float[] position = null;
    long last_timestamp = 0;
    
    @Override
    public void onSensorChanged(SensorEvent event) {
        if(last_values != null){
            float dt = (event.timestamp - last_timestamp) * NS2S;
    
            for(int index = 0; index < 3;++index){
                velocity[index] += (event.values[index] + last_values[index])/2 * dt;
                position[index] += velocity[index] * dt;
            }
        }
        else{
            last_values = new float[3];
            velocity = new float[3];
            position = new float[3];
            velocity[0] = velocity[1] = velocity[2] = 0f;
            position[0] = position[1] = position[2] = 0f;
        }
        System.arraycopy(event.values, 0, last_values, 0, 3);
        last_timestamp = event.timestamp;
    }
    

    Now you have the position in 3d space, keep in mind it assumes that the phone is stationary when it starts sampling.

    If you don't remove gravity it will soon be very far away.

    This doesn't filter the data at all and will generate a lot of drift.

    0 讨论(0)
  • 2020-12-13 08:44

    Read this tutorial.

    brief summary of the above given tutorial ::

    first get an instance of SensorManager and Sensor.
    Inside onCreate() ::

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    

    after this, override onSensorChanged(SensorEvent event) and use event.values[] to get the co-ordinates.

    @Override
    public void onSensorChanged(SensorEvent event) {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
    }
    
    0 讨论(0)
提交回复
热议问题