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

查看:35
本文介绍了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 axes, and some sort of math work, considering where the gravity direction is, will be needed to calculate the actual acceleration in that direction.

如果我错了,请纠正我?

Correct me if I am wrong?

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

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

顺便说一句 - 关于 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 轴的加速度 - 简单几何.从方向结果中获取角度,并使用三角属性转换轴.如果您不知道公式,则需要稍微阅读一下三角函数,否则即使我告诉您它们也会出错.

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天全站免登陆