1

I need to plot a acceleration change on a chart. I've managed to get the value from the accelerometer

        Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);

And now I just need to plot the acceleration change to signal for example if the user fall down his phone. What I've done :

@Override
public void onSensorChanged(SensorEvent event) {
    if (started) {
        double x = event.values[0];
        double y = event.values[1];
        double z = event.values[2];
        long timestamp = System.currentTimeMillis();
        double sec = (timestamp-timestampStart)/1000.0;
        double e = Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z,2));
        eSeries.add(sec, e);
        mChart.repaint();    
    }

}

The problem is that the value returned by "e" is always close to 10 when the phone is not moving, this should be 0 when their is no change... Do you know what is wrong ?

KingOfBabu
  • 409
  • 3
  • 21
  • I'm so dump -_- You're right thanks ! You can post this on a comment and i will put it resolved :) – KingOfBabu Jan 12 '16 at 03:35
  • Mmmh just another question, now i have some negativ value when doind this : double e = Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z,2))-GRAVITY; – KingOfBabu Jan 12 '16 at 03:45
  • yes. if you, for example, exert a force upward on your phone, the measure you'll have will be less than gravity. If you then remove gravity, then the total is negative. I am not sure you can simply remove the value of gravity like that, but may be with the orientation sensor you can compute the vector that is due to gravity and remove it from x, y and z before computing the norm of the acceleration vector. – njzk2 Jan 12 '16 at 03:58
  • here is an example of why you can't just remove g from the final value: assuming your phone sits still in a rocket that is experiencing 1g of lateral acceleration. you'll read x = 0, y = 10, z = 10. The norm is 14.14. If you now remove g, you'll get 4.14. But if you ignore g, you'd be reading y = 10 and z = 0, and the norm would be 10. – njzk2 Jan 12 '16 at 04:01
  • 1
    now, I don't think you should try to remove or ignore g. If the norm of the acceleration is exactly g, there is a good chance the device is still. If it is 0, it is in free fall. If it is significatively more than g, it is crashing into something. – njzk2 Jan 12 '16 at 04:02

0 Answers0