2013-10-13 21 views
2

我有一些getOrientation的麻煩。我在橫向模式下運行我的應用程序,我遇到了一個軸的問題。有一個在代碼更多的解釋,一些圖片:Android getOrientation問題 - 沿着軸的鏡像值

enter image description here enter image description here

valuesMagnet  = new float[3]; 
    valuesAccel  = new float[3]; 
    valuesOrientation = new float[3]; 
    rotationMatrix = new float[9]; 
    rotationMatrixTemp= new float[9]; 

.... 


@Override 
public void onSensorChanged(SensorEvent event) { 


    switch (event.sensor.getType()) { 
     case Sensor.TYPE_ACCELEROMETER: 
      System.arraycopy(event.values, 0, valuesAccel, 0, 3); 
      break; 

     case Sensor.TYPE_MAGNETIC_FIELD: 
      System.arraycopy(event.values, 0, valuesMagnet, 0, 3); 
      break; 
    } 

    if (SensorManager.getRotationMatrix(rotationMatrixTemp, null, valuesAccel, valuesMagnet)) { 
     //SensorManager.remapCoordinateSystem(rotationMatrixTemp, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix); 
     SensorManager.getOrientation(rotationMatrix, valuesOrientation); 

     for (int i = 0; i < valuesOrientation.length; i++) { 
      valuesOrientation[i] /= Math.PI; 
      valuesOrientation[i] *= 100; 
      valuesOrientation[i] = (int)valuesOrientation[i]; 
      valuesOrientation[i] /= 100; 
     } 

     updateOrientationBuffer(valuesOrientation); 

     quadcopter.onEvent(new Event(Event.Codes.ORIENTATION_CHANGED, calculateSmoothedOrientation())); 
    } 
    else { 
     Log.d("App", "Matrix rotate error"); 
    } 
} 

我想orientation[1]軸,而180度旋轉手機給-1到1之間的值的全光譜。另外,目標設備不像陀螺儀。

+0

你可以發佈更多的代碼,看看你如何生成valuesAccel和valuesMagnet嗎? –

+0

@GilMoshayof,我編輯了我的帖子。 –

+0

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/hardware/SensorEvent.java http://grepcode.com/ file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/hardware/SensorListener.java?av = f – pvllnspk

回答

1
@Override 
public void onSensorChanged(SensorEvent event) { 
    switch (event.sensor.getType()) { 
     case Sensor.TYPE_ACCELEROMETER: 
      System.arraycopy(event.values, 0, valuesAccel, 0, 3); 
      break; 

     case Sensor.TYPE_MAGNETIC_FIELD: 
      System.arraycopy(event.values, 0, valuesMagnet, 0, 3); 
      break; 

     case Sensor.TYPE_GRAVITY: 
      System.arraycopy(event.values, 0, valuesGravity, 0, 3); 
      break; 

     default: 
      break; 
    } 

    if (SensorManager.getRotationMatrix(rotationMatrixTemp, null, valuesAccel, valuesMagnet)) { 
     SensorManager.getOrientation(rotationMatrixTemp, valuesOrientation); 

     if(valuesGravity[2] < 0)          { 
      if (valuesOrientation[1] > 0) { 
       valuesOrientation[1] = (float) (Math.PI - valuesOrientation[1]); 
      } 
      else { 
       valuesOrientation[1] = (float) (-Math.PI - valuesOrientation[1]); 
      } 
     } 

     for (int i = 0; i < valuesOrientation.length; i++) { 
      valuesOrientation[i] /= Math.PI; 
      valuesOrientation[i] *= 100; 
      valuesOrientation[i] = (int)valuesOrientation[i]; 
      valuesOrientation[i] /= 100; 
     } 


     updateOrientationBuffer(valuesOrientation); 

     quadcopter.onEvent(new Event(Event.Codes.ORIENTATION_CHANGED, calculateSmoothedOrientation())); 
    } 
    else { 
     Log.d("Quadcopter-SM", "Matrix rotate error"); 
    } 
} 
0

根據你所面臨的問題,你所面對的是我所瞭解的是你主要關注兩個座標軸,主要是根據Cartesian系統的X和Y.

在這裏的答案,我會參考,Orintation1爲X和Orientation2爲Y.

所以,你要值的頻譜時,旋轉發生在X -90之間軸度到90度。

我已經分析了這個問題和你的代碼,下面是我對這個問題的解決方案。

package com.example.stackoverflowquestion; 

import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity implements SensorEventListener { 

    private SensorManager mSensorManager; 
    private Sensor accel, magnet; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     accel = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     magnet = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     mSensorManager.unregisterListener(this); 
    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     if (accel != null) { 
      mSensorManager.registerListener(this, accel, 
        SensorManager.SENSOR_DELAY_NORMAL); 
     } else { 
      Toast.makeText(this, "Accelerometer not available", 
        Toast.LENGTH_LONG).show(); 
     } 
     if (magnet != null) { 
      mSensorManager.registerListener(this, magnet, 
        SensorManager.SENSOR_DELAY_NORMAL); 
     } else { 
      Toast.makeText(this, "MagnetoMeter not available", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onAccuracyChanged(Sensor arg0, int arg1) { 
     // TODO Auto-generated method stub 

    } 

    float[] accelMat = new float[3]; 
    float[] magMat = new float[3]; 
    float[] rotmat = new float[9]; 
    float[] orientationMat = new float[3]; 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // TODO Auto-generated method stub 
     switch (event.sensor.getType()) { 
     case Sensor.TYPE_ACCELEROMETER: 
      System.arraycopy(event.values, 0, accelMat, 0, 3); 
      break; 
     case Sensor.TYPE_MAGNETIC_FIELD: 
      System.arraycopy(event.values, 0, magMat, 0, 3); 
      break; 
     } 

     if (SensorManager.getRotationMatrix(rotmat, null, accelMat, magMat)) { 
      SensorManager.getOrientation(rotmat, orientationMat); 

      for (int i = 0; i < orientationMat.length; i++) { 
       // convert from redians to degrees 
       orientationMat[i] = (float) (orientationMat[i]/Math.PI) * 180; 
      } 
      Log.d("yaw", ":" + orientationMat[0]); 
      Log.d("pitch", ":" + orientationMat[1]); 
      Log.d("roll", ":" + orientationMat[2]); 

     } 

    } 
} 

此代碼打印度的日誌,如果你想在redians然後簡單的公式更改是必需的。

注意:該代碼也可用於沒有陀螺儀的設備。我已經在HTC Evo 4G設備上進行了測試,並得到了預期的結果。

+0

對不起,您的解決方案中的音調在-90到90之間,而不是在-180和180之間。仍然,例如。 -45度意味着兩個不同的音調旋轉。 我想我已經在這裏發現瞭解決方案的評論:http://stackoverflow.com/questions/17747823/android-sensormanager-getorientation-returns-pitch-between-pi-2-and-pi-2 我是測試它。 –

+0

ok.t​​hat很好,你已經找到了解決方案,但是我們使用的公式是有用的。 –

+0

此外,在這種情況下,解決方案不夠全面,我正在等待我的朋友發佈代碼。 –