使用旋转向量传感器

7
我想了解如何正确使用“旋转向量传感器”的输出结果。目前我得到了以下结果,想要从result[]计算偏航和俯仰角,以了解设备的指向(横向或纵向)。但是我的结果出现了问题,偏航角计算相当精确,但俯仰角表现奇怪。也许有人可以指点我如何使用这些数据。另外,我还想知道设备方向(横向或纵向)是否对该传感器的输出结果产生影响。谢谢。
private double max = Math.PI / 2 - 0.01;
private double min = -max;

private float[] rotationVectorAction(float[] values) {
    float[] result = new float[3];
    float vec[] = values;
    float quat[] = new float[4];
    float[] orientation = new float[3];
    SensorManager.getQuaternionFromVector(quat, vec);
    float[] rotMat = new float[9];
    SensorManager.getRotationMatrixFromVector(rotMat, quat);
    SensorManager.getOrientation(rotMat, orientation);
    result[0] = (float) orientation[0];
    result[1] = (float) orientation[1];
    result[2] = (float) orientation[2];     
    return result;
}

private void main () {
    float[] result = rotationVectorAction(sensorInput);
    yaw = result[0];
    pitch = result[1];
    pitch = (float) Math.max(min, pitch);
    pitch = (float) Math.min(max, pitch);
    float dx = (float) (Math.sin(yaw) * (-Math.cos(pitch)));
    float dy = (float) Math.sin(pitch);
    float dz = (float) (Math.cos(yaw) * Math.cos(pitch));
}

在OpenGL ES 2.0中,我设定了以下内容来移动我的相机:
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 0, dx, dy, dz, 0, 1, 0);
3个回答

5

最终我自己解决了它。pitch不正常工作的原因是由于 SensorManager.getQuaternionFromVector(quat, vec); 中输入和输出之间的差异。该方法期望向量vec的格式为(x|y|z|w),而输出quat的格式应为(w|x|y|z)。在我的情况下,我只需要将quat数组的第一个值移到最后,就可以完美解决这个问题。


3
上面的解决方法对我没有帮助。根据文档,需要删除SensorManager.getQuaternionFromVector(quat, vec); ,因为SensorManager.getRotationMatrixFromVector()需要由ROTATION_VECTOR传感器返回的旋转向量。
private float[] rotationVectorAction(float[] values) 
{
    float[] result = new float[3];
    float vec[] = values;
    float[] orientation = new float[3];
    float[] rotMat = new float[9];
    SensorManager.getRotationMatrixFromVector(rotMat, vec);
    SensorManager.getOrientation(rotMat, orientation);
    result[0] = (float) orientation[0]; //Yaw
    result[1] = (float) orientation[1]; //Pitch
    result[2] = (float) orientation[2]; //Roll
    return result;
}

1
我可以回答这个问题的一部分。
设备方向对传感器输出没有影响,因为传感器始终相对于设备的默认方向。然而,如果设备不处于默认方向,则需要考虑设备方向来绘制屏幕上的内容,例如指向北的指南针箭头,因为您绘制的画布坐标系已经旋转。

感谢您的帮助,现在我确定我已经处理好了这一部分。 - Dmitry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接