2

現在の向きが であるとし(azimuth, pitch, roll)ます。ここで、ジャイロスコープを使用して向きを更新したいと考えています。Android 開発 Webによって提供されたコードによると、いわゆるdeltaRotationMatrix次のように取得できます。

// Create a constant to convert nanoseconds to seconds.
private static final float NS2S = 1.0f / 1000000000.0f;
private final float[] deltaRotationVector = new float[4]();
private float timestamp;

public void onSensorChanged(SensorEvent event) {
  // This timestep's delta rotation to be multiplied by the current rotation
  // after computing it from the gyro sample data.
  if (timestamp != 0) {
    final float dT = (event.timestamp - timestamp) * NS2S;
    // Axis of the rotation sample, not normalized yet.
    float axisX = event.values[0];
    float axisY = event.values[1];
    float axisZ = event.values[2];

    // Calculate the angular speed of the sample
    float omegaMagnitude = sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);

    // Normalize the rotation vector if it's big enough to get the axis
    // (that is, EPSILON should represent your maximum allowable margin of error)
    if (omegaMagnitude > EPSILON) {
      axisX /= omegaMagnitude;
      axisY /= omegaMagnitude;
      axisZ /= omegaMagnitude;
    }

    // Integrate around this axis with the angular speed by the timestep
    // in order to get a delta rotation from this sample over the timestep
    // We will convert this axis-angle representation of the delta rotation
    // into a quaternion before turning it into the rotation matrix.
    float thetaOverTwo = omegaMagnitude * dT / 2.0f;
    float sinThetaOverTwo = sin(thetaOverTwo);
    float cosThetaOverTwo = cos(thetaOverTwo);
    deltaRotationVector[0] = sinThetaOverTwo * axisX;
    deltaRotationVector[1] = sinThetaOverTwo * axisY;
    deltaRotationVector[2] = sinThetaOverTwo * axisZ;
    deltaRotationVector[3] = cosThetaOverTwo;
  }
  timestamp = event.timestamp;
  float[] deltaRotationMatrix = new float[9];
  SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);
    // User code should concatenate the delta rotation we computed with the current rotation
    // in order to get the updated rotation.
    // rotationCurrent = rotationCurrent * deltaRotationMatrix;
   }
}

オリエンテーションを更新するには、このスニペットをどのように処理すればよいですか?

4

1 に答える 1

1

deltaRotationMatrix に rotationCurrentMatrix を掛けてから、SensorManager.getOrientation() を呼び出すだけです。行列乗算メソッドを実装する必要があります。また、初期の currentRotationMatrix も必要です。加速度センサーと磁気センサーを SensorManager.getRotationMatrix と SensorManager.getOrientation で使用して、初期の currentRotationMatrix を取得できます。または、TYPE_ROTATION_VECTOR を使用して、最初の currentRotationMatrix を取得することもできます。

    currentRotationMatrix = matrixMultiplication(
                currentRotationMatrix,
                deltaRotationMatrix);

    SensorManager.getOrientation(currentRotationMatrix,
                gyroscopeOrientation;

残念ながら、ドリフト用にキャリブレーションされていることになっている TYPE_GYROSCOPE センサーでさえ、あまりうまくいかず、センサーがデバイスの回転からすぐにずれてしまうことに気付くでしょう。イライラします。

ここにすべてが実装された GitHub リポジトリがあり、ここ に Play ストアの作業中のプロジェクトがあります

于 2013-12-04T16:49:27.530 に答える