如何更新与来自陀螺仪的旋转矩阵方位角? [英] How to update the azimuth with the rotation matrix from gyroscope?

查看:1220
本文介绍了如何更新与来自陀螺仪的旋转矩阵方位角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有我的当前方位为(方位角,俯仰,滚动)。现在,我想我的更新与陀螺仪的方向。据 Android开发网站给出的codeS ,我可以得到所谓的 deltaRotationMatrix 如下:

Suppose I have my current orientation as (azimuth, pitch, roll). Now I wish to update my orientation with the gyroscope. According to the codes given by the Android development web, I can obtain the so-called deltaRotationMatrix as follows:

// 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;
   }
}

我应该如何着手在这个片段中,以更新我的方向?

推荐答案

您只需要在rotationCurrentMatrix乘以deltaRotationMatrix,然后做出SensorManager.getOrientation()的调用。您将需要实现矩阵相乘的方法。您还需要一个初始currentRotationMatrix,可以使用加速度传感器和磁传感器SensorManager.getRotationMatrix和SensorManager.getOrientation获取初始currentRotationMatrix。或者,你可以使用TYPE_ROTATION_VECTOR获取初始currentRotationMatrix。

You just need to multiply the deltaRotationMatrix by the rotationCurrentMatrix and then make a call to SensorManager.getOrientation(). You will need to implement a matrix multiplication method. You will also need an initial currentRotationMatrix, you can use the acceleration sensor and magnetic sensor with SensorManager.getRotationMatrix and SensorManager.getOrientation to get the initial currentRotationMatrix. Alternatively, you could use TYPE_ROTATION_VECTOR to get the initial currentRotationMatrix.

    currentRotationMatrix = matrixMultiplication(
                currentRotationMatrix,
                deltaRotationMatrix);

    SensorManager.getOrientation(currentRotationMatrix,
                gyroscopeOrientation;

不幸的是,你会意识到的是,即使这是应该的TYPE_GYROSCOPE传感器进行校准漂移并不做了很好的工作和传感器漂移迅速出旋转与设备。令人沮丧的。

Unfortunately, what you will realize is that even the TYPE_GYROSCOPE sensor which is supposed to be calibrated for drift doesn't do a very good job and the sensor quickly drifts out of rotation with the device. Frustrating.

我有一个GitHub的回购与这一切实现的 rel=\"nofollow\">
而在Play商店工作项目<一个href=\"https://play.google.com/store/apps/details?id=com.kircherelectronics.com.gyroscopeexplorer&hl=en\"相对=nofollow>这里

I have a GitHub repo with all of this implemented here And a working project on the Play Store here

这篇关于如何更新与来自陀螺仪的旋转矩阵方位角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆