简体   繁体   English

Android:从压力传感器获取高度

[英]Android: get height from pressure sensor

I tried to get the value of air pressure from my pressure sensor and get the value of height from this. 我试图从压力传感器中获取气压值,并从中获取高度值。 This is how I tried: 这是我尝试的方法:

            @Override
            public void onSensorChanged(SensorEvent event) {
              // when pressure value is changed, this method will be called.
              float pressure_value = 0.0f;
              float height = 0.0f;

              // if you use this listener as listener of only one sensor (ex, Pressure), then you don't need to check sensor type.
              if( Sensor.TYPE_PRESSURE == event.sensor.getType() ) {
                pressure_value = event.values[0];
                System.out.println("PRESSURE" + pressure_value);
                height = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, pressure_value);
                System.out.println("HÄÄÄÄÄÄÄÄ" + height);
                tv1.setText("Druck: " + pressure_value);

                tv2.setText("Höhe: " + height);

              }
            }

The value I get for my place is 440m. 我为自己的位置获得的价值是440m。 The real value is about 490 metres. 实际价值约为490米。 I checked that the pressure I get is already wrong. 我检查我得到的压力已经不对了。 What can I do to get the exact value from the sensor?? 我该怎么做才能从传感器获得准确的值?

The code you have is working as expected. 您拥有的代码正在按预期工作。 The pressure changes with the weather you can get a big range even at sea level eg say -10% +5%.. 压力随天气而变化,即使在海平面上,您也可以得到很大的范围,例如-10%+ 5%。

For real altitude information you need to do some calibration which means you use a calibrated value instead of SensorManager.PRESSURE_STANDARD_ATMOSPHERE in your code above. 对于真实的海拔信息,您需要进行一些校准,这意味着您在上面的代码中使用了校准值而不是SensorManager.PRESSURE_STANDARD_ATMOSPHERE

How you do the calibration if at all really depends on what you are trying to do. 校准的方式实际上完全取决于您要进行的操作。 one method would be to work out the calibration value at a known altitude. 一种方法是在已知高度计算出校准值。 eg at a known altitude get the pressure and then change the calibration value in the call in your code till you get something close enough to the known altitude you want out. 例如,在已知高度上获取压力,然后在代码中的调用中更改校准值,直到获得足够接近所需海拔高度的值。 You need to be aware that the calibration value will change with the weather. 您需要注意,校准值会随天气而变化。 How long it will be good enough for will depend on your use case. 足够长的时间取决于您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM