Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count speed based on steps

Tags:

java

android

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?

like image 791
Akshat Avatar asked Apr 16 '17 19:04

Akshat


People also ask

What is the formula in calculating speed?

The formula for speed is speed = distance ÷ time. To work out what the units are for speed, you need to know the units for distance and time.

How do you calculate steps?

Divide the number of feet in your measured distance by the number of steps you took from the first mark to the second. Distance in feet/number of steps = step length. For example, if it took you 16 steps to cover 20 feet, your step length would be 1.25 feet (15 inches).

How do you calculate speed per hour?

Speed is distance divided by the time taken. For example, a car travels 30 kilometres in 2 hours. Its speed is 30 ÷ 2 = 15km/hr.


2 Answers

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.

like image 196
Ankit Arora Avatar answered Oct 19 '22 11:10

Ankit Arora


The usual way to calculate speed ideally would be using Location.getSpeed(), or some analysis on Accelerometer values. but I'm assuming you want to get it based on steps counted.

To solve one of the problems: " if the user stops, the speed remains constant and doesn't drop to zero."

You can use the Android Activity Recognition Api to see the users current activity. Use

ActivityRecognition.getMostProbableActivity()

If the DetectedActivity is type STILL, you can set your speed to be zero.

You can also get the confidence DetectedActivity.getConfidence() to be sure.

For the other problem when you said it doesn't count speed properly, could you elaborate more on that question?

like image 4
sai Avatar answered Oct 19 '22 11:10

sai