开发者

how to count step using accelerometer in android

i need to count no of steps while walking. so that i am using accelerometer. in the above coding i get accelerometer sensor's x,y,z values. this is i have done so far. my problems is by the x,y,z how to count steps while walking? i get the following code from the link

http://pedometer.googlecode.com/svn/trunk/src/name/bagi/levente/pedometer/Pedometer.java

my code:

 import android.app.Activity;
 import android.content.Context;
 import android.os.Bund开发者_开发技巧le;
 import android.widget.TextView;
 import android.widget.Toast;

 public class Accelerometer extends Activity implements AccelerometerListener {

private static Context CONTEXT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    CONTEXT = this;
}

protected void onResume() {
    super.onResume();
    if (AccelerometerManager.isSupported()) {
        AccelerometerManager.startListening(this);
    }
}

protected void onDestroy() {
    super.onDestroy();
    if (AccelerometerManager.isListening()) {
        AccelerometerManager.stopListening();
    }

}

public static Context getContext() {
    return CONTEXT;
}

/**
 * onShake callback
 */
public void onShake(float force) {
    Toast.makeText(this, "Phone shaked : " + force, 1000).show();
}

/**
 * onAccelerationChanged callback
 */
public void onAccelerationChanged(float x, float y, float z) {
    ((TextView) findViewById(R.id.x)).setText(String.valueOf(x));
    ((TextView) findViewById(R.id.y)).setText(String.valueOf(y));
    ((TextView) findViewById(R.id.z)).setText(String.valueOf(z));
}

 }

please help me.


You will not find here a simple code to just count steps (it is just too complex). But there's info out there if you're interested:

  • There are good diagrams and step analyses here (in pdf).

  • You can find a more formal publication here: http://portal.acm.org/citation.cfm?id=1554235

  • And if you want to create a sensitive pedometer (proposed for the elderly), I suggest you to start from this paper: http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4575030


You can estimate the gravity force on the phone using the x,y,z values...

float g = (x * x + y * y + z * z) / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);

...here a value of 1 = normal (1g is normal)

The a crude pedometer can be built fairly easily by just counting how many peaks above a specified g value over a given sample period (eg 6 seconds and multiple by 10 for paces per minute)

Say for example record the time in ms that a g of >2 is recorded... then the peak will continue going up.... and come back down below 2.. probably to 0.5 or something.. then it'll go up again >2... at this point stop the clock.

...then you have a complete cycle timed!

To stabilise the result it's better to count a few cycles.


For step detection I use the derivative applied to the smoothed signal from accelerometer. When the derivative is greater than threshold value I can suggest that it was a step. May be it's not best practise but it's works for me :)

The following code was used in this app https://play.google.com/store/apps/details?id=com.tartakynov.robotnoise

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){
            return;
        }
        final float z = smooth(event.values[2]); // scalar kalman filter                               
        if (Math.abs(z - mLastZ) > LEG_THRSHOLD_AMPLITUDE)
        {
            mInactivityCount = 0;
            int currentActivity = (z > mLastZ) ? LEG_MOVEMENT_FORWARD : LEG_MOVEMENT_BACKWARD;                  
            if (currentActivity != mLastActivity){
                mLastActivity = currentActivity;
                notifyListeners(currentActivity);
            }                   
        } else {
            if (mInactivityCount > LEG_THRSHOLD_INACTIVITY) {
                if (mLastActivity != LEG_MOVEMENT_NONE){
                    mLastActivity = LEG_MOVEMENT_NONE;
                    notifyListeners(LEG_MOVEMENT_NONE);                                 
                }
            } else {
                mInactivityCount++;
            }
        }
        mLastZ = z;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜