How to count speed based on steps

Akshat :

I have a step counter app in which I am running a service which counts steps taken and then sends a broadcast to the fragment which is then updated on the fragment. The step counting is working fine but I want to calculate speed based on the steps. Here is what I am trying right now.

The receiver to get step count:

receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int steps = intent.getIntExtra(StepCounterService.STEP_INCREMENT_KEY, 0);
        if (firstStepTime.equals("0")) {
            firstStepTime = intent.getStringExtra(StepCounterService.TIME_STAMP_KEY);
        } else if (secondStepTime.equals("0")) {
            secondStepTime = intent.getStringExtra(StepCounterService.TIME_STAMP_KEY);
        } else {
            firstStepTime = secondStepTime;
            secondStepTime = intent.getStringExtra(StepCounterService.TIME_STAMP_KEY);
        }

        updateAllUI(steps);
    }
};

So what I am doing is as soon as I start getting steps, I see if the variable firstStepTime is empty. If it is, I save the time in firstStepTime variable. The in the next step I see if secondStepTime is empty, and if it is, I save that time in secondStepTime variable. Now for the next steps both these are updated.

public void updateAllUI(int numberOfSteps) {

    if (!(firstStepTime.equals("0")) && !(secondStepTime.equals("0"))) {
        try {
            Calendar c = Calendar.getInstance();
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
            timeDifference = timeFormat.parse(secondStepTime).getTime() - timeFormat.parse(firstStepTime).getTime();

            speed = (float) ((0.0254 / (timeDifference * 0.001)) * 3.6);
        } catch (Exception e) {
            timeDifference = 0;
            speed = 0;
        }

        textview.settext(speed +"Km/h);
    }
}

So in this I just check if both are not empty, I take the values and calculate the difference in times. The problem here is sometimes it doesn't count speed properly. And a bigger problem is if the user stops, the speed remains constant and doesn't drop to zero.

Is there any better way to do the speed calculation?

Ankit Arora :

As you're only counting steps and would like to calculate speed with steps taken only i am assuming that you would not like to use GPS for counting speed or you would like to provide backup speed counter if GPS is not there.

You can start a new thread by doing

    int interval=1*1000; //specifying interval to check steps
    Handler.postDelayed(mySpeedChecker(),1000);

You'll need to do this for every step taken

public void mySpeedChecker(){
// assuming steps taken stored in a global variable
// and there is one more variable which is updated by this function 
// to store number of steps before as global variable to calculate number of steps in time interval. 
// this should check if another step has been taken in the interval to check
// if stepsTaken>0 then speed is not zero and you'll know how much steps are taken
// if zero steps are taken then speed is zero 
// also it will tell you number of steps taken in last second 
// so you can use this to calculate and update speed 
// increase the interval for high accuracy but lower frequency of updates 
}

Also, it's a good idea to calculate user stride length when GPS is available so, whenever user is using GPS the step length can be calculated by finding out the distance covered/number of steps taken with the help of GPS, this will help your system calculate accurately the distance covered by user with the help of number of steps taken.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=434173&siteId=1