9

我試圖構建一個簡單的增強現實應用程序,所以我開始使用傳感器數據。Android:計算設備方向的問題

根據這一線索(Android compass example)和實例(http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html),定向的使用Sensor.TYPE_ACCELEROMETERSensor.TYPE_MAGNETIC_FIELD計算並不真正適合。

所以我無法獲得「好」的價值。方位角值完全沒有任何意義,所以如果我只是將電話向上移動,則值會發生極大的變化。即使我只是旋轉手機,這些值也不代表手機方向。

有沒有人有一個想法,誰來改善根據給定的例子值的質量?

回答

0

您是否嘗試過組合(傳感器融合)類型Sensor.TYPE_ROTATION_VECTOR。這可能會產生更好的結果: 轉到https://developer.android.com/reference/android/hardware/SensorEvent.html並搜索'rotation_vector'。

+1

好吧,那麼有沒有任何示例如何使用此傳感器?我得到x * sin(θ/ 2),y * sin(θ/ 2)和z * sin(θ/ 2)的值。但是,我如何獲得價值,我需要建立我的指南針。我應該再次使用getRotationMatrix嗎?謝謝你的幫助。 –

+0

「X被定義爲矢量積Y.Z(它與設備當前位置的地面相切,大致指向東)。 Y在設備當前位置與地面相切並指向磁北。「 Z指向天空並垂直於地面。」 除塵幾何課本或谷歌它:),你應該能夠弄清楚。 –

19

你在什麼樣的方向使用這個示例應用程序?從代碼中可以看出,支持的唯一方向是桌面上的縱向或平面,取決於設備。 「好」是什麼意思?

旋轉設備時,該值不是「好」是正常的,設備座標系應該在縱向工作,或者平坦不知道(Y軸垂直沿着屏幕朝上,Z軸指向屏幕中心的屏幕,垂直於Y軸的X軸沿着屏幕向右)。有了這個,旋轉設備不會旋轉設備座標系,您必須重新映射它。

但是,如果你想在縱向方向的裝置的航向,這裏是一段代碼,爲我的作品好:在

@Override 
public void onSensorChanged(SensorEvent event) 
{ 
    // It is good practice to check that we received the proper sensor event 
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) 
    { 
     // Convert the rotation-vector to a 4x4 matrix. 
     SensorManager.getRotationMatrixFromVector(mRotationMatrix, 
       event.values); 
     SensorManager 
       .remapCoordinateSystem(mRotationMatrix, 
         SensorManager.AXIS_X, SensorManager.AXIS_Z, 
         mRotationMatrix); 
     SensorManager.getOrientation(mRotationMatrix, orientationVals); 

     // Optionally convert the result from radians to degrees 
     orientationVals[0] = (float) Math.toDegrees(orientationVals[0]); 
     orientationVals[1] = (float) Math.toDegrees(orientationVals[1]); 
     orientationVals[2] = (float) Math.toDegrees(orientationVals[2]); 

     tv.setText(" Yaw: " + orientationVals[0] + "\n Pitch: " 
       + orientationVals[1] + "\n Roll (not used): " 
       + orientationVals[2]); 

    } 
} 

你會得到航向(或方位):

orientationVals[0] 
+1

爲了記錄,我試着用3x3代碼矩陣,但它只適用於4x4(又名浮動[16]) – MonoThreaded

+0

我的也可以使用4 * 4(又名浮動[16]) –

7

大概遲到了派對。反正這裏是我是如何得到的方位

private final int sensorType = Sensor.TYPE_ROTATION_VECTOR; 
float[] rotMat = new float[9]; 
float[] vals = new float[3]; 

@Override 
public void onSensorChanged(SensorEvent event) { 
    sensorHasChanged = false; 
    if (event.sensor.getType() == sensorType){ 
     SensorManager.getRotationMatrixFromVector(rotMat, 
       event.values); 
     SensorManager 
       .remapCoordinateSystem(rotMat, 
         SensorManager.AXIS_X, SensorManager.AXIS_Y, 
         rotMat); 
     SensorManager.getOrientation(rotMat, vals); 
     azimuth = deg(vals[0]); // in degrees [-180, +180] 
     pitch = deg(vals[1]); 
     roll = deg(vals[2]); 
     sensorHasChanged = true; 
    } 
} 

希望它可以幫助

+0

'remapCoordinateSystem'調用只是身份轉換,所以它是多餘的。使用'rotMat'輸入和輸出,文檔明確說你不應該這樣做 – Thomas

+0

感謝這工作! – Bogdan

11

回答提伯是好的,但如果你日誌回滾價值,你會期待不規則的號碼。 (卷是AR瀏覽器很重要)

這是由於

SensorManager.remapCoordinateSystem(mRotationMatrix, 
        SensorManager.AXIS_X, SensorManager.AXIS_Z, 
        mRotationMatrix); 

你必須使用不同的矩陣進出重映射的。以下代碼適用於我的滾動值正確:

@Override 
public void onSensorChanged(SensorEvent event) 
{ 
    // It is good practice to check that we received the proper sensor event 
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) 
    { 
     // Convert the rotation-vector to a 4x4 matrix. 
     SensorManager.getRotationMatrixFromVector(mRotationMatrixFromVector, event.values); 
     SensorManager.remapCoordinateSystem(mRotationMatrixFromVector, 
        SensorManager.AXIS_X, SensorManager.AXIS_Z, 
        mRotationMatrix); 
     SensorManager.getOrientation(mRotationMatrix, orientationVals); 

     // Optionally convert the result from radians to degrees 
     orientationVals[0] = (float) Math.toDegrees(orientationVals[0]); 
     orientationVals[1] = (float) Math.toDegrees(orientationVals[1]); 
     orientationVals[2] = (float) Math.toDegrees(orientationVals[2]); 

     tv.setText(" Yaw: " + orientationVals[0] + "\n Pitch: " 
       + orientationVals[1] + "\n Roll (not used): " 
       + orientationVals[2]); 

    } 
} 
+0

是的,你可以檢查源代碼: public static boolean remapCoordinateSystem(float [] inR,int X, int Y, float [] outR) * @param outR *轉換後的旋轉矩陣。 inR和outR不應該是相同的 *數組。 – codevscolor