【问题标题】:How to count speed based on steps如何根据步数计算速度
【发布时间】:2017-04-16 19:12:57
【问题描述】:

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

获取步数的接收者:

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

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

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

【问题讨论】:

  • 如果您使用的是 FineLocation,那么使用带有 google maps api 的 getSpeed 应该不会太难。我使用 api 制作了一个速度计,效果很好,但是当用户的移动速度不超过 1 英里/小时时会有点延迟,有时会有点偏离。
  • 这个 0.0254 是每一步的固定距离吗?什么是 3.6?
  • 另外,你是如何收集时间戳的?是用加速度计吗?
  • 只有在调用 updateAllUI 时才会显示或更新您的速度,即每当您的接收器变量调用其 onReceive 方法时。所以我的问题是,它什么时候被调用?它是在每一步之后调用的(这可以解释为什么当一个人停止时它不会更新为 0)还是有一个设定的时间间隔(在这种情况下,您需要一个“超时”窗口来更新速度到 0)?附带说明:为什么要更新变量?你不应该处理手头的异常吗?
  • 我是否遗漏了什么,或者这根本不可能?要测量速度,您需要将行驶距离除以到达那里所需的时间(即speed = distance / time)。您正在使用的StepCounterService 似乎只给您步骤之间的时间,而不是步骤的距离;那你怎么可能计算速度呢?

标签: java android


【解决方案1】:

由于您只计算步数并且希望仅计算所采取的步数的速度,我假设您不想使用 GPS 来计算速度,或者如果 GPS 不存在,您希望提供备用速度计数器。

你可以通过这样做来启动一个新线程

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

您需要为所采取的每一步都执行此操作

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 
}

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

【讨论】:

    【解决方案2】:

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

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

    您可以使用 Android Activity Recognition Api 查看用户当前的活动。使用

    ActivityRecognition.getMostProbableActivity()
    

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

    你也可以得到信心DetectedActivity.getConfidence()来确定。

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

    【讨论】:

      【解决方案3】:

      来自传感器的每条信息都存在大量错误,甚至数据丢失。

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

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

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2012-01-08
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多