9

Estoy tratando de obtener la dirección del vector apuntando fuera de la cámara, con respecto al norte magnético. Tengo la impresión de que necesito usar los valores devueltos por getOrientation(), pero no estoy seguro de lo que representan. Los valores que obtengo de getOrientation() no cambian de manera predecible cuando cambio la orientación del teléfono (al girar 90 grados no cambian los valores 90 grados). Necesito saber qué significan los valores devueltos por getOrientation(). Lo que he escrito hasta ahora es a continuación:buscando orientación usando getRotationMatrix() y 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();*/ 
    } 
    } 

    } 

} 

He mirado en la documentación de la clase sensorManager, pero no ha ayudado a resolver esto. Si alguien pudiera ayudarme a entender el significado de esto, realmente lo apreciaría. Estoy probando en un Nexus One con Android 2.1

Respuesta

4

Porque era nuevo en Android estaba usando tostadas para mostrar la información en la pantalla. Lo cambié para simplemente actualizar el texto en una vista y eso pareció arreglarlo. También descubrí que lo que asumí que era en realidad los valores de Oriente es correcto. Para lo que necesito, el rollo no se usa. Tampoco me di cuenta de que había una forma de convertir de rad a deg incorporado, así que acabo de usar eso.

1

Creo que deberías usar getInclination para obtener la dirección en lugar de obtener la orientación. como getRotationMatrix está calculando en base a la gravedad y al campo geomagnético, obtendrás la inlinación del campo magnético directamente.

3

puede consultar el logger application que muestra los valores en bruto en la pantalla. En su descripción, encontrará un enlace al código fuente para que pueda aprender cómo accede a los sensores.

HTH, Daniele

3

que necesita para obtener la orientación del dispositivo (Horizontal/Vertical) y hacer algún tipo de compensación.

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; 
1

creo que debería cambiar a outr INR en línea getOrientation

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);** 
Cuestiones relacionadas