如何使用SensorManager.getOrientation()计算"Pitch"和"Roll"

Raf*_*l T 1 algorithm android sensor android-sensors

我目前正在尝试替换我的'Sensor.TYPE_ORIENTATION',因为它已被弃用.所以Android文档缺少大部分信息,关于如何做到这一点.虽然调试和陷在这里SO我想通了,如何计算azimuth其曾经被OrientationSensor提供.我是这样做的:

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])
        }    
Run Code Online (Sandbox Code Playgroud)

注意变量FALSE_PITCH && FALSE_ROLL.我不知道如何将这个值"规范化"(值从+10到-10不等),以获得与之前在OrientationSensor中相同的输出

Pet*_*fin 6

我遇到了同样的问题,但我很惊讶你的滚动是不对的,因为它适合我.这是我用过的东西:

//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
Run Code Online (Sandbox Code Playgroud)

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