Gyroscope Issues with Device Orientation

前端 未结 1 537
日久生厌
日久生厌 2021-02-02 03:16

I\'m getting pitch and roll data from my device\'s gyroscope using this tutorial: http://www.thousand-thoughts.com/2012/03/android-sensor-fusion-tutorial/

All the readin

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 03:43

    The theory...

    I'm not really sure about the format in which your "frame of reference" matrix is represented, but typically rotations are done with matrix multiplication.

    Basically, you would take your "frame of reference matrix" and multiply it by a 90 degrees rotation matrix.

    Such a matrix can be found on Wikipedia: Three-dimensional rotation matrices

    Since your angle is 90 degrees, your sines and cosines would resolve to 1's or 0's which you can plug directly into the matrix instead of computing the sines and cosines. For example, a matrix that would rotate 90 degrees counter-clockwise about the x axis would look like this:

    1   0   0
    0   0   1
    0  -1   0
    

    Also, please not that matrices like these operate on row vectors of x y z coordinates. So for example, if you have a point in space that is at (2,5,7) and you would like to rotate it using the above matrix, you would have to do the following operation:

    |2 5 7| |1  0  0|
            |0  0  1|
            |0 -1  0|
    

    Which gives [2 -7 5]

    ...applied to your code

    I have glanced quickly at your code and it seems like the modification you need to make involves the output of calculateAccMagOrientation() because it is used to initialize the orientation of the device.

    1: public void calculateAccMagOrientation() {
    2:     if(SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) {
    3:         SensorManager.getOrientation(rotationMatrix, accMagOrientation);
    4:     }
    5: }
    

    At line 2 in the above snippet is where you get your initial rotationMatrix. Try multiplying rotationMatrix by a hand crafted 90 degrees rotation matrix before calling getOrientation at line 3. I think this will effectively re-align your reference orientation:

    public void calculateAccMagOrientation() {
        if(SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) {
            rotationMatrix = matrixMultiplication(rotationMatrix, my90DegRotationMatrix);
            SensorManager.getOrientation(rotationMatrix, accMagOrientation);
        }
    }
    

    Please note that depending on how the angles work in Android, you might need to use a 90 degrees clockwise rotation matrix instead of a counter-clockwise.

    Alternative solution

    It just occurred to me, maybe you could also simply subtract 90 from the final pitch result before displaying it?

    0 讨论(0)
提交回复
热议问题