2013-03-09 5 views
0

Android 방향에서 기울기 천사를 가져 오려고합니다.getOrientation 메서드에서 올바른 각도를 가져올 수 없습니다.

내가

SensorManager.getOrientation에서 점점 오전 값 (mRotationMatrix을 mValuesOrientation)

는 수평면에 전화를 퍼 팅은 부적절한 값

을 반환합니다 ....도 아니다

내가 운이없이 인터넷에서 찾은 몇 가지 방법을 사용하여 시도 할 수 있습니다 ..

훨씬

01 시도 그래서

Math.sin (Math.toRadians (mValuesOrientation [0]));

Math.toDegrees (mValuesOrientation [0]);

코드는 getOrientation는 라디안 방위각, 피치와 롤을 반환

public class MainActivity extends Activity { 

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

    SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);   

    final float[] mValuesMagnet  = new float[3]; 
    final float[] mValuesAccel  = new float[3]; 
    final float[] mValuesOrientation = new float[3]; 
    final float[] mRotationMatrix = new float[9]; 

    final Button btn_valider = (Button) findViewById(R.id.button1); 
    final TextView txt1 = (TextView) findViewById(R.id.editText1); 
    final SensorEventListener mEventListener = new SensorEventListener() { 
     public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     } 

     public void onSensorChanged(SensorEvent event) { 
      // Handle the events for which we registered 
      switch (event.sensor.getType()) { 
       case Sensor.TYPE_ACCELEROMETER: 
        System.arraycopy(event.values, 0, mValuesAccel, 0, 3); 
        break; 

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

    // You have set the event lisetner up, now just need to register this with the 
    // sensor manager along with the sensor wanted. 
    setListners(sensorManager, mEventListener); 

    btn_valider.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View view) 
     { 
      SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet); 
      SensorManager.getOrientation(mRotationMatrix, mValuesOrientation); 
      String test; 
      /* double accX = -mValuesOrientation[0]/SensorManager.GRAVITY_EARTH; 
      double accY = -mValuesOrientation[1]/SensorManager.GRAVITY_EARTH; 
      double accZ = -mValuesOrientation[2]/SensorManager.GRAVITY_EARTH; 
      double totAcc = Math.sqrt((accX*accX)+(accY*accY)+(accZ*accZ)); 
      double tiltX = Math.asin(accX/totAcc); 
      double tiltY = Math.asin(accY/totAcc); 
      double tiltZ = Math.asin(accZ/totAcc);*/ 
      //float tiltX = mValuesOrientation[0] * 57.2957795f; 
      //float tiltY = mValuesOrientation[1] * 57.2957795f; 
      //float tiltZ = mValuesOrientation[2] * 57.2957795f; 
      //double tiltX =Math.sin(Math.toRadians(mValuesOrientation[0])); 
      //double tiltY =Math.sin(Math.toRadians(mValuesOrientation[1])); 
      //double tiltZ =Math.sin(Math.toRadians(mValuesOrientation[2])); 
      //String.format("Azimuth: %.2f\n\nPitch:%.2f\nRoll", azimuth, 
      //  pitch, roll); 
      double tiltX = Math.toDegrees(mValuesOrientation[0]); 
      double tiltY = Math.toDegrees(mValuesOrientation[1]); 
      double tiltZ = Math.toDegrees(mValuesOrientation[2]); 
      test = "results New: " +tiltX +" "+tiltY+ " "+ tiltZ; 
      Log.d("test", test); 
      txt1.setText(test); 
     } 
    }); 


} 
public void setListners(SensorManager sensorManager, SensorEventListener mEventListener) 
{ 
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
      SensorManager.SENSOR_DELAY_NORMAL); 
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
      SensorManager.SENSOR_DELAY_NORMAL); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

}

+0

틸트 란 무엇입니까? –

+0

성향. X Y Z 축에 전화 wrt .... 그래서 만약 내가 테이블 상단에 모든 x y는 z 값을 이상적으로 0에 있어야 내 휴대폰을 넣어 – Astr0

답변

2

이하입니다.

mValuesOrientation [0]은 방위각, 즉 z 축을 중심으로 한 회전입니다.
mValuesOrientation [1]은 x 축을 중심으로 한 피치입니다.
mValuesOrientation [2]는 롤이며 y 축을 중심으로 회전합니다.

휴대 전화가 테이블 위에 놓여 있으면 피치와 롤은 거의 0이되어야하지만 방위각은 그렇지 않아야합니다. 휴대 전화의 더 긴 크기 y 축이 자기 북쪽을 정확히 가리키고있는 경우에만 0입니다.

휴대 전화의 기울기, 즉 화면 표면과 세계 xy 평면 간의 각도를 계산하려면 피치 또는 롤 (기기 방향에 따라 다름)을 사용하거나 How to measure the tilt of the phone in XY plane using accelerometer in Android에 내 대답의 기울기를 사용할 수 있습니다

회전 매트릭스를 더 잘 이해하려면 내 대답 Convert magnetic field X, Y, Z values from device into global reference frame을 읽어야합니다.

+0

안녕하세요 감사합니다 !! 그건 유익했다 :) – Astr0

+0

단위가 라디안임을 지적 주셔서 감사합니다. 방위각/피치/롤을 각도로 제공하는 'TYPE_ORIENTATION'센서와 다릅니다. 당신은 API 워드 프로세서에서 작은 글씨를 읽지 않으면 꽤 함정 ... – user149408

관련 문제