【问题标题】:finding orientation using getRotationMatrix() and getOrientation()使用 getRotationMatrix() 和 getOrientation() 查找方向
【发布时间】:2011-04-30 12:30:53
【问题描述】:

我试图获取指向相机外的矢量相对于磁北的方向。我的印象是我需要使用从 getOrientation() 返回的值,但我不确定它们代表什么。当我更改手机的方向时,从 getOrientation() 获得的值不会发生可预测的变化(旋转 90 度不会将值更改 90 度)。我需要知道 getOrientation() 返回的值是什么意思。到目前为止我所拥有的内容如下:

package com.example.orientation;

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

public class Orientation extends Activity{

 private SensorManager mSM;
 private mSensorEventListener mSEL;

    float[] inR = new float[16];
    float[] outR= new float[16];
    float[] I = new float[16];
    float[] gravity = new float[3];
    float[] geomag = new float[3];
    float[] orientVals = new float[3];

    final float pi = (float) Math.PI;
    final float rad2deg = 180/pi;    

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSM = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSEL = new mSensorEventListener();  

        mSM.registerListener(mSEL, 
       mSM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
       SensorManager.SENSOR_DELAY_NORMAL);

     mSM.registerListener(mSEL, 
       mSM.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
       SensorManager.SENSOR_DELAY_NORMAL);

 }



 private class mSensorEventListener implements SensorEventListener{

  @Override
  public void onAccuracyChanged(Sensor arg0, int arg1) {}

  @Override
  public void onSensorChanged(SensorEvent event) {

   // If the sensor data is unreliable return
   if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    return;

   // Gets the value of the sensor that has been changed
   switch (event.sensor.getType()){  
   case Sensor.TYPE_ACCELEROMETER:
    gravity = event.values.clone();
    break;
   case Sensor.TYPE_MAGNETIC_FIELD:
    geomag = event.values.clone();
    break;
   }

   // If gravity and geomag have values then find rotation matrix
   if (gravity != null && geomag != null){

    // checks that the rotation matrix is found
    boolean success = SensorManager.getRotationMatrix(inR, I, gravity, geomag);
    if (success){

     // Re-map coordinates so y-axis comes out of camera
     SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X, 
       SensorManager.AXIS_Z, outR);

     // Finds the Azimuth and Pitch angles of the y-axis with 
     // magnetic north and the horizon respectively
     SensorManager.getOrientation(outR, orientVals);
     float azimuth = orientVals[0]*rad2deg;
     float pitch = orientVals[1]*rad2deg;
     float roll = orientVals[2]*rad2deg;

     // Displays a pop up message with the azimuth and inclination angles
     String endl = System.getProperty("line.separator");
     Toast.makeText(getBaseContext(), 
       "Rotation:" +
       outR[0] + " " + outR[1] + " " + outR[2] + endl +
       outR[4] + " " + outR[5] + " " + outR[6] + endl +
       outR[8] + " " + outR[9] + " " + outR[10] + endl +endl +
       "Azimuth: " + azimuth + " degrees" + endl + 
       "Pitch: " + pitch + " degrees" + endl +
       "Roll: " + roll + " degrees", 
       Toast.LENGTH_LONG).show();
    } /*else
     Toast.makeText(getBaseContext(), 
       "Get Rotation Matrix Failed", Toast.LENGTH_LONG).show();*/
   }   
  }

    }

}

我查看了有关 sensorManager 类的文档,但没有帮助解决这个问题。如果有人可以帮助我从中获得意义,我将不胜感激。我正在运行 Android 2.1 的 Nexus One 上进行测试

【问题讨论】:

    标签: rotation orientation accelerometer augmented-reality magnetometer


    【解决方案1】:

    我认为您应该在 getOrientation 行中将 outR 更改为 inR

    boolean success = SensorManager.getRotationMatrix(inR, I, gravity, geomag);
    if (success){
     // Re-map coordinates so y-axis comes out of camera
     SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X, 
       SensorManager.AXIS_Z, outR);
    
     // Finds the Azimuth and Pitch angles of the y-axis with 
     // magnetic north and the horizon respectively
     **SensorManager.getOrientation(inR, orientVals);**
    

    【讨论】:

      【解决方案2】:

      您需要获取设备的方向(横向/纵向) 并做出一些补偿。

      SensorManager.getOrientation(R, values);  
      mHeading = (int) Math.toDegrees(values[0]); 
      Display display = 
      ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
      int compensation = display.getRotation() * 90;                          
      mHeading = mHeading+compensation;
      

      【讨论】:

        【解决方案3】:

        您可以查看在屏幕上显示原始值的logger application。在其描述中,您会找到指向源代码的链接,以便您了解它如何访问传感器。

        HTH, 丹尼尔

        【讨论】:

          【解决方案4】:

          我认为您应该使用 getInclination 来获取方向而不是获取方向。因为 getRotationMatrix 是根据重力和地磁场计算的,你会直接从磁场中得到倾角。

          【讨论】:

            【解决方案5】:

            因为我是 android 新手,所以我使用 toast 在屏幕上显示信息。我将其更改为仅更新视图上的文本,这似乎可以解决问题。我还发现我认为 orientVals 实际上是正确的。对于我需要的东西,不使用卷。也没有意识到有一种方法可以将 rad 转换为内置的 deg,所以我只是使用了它。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-02-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-10
              • 2023-01-12
              • 1970-01-01
              • 2019-05-16
              相关资源
              最近更新 更多