确定用户是否正在移动。 [英] Determine whether user is moving or not.

查看:58
本文介绍了确定用户是否正在移动。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在开发一个Android应用程序,在用户移动时启动计时器。计时器正在使用加速度计开始计划。然而,问题是当用户停止时让计时器停止。这应该是在用户完全停顿时。我很难用它,因为如果你移动得太慢,计时器停止,或者有时停止,我甚至不知道为什么。



这是我的代码,这不按计划运行:



Hi All,

I am developing an android app that starts a timer when the user is moving. The timer is starting ass planned, using the accelerometer. The problem, however is to get the timer to stop when the user stops. This should be when the user is at a complete standstill. I am having difficulty with it because if you move too slow the timer stops, or sometimes it stops and I just don't even know why.

Here is my code, which is not working as planned:

@Override
    public void onSensorChanged(SensorEvent event) {
        // get the change of the x,y,z values of the accelerometer
        deltaX = Math.abs(lastX - event.values[0]);
        deltaY = Math.abs(lastY - event.values[1]);
        deltaZ = Math.abs(lastZ - event.values[2]);

        //Zero out gravity on the accelerometer.
        gDeltaX = (float)((0.9 * gDeltaX) + (0.1 * deltaX));
        gDeltaY = (float)((0.9 * gDeltaY) + (0.1 * deltaY));
        gDeltaZ = (float)((0.9 * gDeltaZ) + (0.1 * deltaZ));

        deltaX = deltaX - gDeltaX;
        deltaY = deltaY - gDeltaY;
        deltaZ = deltaZ - gDeltaZ;

        //Make sure process starts only once
        if (flag == 0) {
            if ((deltaX > 10) || (deltaY > 10) || (deltaZ > 10)) {
                //Start the timer
                startTime = SystemClock.uptimeMillis();
                timeHandler.postDelayed(updateTimerMethod, 0);
                flag = 1;
            }
        } else if (flag == 1) {
            if ((deltaX < 1) && (deltaY < 1) && (deltaZ < 1)) {
                //Stop the timer.
                timeSwap += timeInMillies;
                timeHandler.removeCallbacks(updateTimerMethod);
                flag = 2;
            }
        }
    }





如果有任何其他方法可以停止计时器,比没有移动时使用加速度计,我都乐于接受建议。我在想加速度计可以是初始化器,但停止时要使用什么?



If there is any alternate way of stopping the timer, rather than using the accelerometer when it has no movement, I am all open to suggestions. I was thinking the the accelerometer can be the initiallizer, but what to use when stopping??

推荐答案

当你询问移动时,问题是:移动相对于什么? :-)



显然,用加速度计来判断速度是不可能的;基本物理原理禁止它:不同参考系统中以均匀速度运动的所有现象都以相同的方式运行,因此您无法从系统内部检测到它们之间的任何差异。换句话说,你会在不同坐标和速度的系统中检测到相同的加速度,主要是不可能彼此分辨。



我明白你在做另一件事:真正检测加速度,而不是速度。这足以检测一些动作开始的时刻,如果之前的状态几乎完全休息的话。但当一个人停下来会发生什么?通常,它与开始移动没有什么不同:设备只是遇到不同的加速度。这还不够吗?让我们考虑一下你的用户永远不会改变设备的方向(一个有问题的假设本身),并且初始加速度为正,然后,为了完成停止,你需要等量的加速度和负号。但什么是等值?很明显,不是相同的加速度曲线,而是一些加速度与相同的第一积分运动,速度。请注意,您必须在所有动作中整合动作,而不会有任何停顿。由于许多原因,它无法准确地完成。在所有情况下,积分值可能会累积错误。



您可能会做一些非常不同的事情,而不是试图找出速度。例如,获取某段时间内平均加速度水平的统计数据。换句话说,检测加速度计可检测的频率和振幅的振动。假设一些低级别的振动如无动作。



-SA
When you ask about "moving", the question would be: moving relative to what? :-)

Apparently, it is impossible to tell the speed by an accelerometer; it is prohibited by the fundamental physical principles: all phenomena in different reference system moving at uniform speeds go in the same way, so you cannot detect any difference between them from inside the system. In other words, you would detect the same acceleration in systems at different coordinates and speeds, it is principally impossible to tell one from another.

I understand that you are doing a different thing: really detecting acceleration, not speed. This would be enough to detect the moment of time when some motion has started, if the previous state was pretty much complete rest. But what happens when one stops? Generally, it's no different from starting to move: the device just experience different acceleration. This is not enough? Let's consider that your user never change the orientation of the device (a questionable assumption itself), and initial acceleration is positive, then, to come to complete stop, you would need equivalent amount of acceleration with negative sign. But what is "equivalent amount"? Quite apparently, not identical acceleration profile, but some acceleration with the same first integral of motion, speed. Note that you would have to integrate the motion during all the motion, without any stops. It cannot be done precisely, by many reasons. In all cases, integrated values may accumulate the errors.

Instead of trying to find out the speed, you may do something very different. For example, get a statistic of the average acceleration level during some period of time. In other words, detect the vibrations at the frequencies and amplitudes detectable by an accelerometer. Assume some low level of such vibrations as "no motion".

—SA


这篇关于确定用户是否正在移动。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆