Detect Movement Accurately using Accelerometer in Android

僤鯓⒐⒋嵵緔 提交于 2019-12-02 21:11:37

I think the next stage in the development of your app is to look at values of acceleration that are produced in a spreadsheet. I use Excel for this, but any tool that can produce graphs will do. So alter onSensorChanged() to something like

public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];

    float mAccelCurrent = FloatMath.sqrt(x*x + y*y + z*z);
    float mAccel = mAccel * 0.9f + mAccelCurrent * 0.1f;
    Log.d("onSensorChanged",System.currentTimeMillis()+","+mAccelCurrent +","+mAccel);

}

and then you can capture the currentTime, mAccelCurrent and mAccel into the Android logging mechanism. Alternatively, create your own text file, write the values there, and open the file in a tool that can produce graphs. From the graphs, you can then decide what values to use for your trigger.

Stochastically

Two suggestions, and one thought:

  1. For consistent behaviour, you should look at the length of the event.values vector, i.e. Math.sqrt(x*x+y*y+z*z), rather than the individual values. Mathematically, it's Math.sqrt(x*x+y*y+z*z) that's independent of your co-ordinate system, i.e. how the device is oriented relative to the ground etc, whereas the individual numbers x, y, z aren't.
  2. Your current app looks for changes in acceleration. Instead, I think you should just look for high acceleration, i.e. large values of Math.sqrt(x*x+y*y+z*z).
  3. I recently wrote this answer about using accelerometers for a pedometer, and I think you might find it interesting.

To find linear acceleration in a particular direction you can use this code too:

    @Override
public void onSensorChanged(SensorEvent event) {
// alpha is calculated as t / (t + dT)
// with t, the low-pass filter's time-constant
// and dT, the event delivery rate

final float alpha = 0.8f;

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];
}  

after calculating the linear acceleration you can simply use an 'if' statement to check whether you jerk was powerful or not! [Higher value= more powerful jerk]

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