27

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.Bundle;
 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.

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • so what is the problem here? what's not working? – corroded May 26 '11 at 04:20
  • i do not know how to calculate steps counting with accelerometer's x,y,z values. that is my problem. – M.A.Murali May 26 '11 at 04:23
  • 2
    Well, you're probably going to have to start by doing some tests and see what the data looks like when someone takes a step, then you should have an idea of how to determine when a step was taken and can count them accordingly. Might be an easier way, but that's my best guess... –  May 26 '11 at 04:40
  • 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?. – M.A.Murali May 26 '11 at 04:49
  • @nil : please check my commands or edited questions. could you explain your command please. – M.A.Murali May 26 '11 at 04:54
  • 1
    @nil that was exactly my first idea, but looking at some analyses out there, I don't really think it is that simple... – Aleadam May 26 '11 at 04:58
  • Like I said, just take your phone out and record what happens with the data the accelerometer provides while you're walking (and running). Test it on slopes, stairs, normal paths, etc. and you should have a fairly good set of data to look at and determine how to see if someone has taken a step. (Nope: Aleadam's answer is better than anything I previously suggested.) –  May 26 '11 at 05:01
  • @Aleadam Yeah, after checking out your answer I think that's definitely true. –  May 26 '11 at 05:05
  • @murali_ma : Do u finally came to know how to count the steps??? If yes please help me I am in same problem right now.Thank you. – Kushal Shah Jun 28 '12 at 11:26
  • No i had moved to other project. i did not find. – M.A.Murali Jun 29 '12 at 04:38
  • @KushalShah Have you completed this one? which one is best option among all listed belows in all answer? – Jeegar Patel Apr 15 '14 at 05:35
  • 1
    Android 4.4 (KitKat) has a new API for counting steps. Here's a quick tutorial: http://blog.bawa.com/2013/11/create-your-own-simple-pedometer.html – Skylar Ittner May 12 '14 at 04:49
  • Looks like it is available only on nexus 5. Confirmed not working on moto G. – Krzysztof Bociurko Sep 20 '14 at 09:49
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Sufian Sep 03 '15 at 07:32

3 Answers3

15

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:

Aleadam
  • 40,203
  • 9
  • 86
  • 108
8

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.

timemirror
  • 586
  • 4
  • 11
4

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;
    }
tartakynov
  • 2,768
  • 3
  • 26
  • 23