user1798620
user1798620

Reputation: 19

Calculation of distance with linear acceleration sensor

I'm developing application for indoor navigation, but I can't interpret the values. Here is my code snippet:

public class MainActivity extends Activity implements SensorEventListener {

private SensorManager sensorManager;

TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object
Button button;

float[] linear_acceleration = new float[3];
float[] gravity = new float[3];
float[] distanceInMeter = new float[3];
float[] speed = new float[3];
long timeStamp;
float dT;

private boolean firstTimeOfMeasurement = false;

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    xCoor=(TextView)findViewById(R.id.xcoor); 
    yCoor=(TextView)findViewById(R.id.ycoor); 
    zCoor=(TextView)findViewById(R.id.zcoor); 
    button=(Button)findViewById(R.id.button1);

    sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            sensorManager.registerListener(MainActivity.this,
                    sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                    SensorManager.SENSOR_DELAY_FASTEST);

            firstTimeOfMeasurement = false;
        }
    });
}

public void onSensorChanged(SensorEvent event){

    // check sensor type
    if(event.sensor.getType()==Sensor.TYPE_LINEAR_ACCELERATION){

        if(!firstTimeOfMeasurement ){
            firstTimeOfMeasurement = true;
            timeStamp = event.timestamp;
            
            speed[0] = speed[1] = speed[2] = 0;
            distanceInMeter[0] = distanceInMeter[1] = distanceInMeter[2] = 0;
            
        }
        else{
            dT= (event.timestamp - timeStamp) / 1000000000.0f;
            timeStamp = event.timestamp;

            calculateDistance(event.values, dT);
            
            xCoor.setText("X: " + Float.toString(distanceInMeter[0]));
            yCoor.setText("Y: " + Float.toString(distanceInMeter[1]));
            zCoor.setText("Z: " + Float.toString(distanceInMeter[2]));
            
            Log.d("","X: " + Float.toString(distanceInMeter[0]) +  " Y: " + Float.toString(distanceInMeter[1]) + " Z : " + Float.toString(distanceInMeter[2]));
        }
    }
}

public void calculateDistance (float[] acceleration, float deltaTime) {
    for (int i = 0; i < acceleration.length; i++) {
        speed[i] = acceleration[i] * deltaTime;
        distanceInMeter[i] += speed[i] * deltaTime + acceleration[i] * deltaTime * deltaTime / 2;
    }
}

... and here the Log (device was moved ca. 0.5 meters in the X-direction.):

X: 2.516964E-5 Y: 2.3743667E-5 Z : -5.294226E-5 
X: 4.897119E-5 Y: 2.4477049E-5 Z : -1.0725691E-4 
X: 6.026584E-5 Y: 4.4123855E-5 Z : -1.4899505E-4 
X: 3.9997853E-5 Y: -1.7013517E-6 Z : -1.8953103E-4 
X: 4.6437595E-5 Y: -5.077744E-6 Z : -2.621123E-4 
X: 5.7528538E-5 Y: 5.1304473E-6 Z : -2.8128378E-4 
X: 4.4061002E-5 Y: 5.7082625E-6 Z : -3.4537681E-4 
X: 3.2294975E-5 Y: -1.6293983E-5 Z : -4.103487E-4 
X: 5.8478785E-5 Y: -1.4691614E-6 Z : -4.9171754E-4 
X: 6.55654E-5 Y: -2.2436536E-5 Z : -5.5965903E-4 
X: 4.2109532E-5 Y: -5.2801708E-5 Z : -6.242801E-4 
X: 3.8397735E-5 Y: -8.7437686E-5 Z : -6.771843E-4 
X: -2.4613353E-5 Y: -1.6809099E-4 Z : -7.590898E-4 
X: -4.7245518E-5 Y: -2.0863091E-4 Z : -8.195847E-4 
X: -6.6750574E-5 Y: -2.5927206E-4 Z : -8.85209E-4 
X: -6.952324E-5 Y: -2.9220866E-4 Z : -9.949753E-4 
X: -8.74085E-5 Y: -3.4179245E-4 Z : -0.0010735805 
X: -1.0780102E-4 Y: -3.5630423E-4 Z : -0.0011248525
X: -1.19364566E-4 Y: -3.3947633E-4 Z : -0.001229486 
X: -1.549891E-4 Y: -3.7859E-4 Z : -0.0013151368 
X: -1.5717075E-4 Y: -3.702779E-4 Z : -0.0013620423 
X: -2.5051413E-6 Y: -1.9888686E-4 Z : -0.0013941963 
X: 1.4368152E-4 Y: -3.9539344E-5 Z : -0.0014529589 
X: 2.6982307E-4 Y: 2.944292E-5 Z : -0.0015093404 
X: 4.395193E-4 Y: 7.502221E-5 Z : -0.0015618263 
X: 6.21904E-4 Y: 7.436979E-5 Z : -0.0015917926 
X: 7.617797E-4 Y: -5.3465046E-6 Z : -0.0016443293 
X: 8.4774144E-4 Y: -1.3927222E-4 Z : -0.0016940705 
X: 9.120384E-4 Y: -2.1821483E-4 Z : -0.0017769502 
X: 0.0010219973 Y: -2.7528123E-4 Z : -0.0018363354 
X: 0.0011270456 Y: -3.19711E-4 Z : -0.0019322607 
X: 0.0012737722 Y: -3.4506532E-4 Z : -0.0019969852 
X: 0.001502906 Y: -3.835355E-4 Z : -0.0020537905 
X: 0.0017388286 Y: -4.398609E-4 Z : -0.0021189034 
X: 0.0019086768 Y: -4.70718E-4 Z : -0.0021404363 
X: 0.0021076156 Y: -5.0305086E-4 Z : -0.002185045 
X: 0.0022671057 Y: -5.1701383E-4 Z : -0.0022573534 
X: 0.0024057168 Y: -5.4342125E-4 Z : -0.002310839 
X: 0.002512934 Y: -5.4679764E-4 Z : -0.0023926103 
X: 0.0025857347 Y: -5.900372E-4 Z : -0.0024241738 
X: 0.0026625344 Y: -6.842513E-4 Z : -0.0024786198 
X: 0.0027399021 Y: -7.289082E-4 Z : -0.0025227368 
X: 0.0028299019 Y: -7.829082E-4 Z : -0.0025947813 
X: 0.0029214222 Y: -8.3661743E-4 Z : -0.0026276957 
X: 0.0030301774 Y: -8.8301615E-4 Z : -0.0027011314 
X: 0.0030617723 Y: -9.42785E-4 Z : -0.0027936357 
X: 0.0030394243 Y: -9.79083E-4 Z : -0.00285914 
X: 0.0030165722 Y: -9.802011E-4 Z : -0.0029144369 
X: 0.0029567073 Y: -9.6269825E-4 Z : -0.0029774965 
X: 0.00297403 Y: -9.315846E-4 Z : -0.0030192512 
X: 0.0030169394 Y: -9.6226286E-4 Z : -0.0030642622 
X: 0.0031092423 Y: -0.0010050826 Z : -0.003118441 
X: 0.0031988935 Y: -0.0011072198 Z : -0.003198576 
X: 0.0033174676 Y: -0.0012394118 Z : -0.0032808 
X: 0.0034213979 Y: -0.0013103384 Z : -0.0033766583 
X: 0.003445055 Y: -0.0013735284 Z : -0.0034781513 
X: 0.0034835928 Y: -0.0014324035 Z : -0.0035485225 
X: 0.0034975903 Y: -0.0014486146 Z : -0.0036555608 
X: 0.0034793443 Y: -0.0014780854 Z : -0.0037472823 
X: 0.0034312923 Y: -0.0015043139 Z : -0.0037952673 
X: 0.0034234857 Y: -0.001599117 Z : -0.0038744798 
X: 0.003435113 Y: -0.0016648113 Z : -0.003960097 

Upvotes: 1

Views: 3371

Answers (2)

msysmilu
msysmilu

Reputation: 2024

With respect to all practicalities - it's not reasonably possible. Why?

Upvotes: 0

Ali
Ali

Reputation: 58461

It won't work. These sensors are not accurate enough.

In the above answer I give alternatives what you actually could do for indoor localization.

Upvotes: 2

Related Questions