【问题标题】:How to calculate "Pitch" and "Roll" using SensorManager.getOrientation()如何使用 SensorManager.getOrientation() 计算“Pitch”和“Roll”
【发布时间】:2012-04-05 16:28:17
【问题描述】:

我目前正在尝试替换我的“Sensor.TYPE_ORIENTATION”,因为它已被弃用。 所以Android Documentation 缺少大部分关于如何操作的信息。在SO 上调试和挖掘时,我想出了如何计算曾经由 OrientationSensor 提供的azimuth。 我是这样做的:

float accelerometerVals[] = new float[3];
float magneticVals[] = new float[3];

float orientationSensorVals[] = new float[3]; 

float rotationMatrix[] = new float[16];
float orientation[] = new float[3];

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
             System.arraycopy(event.values, 0, accelerometerVals, 0, 3);
             break;
        case Sensor.TYPE_MAGNETIC_FIELD:
              System.arraycopy(event.values, 0, magneticVals, 0, 3);
              break;
        case Sensor.TYPE_ORIENTATION:
              System.arraycopy(event.values, 0, orientationSensorVals, 0, 3);
              break;
        default:
              break;
        }

        if (null != magneticVals && null != accelerometerVals){
            SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerVals, magneticVals)
            SensorManager.getOrientation(rotationMatrix, orientation);
            float azimuth = Math.toDegrees(orientation[0])
            //this calculation gives me the Azimuth in the same format that OrientationSensor
            azimuth += (azimuth >= 0) ? 0 : 360
            float FALSE_PITCH = Math.toDegrees(orientation[1])
            float FALSE_ROLL = Math.toDegrees(orientation[2])
        }    

注意变量 FALSE_PITCH && FALSE_ROLL。我不知道如何“标准化”(值从 +10 到 -10 不同)这个值,以获得与我之前在 OrientationSensor 中相同的输出

【问题讨论】:

    标签: android algorithm sensors android-sensors


    【解决方案1】:

    我也遇到过同样的问题,但我很惊讶你的点数不对,因为它对我来说是不对的。这是我使用的:

    //Let values[] be your orientation[] variable once in degrees.
    
    // Azimuth scaling
    if (values[0]<0) values[0] += 360;
    
    // Pitch scaling
    if (values[1]<-90) values[1] += (-2*(90+values[1]));
    else if (values[1]>90) values[1] += (2*(90-values[1]));
    
    // Roll scaling
    // NOT NEEDED
    

    你能发布一些方向传感器和你的度数orientation[]变量的输出吗?

    【讨论】:

    • 您能解释一下为什么没有必要对卷筒进行标准化吗?
    【解决方案2】:

    也许对你有帮助!!

    SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    boolean orientok = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
    
    //test if orientation sensor exist on the device
    if (!orientok){
            mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
            ((TextView)findViewById(R.id.orient)).setText("no orientation sensor");
        }
    
    public void onSensorChanged(SensorEvent event) {
        switch(event.sensor.getType()){
        case Sensor.TYPE_ORIENTATION:
            onOrientChanged(event);
            break;
                        ...// add other sensor type if you want and create for each one his function like the example below
    }
    
     public void onOrientChanged(SensorEvent event) {
        float azimuth,pitch,roll;
        azimuth = event.values[0];
        pitch = event.values[1];
        roll = event.values[2];
        ((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
        ((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
        ((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
    }
    

    【讨论】:

    • TYPE_ORIENTATION 已弃用
    猜你喜欢
    • 1970-01-01
    • 2019-06-19
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2021-05-10
    相关资源
    最近更新 更多