android-检测向下加速度,特别是电梯 [英] android - detect downward acceleration, specifically an elevator

查看:142
本文介绍了android-检测向下加速度,特别是电梯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检测到手机向地面加速的情况(可能意味着在这里也必须使用重力传感器)。

I want to be able to detect a situation where the phone has an acceleration towards the ground (probably means that the Gravity sensor has to be used here also).

我已经在Android文档中阅读了很多有关此主题的内容,关于高通和低通滤波器以及其他文章,现在我所拥有的是一个代码示例去除重力后在X,Y和Z轴上的加速度:

I have read a lot about this topic in the Android docs, about High and Low pass filters and other posts, and right now what I have is a code sample that gets the acceleration in the X, Y and Z axis after stripping the gravity:

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        final float alpha = (float) 0.8;

        gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

        linear_acceleration[0] = event.values[0] - gravity[0];
        linear_acceleration[1] = event.values[1] - gravity[1];
        linear_acceleration[2] = event.values[2] - gravity[2];  
}

因此, linear_acceleration 应该是没有重力的情况下在X,Y,Z轴上的加速度。

So, the linear_acceleration is supposedly the acceleration in the X, Y, Z axis without the gravity.

这一切都很好,但是问题显然在于这取决于用户的握持方式例如,电话在电梯中-如果他将其放平并平行于地面- Z轴将发生变化,如果他将其笔直抬起- Y轴会发生变化,等等。

This is all nice, but the problem obviously is that is depends on how the user holds the phone, for example, in the elevator - if he holds it flat, parallel to the ground - the Z axis will change, if he holds it up straight - the Y axis will change , etc.

因此,例如,如果用户斜着拿手机,则加速度为考虑到重力方向的位置,需要在不同的轴之间进行划分,并进行某种数学运算,以计算该方向上的实际加速度。

So, for example, if the user holds the phone diagonally, the acceleration will be "divided" between the different axis, and some sort of math work, considering where the gravity direction is, will be needed to calculate the actual acceleration in that direction.

正确我如果我错了?

是否有可靠的方法来检测向下(朝向地球)的加速度?也许使用其他传感器,例如陀螺仪?

Is there a reliable way to detect the downward (towards the earth) acceleration? maybe using other sensors like Gyroscope?

BTW-关于 TYPE_LINEAR_ACCELERATION 类型,我读过answer ,说它实际上不是很准确。

BTW - about the TYPE_LINEAR_ACCELERATION type, I read this answer, saying that its actually not very accurate.

推荐答案

使用一些基本物理学。加速度是向量。向量v的大小始终等于(v.v)^。5或点积的平方根。或更简单地讲(x ^ 2 + y ^ 2 + z ^ 2)^。5。这将告诉您加速度的大小,但不会告诉您加速度是朝向地球还是远离地球。

Use some basic physics. Acceleration is a vector. The magnitude of a vector v is always equal to (v.v)^.5, or the square root of the dot product. Or in simpler terms (x^2+y^2+z^2)^.5. That will tell you the amount of acceleration, but not if its towards or away from the earth.

如果您需要知道它是走向地球还是远离地球,可以将其与SensorManager.getOrientation中的数据结合起来。不过,您可能需要在进入电梯之前执行此操作-方向代码将重力用作其输入之一,因此,如果您尝试在电梯中使用重力,则可能会搞砸它。您需要对其进行测试。

If you need to know if its going towards or away from earth- you can combine that with data from SensorManager.getOrientation. You may need to do that before they enter the elevator though- the orientation code uses gravity as one of its inputs, so it may be screwed up if you try to use it in an elevator. You'd need to test it out.

如果需要将其分解为基于地球x,y和z轴的加速度,则为简单几何。从定向结果中获取角度,并使用trig属性转换轴。如果您不知道这些公式,则需要仔细阅读一下,否则即使我告诉您,他们也会弄错它们。

If you need to break it down to acceleration in terms of earth x, y, and z axes- simple geometry. Take the angle from the orientation result, and use trig properties to convert axes. If you don't know the formulas you need to read up on trig a bit or you'll get them wrong even if I tell them to you.

这篇关于android-检测向下加速度,特别是电梯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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