如何根据步骤计算速度

时间:2017-04-16 19:12:58

标签: java android

我有一个计步器应用程序,我在其中运行一个服务,计算所采取的步骤,然后向片段发送广播,然后在片段上更新。步数正常,但我想根据步骤计算速度。这就是我现在正在尝试的。

获取步数的接收者:

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

所以我正在做的是,一旦我开始步骤,我就会看到变量firstStepTime是否为空。如果是,我将时间保存在firstStepTime变量中。在下一步中,我看到secondStepTime是否为空,如果是,我将该时间保存在secondStepTime变量中。 现在,对于接下来的步骤,这些都会更新。

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

所以在这里我只检查两者是否都是空的,我取值并计算时间差。这里的问题有时它并没有正确计算速度。更大的问题是如果用户停止,速度保持不变并且不会降至零。

有没有更好的方法来进行速度计算?

3 个答案:

答案 0 :(得分:10)

由于你只是计算步数而且想用步数计算速度,我假设你不想用GPS来计算速度,或者你想提供备用速度计数器,如果没有GPS。

您可以通过

开始新的主题
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 
}

您需要为每个步骤执行此操作

sequence (op:ops) = (:) <$> op <*> sequence ops

此外,当GPS可用时计算用户步幅是个好主意,因此,当用户使用GPS时,可以通过找出所覆盖的距离/借助GPS获取的步数来计算步长,这将是借助所采取的步骤,帮助您的系统准确计算用户所覆盖的距离。

答案 1 :(得分:4)

理想情况下,计算速度的常用方法是使用Location.getSpeed()或对Accelerometer值进行一些分析。但我假设你想根据计算的步数得到它。

解决其中一个问题:“如果用户停止,速度保持不变,不会降至零。”

您可以使用Android活动识别功能Api查看用户当前活动。使用

beer_bottles = 99

while beer_bottles >= 2 do
  plural = "bottles"

  singular = "bottle"

  plural_or_singular = beer_bottles > 1 ? plural : singular

  puts "#{beer_bottles} #{plural_or_singular} of beer on the wall, #{beer_bottles} #{plural_or_singular} of beer."

  beer_bottles -= 1

  puts "BOTTLE COUNT: #{beer_bottles}"

  puts "Take one down and pass it around, #{beer_bottles} #{plural_or_singular} of beer on the wall."
end

如果DetectedActivity类型为STILL,则可以将速度设置为零。

您也可以获得信心DetectedActivity.getConfidence()

对于另一个问题,当你说它没有正确计算速度时,你能详细说明这个问题吗?

答案 2 :(得分:0)

来自传感器的每个信息都有一定的误差,甚至是数据松动。

您应该将收到的信息存储在队列或数据库中,包括时间戳。

您会定期选择数据并进行分析,包括过滤以获得更准确的信息。

希望它有所帮助。

相关问题