2010-06-03 29 views
8

我一直有這個問題太久了。這段代碼應該輸出加速度計的dx,dy,dz和dx的運行總數。它還應輸出方位角,俯仰角和俯仰角。在Android 2.1中使用getRotationMatrix和getOrientation

I've used the information given here,但無濟於事。

此代碼無法正確輸出音調,方位角或滾動。它分別輸出0.0,-0.0,-0.0的最後三個文字查看。

switch (event.sensor.getType()) { 
    case Sensor.TYPE_ACCELEROMETER: 
     accelerometerValues = event.values.clone(); 
    case Sensor.TYPE_MAGNETIC_FIELD: 
     geomagneticMatrix = event.values.clone(); 
     sensorReady = true; 
     break; 
    default: 
     break; 
} 

if (geomagneticMatrix != null && accelerometerValues != null && sensorReady) { 
    sensorReady = false; 

    float[] R = new float[16]; 
    float[] I = new float[16]; 

    SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticMatrix); 

    float[] actual_orientation = new float[3]; 
    SensorManager.getOrientation(R, actual_orientation); 

    tvXCoordinate.setText(accelerometerValues[0] + ""); 
    tvYCoordinate.setText(accelerometerValues[1] + ""); 
    tvZCoordinate.setText(accelerometerValues[2] + ""); 

    floatXTotal += accelerometerValues[0]; 
    tvXTotal.setText(floatXTotal + ""); 

    tvAzimuth.setText(actual_orientation[0] + ""); 
    tvPitch.setText(actual_orientation[1] + ""); 
    tvRoll.setText(actual_orientation[2] + ""); 
} 

回答

6

我可能失去了一些東西(你可能已經解決了這一點),但對我來說,它看起來像你的switch語句是不正確的:

switch (event.sensor.getType()) { 
     case Sensor.TYPE_ACCELEROMETER: 
      accelerometerValues = event.values.clone(); 
     case Sensor.TYPE_MAGNETIC_FIELD: 
      geomagneticMatrix = event.values.clone(); 
      sensorReady = true; 
      break; 
     default: 
      break; 
    } 

如果傳感器事件TYPE_ACCELEROMETER從值該事件將被克隆到accelerometerValuesgeomagneticMatrix並且sensorReady將被設置爲真。我認爲您可能需要更改此區塊的順序,或者可能需要在第一個案例後添加break;

2

從getOrientation()獲得0.0,-0.0,-0.0的原因是getRotationMatrix()並不總是得到有效的結果。您需要檢查getRotationMatrix()的返回值,如果結果無效,則返回值爲false;如果成功,則返回true。

補充:實際上,這並沒有出現。出於aganders指出的原因,您得到的結果無效。檢查返回值只會表明您實際上得到的結果無效。

相關問題