在纵向模式下测量设备卷时,如何改善结果?

时间:2016-05-26 20:19:44

标签: java android rotation hardware-programming

我正试图在纵向方向上拍摄设备的俯仰和滚动。 With the axes looking like this,它分别是x和z轴。现在我正在使用SensorManager API来使设备的俯仰,滚转和偏航平放,as is default.

当我尝试将旋转值从平面设备转换为垂直方向时,我会体验到其他SO用户称之为万向节锁定,这是欧拉角度工作中固有的问题。问题是我尝试实现一个旋转矩阵,as other users have来解决类似的问题,但即使我仍然有同样的万向节锁定问题。我已经包含了我的onSensorChanged方法,希望有人可以帮助找出问题所在。

public void onSensorChanged(SensorEvent se) {

    float degPitch = 0;
    float degRoll = 0;
    float degYaw = 0;
    float radPitch = 0;
    float radRoll = 0;
    float radYaw = 0;

    if (se.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mAccelerometerResult = se.values;
        Log.d("onSensorChanged", "Accelerometer: " + mAccelerometerResult.length);
    }

    if (se.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mMagneticFieldResult = se.values;
        Log.d("onSensorChanged", "Magnetic Field: " + mMagneticFieldResult.length);
    }

    if (mAccelerometerResult != null && mMagneticFieldResult != null) {
        float[] rotation = new float[9];
        float[] inclination = new float[9];

        boolean rotationMatrixCheck = mSensorManager.getRotationMatrix(rotation, inclination, mAccelerometerResult, mMagneticFieldResult);

        if (rotationMatrixCheck) {

            float[] orientation = new float[3];
            mSensorManager.getOrientation(rotation, orientation);


            radYaw = orientation[0];      //Yaw = Z axis
            radPitch = orientation[1];      //Pitch = X axis
            radRoll = orientation[2];      //Roll = Y axis

            degYaw = round((float) Math.toDegrees(radYaw), 2);
            degPitch = round((float)Math.toDegrees(radPitch), 2);
            degRoll = round((float)Math.toDegrees(radRoll), 2);


            if ((counter % 10) == 0) {
                //mYawTextView.setText(degYaw + "°");
                mPitchTextView.setText(degPitch + "°");
                mRollTextView.setText(degRoll + "°");
                counter = 0;
            } else {
                counter++;
            }
        }
    }

此外,我甚至不确定我是否理解我正在寻找的旋转值,如果我可以获得关于肖像轴的良好旋转。如果我想要设备的纵向卷轴(关于我原始图像的z轴),那么仍然是设备平放的卷(从平面轴图像约为y)?

任何可以在这里分享的见解都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我找到了问题的解决方案。如原始问题中所讨论的,将旋转矩阵以3×3网格获得以欧拉角度返回旋转矩阵。在4x4网格中获取矩阵将返回旋转矩阵(According to SO user Stochastically's answer found here).

的四元数表示

此外,必须重新映射旋转矩阵坐标系以便在纵向模式中使用。评论的变化如下所示。

<form>
<input type="number" id="Numb1">
<input name="submit" type="submit" onclick="assignVar()">


</form>

<script>
function assignVar() {
var firstNumb = document.getElementByID("Numb1");
}

</script>