9

Sto cercando di ottenere la direzione del vettore che punta fuori dalla telecamera, rispetto al nord magnetico. Ho l'impressione che ho bisogno di utilizzare i valori restituiti da getOrientation(), ma non sono sicuro di ciò che rappresentano. I valori che ottengo da getOrientation() non cambiano in modo prevedibile quando cambio l'orientamento del telefono (la rotazione di 90 gradi non modifica i valori di 90 gradi). Ho bisogno di sapere cosa significano i valori restituiti da getOrientation(). Quello che ho finora è scritto di seguito:trovare l'orientamento usando getRotationMatrix() e 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();*/ 
    } 
    } 

    } 

} 

Ho guardato la documentazione sulla classe SensorManager, ma non ha aiutato a risolvere questo. Se qualcuno potesse aiutarmi a ottenere un significato da questo, lo apprezzerei molto. Sto provando su un Nexus One con Android 2.1

risposta

4

Perché ero nuovo per Android stavo usando toast per visualizzare le informazioni sullo schermo. L'ho modificato per aggiornare solo il testo su una vista e sembrava risolverlo. Ho anche capito che quello che presumevo che gli orientVals fossero effettivamente è corretto. Per quello che mi serve il rotolo non è usato. Inoltre non mi ero reso conto che c'era un modo per convertire da rad a deg built in, quindi l'ho usato.

1

Penso che dovresti usare getInclination per ottenere la direzione invece di orientarti. come getRotationMatrix sta calcolando in base alla gravità e alla posizione geomagnetica e otterrete direttamente l'inline dal campo magnetico.

3

è possibile controllare il logger application che visualizza i valori non elaborati sullo schermo. Nella sua descrizione troverai un link al codice sorgente in modo che tu possa imparare come accede ai sensori.

HTH, Daniele

3

È necessario per ottenere l'orientamento del dispositivo (Paesaggio/Ritratto) e fare qualche risarcimento.

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

Penso che si dovrebbe cambiare outr a INR in linea 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);** 
Problemi correlati