使用磁场计算旋转矩阵

时间:2012-05-22 08:03:51

标签: android accelerometer sensor rotational-matrices magnetometer

在获取旋转矩阵值时,它包含public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic) 我在这里如何计算float[] gravity? 我找到了一个代码示例,它使用orientationAccelerometer来计算Magnetic field

boolean success = SensorManager.getRotationMatrix(
   matrixR,
   matrixI,
   valuesAccelerometer,
   valuesMagneticField);

if(success){
SensorManager.getOrientation(matrixR, matrixValues);

double azimuth = Math.toDegrees(matrixValues[0]);
double pitch = Math.toDegrees(matrixValues[1]);
double roll = Math.toDegrees(matrixValues[2]);

readingAzimuth.setText("Azimuth: " + String.valueOf(azimuth));
readingPitch.setText("Pitch: " + String.valueOf(pitch));
 readingRoll.setText("Roll: "+String.valueOf(roll));
}

我的问题是:

  • 方向值是旋转矩阵值吗?
  • 如果没有,那么如何实现此代码以获取使用磁性的旋转矩阵值?场?

要获得旋转矩阵,我使用此代码

 public void onSensorChanged(SensorEvent sensorEvent) {
    if (timestamp != 0) {
        final double dT = (sensorEvent.timestamp - timestamp) * NS2S;
            double magneticX = sensorEvent.values[0];
            double magneticY = sensorEvent.values[1];
            double magneticZ = sensorEvent.values[2];
                        double omegaMagnitude =Math.sqrt(magneticX*magneticX + magneticY*magneticY + magneticZ*magneticZ);

                        if (omegaMagnitude > EPSILON) {
                            magneticX /= omegaMagnitude;
                            magneticY /= omegaMagnitude;
                            magneticZ /= omegaMagnitude;
        }
                        double thetaOverTwo = omegaMagnitude * dT / 2.0f;
                        double sinThetaOverTwo =Math.sin(thetaOverTwo);
                        double cosThetaOverTwo = Math.cos(thetaOverTwo);
                        deltaRotationVector[0] = (double) (sinThetaOverTwo * magneticX);
                        deltaRotationVector[1] = (double) (sinThetaOverTwo * magneticY);
                        deltaRotationVector[2] = (double) (sinThetaOverTwo * magneticZ);
                        deltaRotationVector[3] = cosThetaOverTwo;


    }
     double[] deltaRotationMatrix = new double[9];
     SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
}

但问题是这个getRotationMatrixFromVector对传感器来说是未定义的。任何想法?

2 个答案:

答案 0 :(得分:4)

方向不是旋转矩阵,因为它只提供与磁北相关的角度。您可以获得旋转矩阵(方向余弦矩阵),它将帮助您以这种方式将坐标从设备框架转换为地球框架:

DCM from Wikipedia

azimuth  =方位角(弧度)

pitch  =音高(弧度)

roll  = roll(弧度)

答案 1 :(得分:1)

我知道这是一个旧线程但是如果它有帮助,对于Android我认为3x3旋转矩阵实际上是由批准答案的变体给出的。具体来说,在Android中,旋转矩阵是

     (cosφ cosψ - sinφ sinψ sinθ)     sinφ cosθ     ( cosφ sinψ + sinφ cosψ sinθ)
    -(sinφ cosψ + cosφ sinψ sinθ)     cosφ cosθ     (-sinφ sinψ + cosφ cosψ sinθ)
              -sinψ cosθ                 -sinθ                  cosφ cosθ

,其中

    φ = azimuth
    θ = pitch
    ψ = roll

对应于3x3 Android旋转矩阵R [0]到R [8](问题中的矩阵R)通过

    R[0] R[1] R[2]
    R[3] R[4] R[5]
    R[6] R[7] R[8]