机器人:在加速度计传感器的z值的范围是在不同的设备不同 [英] Android: the range of z-value in the accelerometer sensor are different on different devices

查看:139
本文介绍了机器人:在加速度计传感器的z值的范围是在不同的设备不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果设备朝上来检测。 (不倾斜,但平朝上地)。结果
在面临了一些设备,Z值9〜10之间的返回值。 (大多数设备)结果
然而,在Nexus 7上,对于朝上,Z值将6〜8之间的返回值。结果

I want to detect if the device is facing up. (Not angled but flat to the ground facing up).
On some devices for facing up, z value will return values between 9~10. (Most devices)
However, on Nexus 7, for facing up, z value will return values between 6~8.

我的code是:

if(z_value > 9.0) {
    // device facing up
}
else {
    // device is in angled
}

然而,上述code不工作了。因为Nexus7未达到9 z_value

However, above code doesn't work anymore. Since Nexus7 doesn't reach z_value of 9.

如何可以检测设备是否面临(完全)向上或没有。 (不要求z_value> 0)

How can I detect if the device is facing (entirely) up or not. (not asking z_value > 0)

我的全code是如下:

My full code is below:

@Override
protected void onStart() {
    super.onStart();
    try {
        sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if(sensorList.size() > 0){
            accelerometerPresent = true;
            accelerometerSensor = sensorList.get(0);  
        }
        else{
            accelerometerPresent = false;  
        }
        if(accelerometerPresent){
            sensorManager.registerListener(accelerometerListener, accelerometerSensor, SensorManager.SENSOR_DELAY_UI);
        }
    } catch(Exception e) {}
}

private SensorEventListener accelerometerListener = new SensorEventListener(){

            @Override
            public void onAccuracyChanged(Sensor arg0, int arg1) {}

            @Override
            public void onSensorChanged(SensorEvent arg0) {
                float z_value = arg0.values[2];
                Log.d("test", "z:" + z_value);
            }};

注意#1 结果
arg0.sensor.getMaximumRange()返回19.6133适用于Nexus 7.哪个传感器永不再来。

Note #1
arg0.sensor.getMaximumRange() returns 19.6133 for Nexus 7. Which sensor never returns.

注意#2 结果
如果你摇装置,z_value倾向于去高一点(有时8〜8.5)。
如果稳定地倾斜的装置中,z_value未达到8(最大)。

Note #2
If you shake the devices, z_value tends to go little higher (sometimes 8~8.5). If you steadily tilt the device, z_value doesn't reach 8 (max).

推荐答案

显然,设备校准不佳。一个精心校准装置应返回9.81米/秒2,重力加速度。

Apparently the device is poorly calibrated. A well-calibrated device should return 9.81m/s^2, the gravitational acceleration.

你可以做什么,而不是:比较Z值x和y的值。如果Z值大于设备主导朝上。例如:

What you could do instead: Compare the z value to the x and y values. If the z value dominates than the device is facing up. For example:

if (z/sqrt(x^2+y^2+z^2+1.0e-6) > 0.9) { // Facing up

我添加术语 1.0E-6 ,这样你就不会意外除以零。

I added the term 1.0e-6 so that you won't accidentally divide by zero.

这启发式要求的测试和调整,但我猜你的想法。祝你好运!

This heuristic requires testing and tweaking but I guess you get the idea. Good luck!

这篇关于机器人:在加速度计传感器的z值的范围是在不同的设备不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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