Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 6.92 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android Pitch and Roll Issue
  2. final SensorEventListener mEventListener = new SensorEventListener(){
  3.      public void onAccuracyChanged(Sensor sensor, int accuracy) {}  
  4.  public void onSensorChanged(SensorEvent event) {
  5.      setListners(sensorManager, mEventListener);
  6.  
  7.       SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
  8.      SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
  9.  
  10.  
  11.         synchronized (this) {
  12.  
  13.             switch (event.sensor.getType()){
  14.                 case Sensor.TYPE_ACCELEROMETER:
  15.  
  16.                     System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
  17.  
  18.                     long actualTime = System.currentTimeMillis();
  19.  
  20.                     //Sensitivity delay
  21.                     if (actualTime - lastUpdate < 250) {
  22.                         return;
  23.                         }
  24.                     else {
  25.                         sysAzimuth = (int)Math.toDegrees(mValuesOrientation[0]);
  26.                         sysPitch = (int)Math.toDegrees(mValuesOrientation[1]);
  27.                         sysRoll = (int)Math.toDegrees(mValuesOrientation[2]);
  28.  
  29.                         //invert direction with -1
  30.                       pitch = (sysPitch - pitchCal)*-1;
  31.                       roll = (sysRoll - rollCal);
  32.                       azimuth = sysAzimuth;
  33.  
  34.                     lastUpdate = actualTime;
  35.                     }
  36.        
  37. <?xml version="1.0" encoding="utf-8"?>
  38. <!-- This file is res/layout/main.xml -->
  39. <RelativeLayout
  40. xmlns:android="http://schemas.android.com/apk/res/android"
  41. android:layout_width="fill_parent"
  42. android:layout_height="fill_parent" >
  43. <Button android:id="@+id/update" android:text="Update Values"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:onClick="doUpdate" />
  47. <Button android:id="@+id/show" android:text="Show Me!"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:onClick="doShow" android:layout_toRightOf="@id/update" />
  51. <TextView android:id="@+id/preferred" android:textSize="20sp"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_below="@id/update" />
  55. <TextView android:id="@+id/orientation" android:textSize="20sp"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:layout_below="@id/preferred" />
  59. </RelativeLayout>
  60.  
  61.  
  62.  
  63. package YOURPACKAGE;
  64.  
  65.  
  66.  
  67. import android.app.Activity;
  68. import android.content.Intent;
  69. import android.hardware.Sensor;
  70. import android.hardware.SensorEvent;
  71. import android.hardware.SensorEventListener;
  72. import android.hardware.SensorManager;
  73. import android.net.Uri;
  74. import android.os.Build;
  75. import android.os.Bundle;
  76. import android.view.View;
  77. import android.view.WindowManager;
  78. import android.widget.TextView;
  79.  
  80.  
  81. public class YOURCLASS extends Activity implements SensorEventListener {
  82. private static final String TAG = "VirtualJax";
  83. private SensorManager mgr;
  84. private Sensor accel;
  85. private Sensor compass;
  86. private Sensor orient;
  87. private TextView preferred;
  88. private TextView orientation;
  89. private boolean ready = false;
  90. private float[] accelValues = new float[3];
  91. private float[] compassValues = new float[3];
  92. private float[] inR = new float[9];
  93. private float[] inclineMatrix = new float[9];
  94. private float[] orientationValues = new float[3];
  95. private float[] prefValues = new float[3];
  96. private float mAzimuth;
  97. private double mInclination;
  98. private int counter;
  99. private int mRotation;
  100.  
  101. @Override
  102. public void onCreate(Bundle savedInstanceState) {
  103. super.onCreate(savedInstanceState);
  104. setContentView(R.layout.main);
  105. preferred = (TextView)findViewById(R.id.preferred);
  106. orientation = (TextView)findViewById(R.id.orientation);
  107. mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
  108. accel = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  109. compass = mgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  110. orient = mgr.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  111. WindowManager window = (WindowManager)
  112. this.getSystemService(WINDOW_SERVICE);
  113. int apiLevel = Integer.parseInt(Build.VERSION.SDK);
  114. if(apiLevel <8) {
  115. mRotation = window.getDefaultDisplay().getOrientation();
  116. }
  117. else {
  118. mRotation = window.getDefaultDisplay().getRotation();
  119. }
  120. }
  121. @Override
  122. protected void onResume() {
  123. mgr.registerListener(this, accel,
  124. SensorManager.SENSOR_DELAY_GAME);
  125. mgr.registerListener(this, compass,
  126. SensorManager.SENSOR_DELAY_GAME);
  127. mgr.registerListener(this, orient,
  128.     SensorManager.SENSOR_DELAY_GAME);
  129.     super.onResume();
  130.     }
  131.     @Override
  132.     protected void onPause() {
  133.     mgr.unregisterListener(this, accel);
  134.     mgr.unregisterListener(this, compass);
  135.     mgr.unregisterListener(this, orient);
  136.     super.onPause();
  137.     }
  138.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  139.     // ignore
  140.     }
  141.     public void onSensorChanged(SensorEvent event) {
  142.     // Need to get both accelerometer and compass
  143.     // before we can determine our orientationValues
  144.     switch(event.sensor.getType()) {
  145.     case Sensor.TYPE_ACCELEROMETER:
  146.     for(int i=0; i<3; i++) {
  147.     accelValues[i] = event.values[i];
  148.     }
  149.     if(compassValues[0] != 0)
  150.     ready = true;
  151.     break;
  152.     case Sensor.TYPE_MAGNETIC_FIELD:
  153.     for(int i=0; i<3; i++) {
  154.     compassValues[i] = event.values[i];
  155.     }
  156.     if(accelValues[2] != 0)
  157.     ready = true;
  158.     break;
  159.     case Sensor.TYPE_ORIENTATION:
  160.     for(int i=0; i<3; i++) {
  161.     orientationValues[i] = event.values[i];
  162.     }
  163.     break;
  164.     }
  165.  
  166.     if(!ready)
  167.         return;
  168.         if(SensorManager.getRotationMatrix(
  169.         inR, inclineMatrix, accelValues, compassValues)) {
  170.         // got a good rotation matrix
  171.         SensorManager.getOrientation(inR, prefValues);
  172.         mInclination = SensorManager.getInclination(inclineMatrix);
  173.         // Display every 10th value
  174.         if(counter++ % 10 == 0) {
  175.         doUpdate(null);
  176.         counter = 1;
  177.         }
  178.  
  179.         }
  180.     }
  181.     public void doUpdate(View view) {
  182.     if(!ready)
  183.     return;
  184.     mAzimuth = (float) Math.toDegrees(prefValues[0]);
  185.     if(mAzimuth < 0) {
  186.     mAzimuth += 360.0f;
  187.     }
  188.     String msg = String.format(
  189.     "Preferred:nazimuth (Z): %7.3f npitch (X): %7.3fnroll (Y): %7.3f",
  190.     mAzimuth, Math.toDegrees(prefValues[1]),
  191.     Math.toDegrees(prefValues[2]));
  192.     preferred.setText(msg);
  193.     msg = String.format(
  194.     "Orientation Sensor:nazimuth (Z): %7.3fnpitch (X): %7.3fnroll (Y): %7.3f",
  195.     orientationValues[0],
  196.     orientationValues[1],
  197.     orientationValues[2]);
  198.     orientation.setText(msg);
  199.     preferred.invalidate();
  200.     orientation.invalidate();
  201.     }
  202.     public void doShow(View view) {
  203.     // google.streetview:cbll=30.32454,-81.6584&cbp=1,yaw,,pitch,1.0
  204.     // yaw = degrees clockwise from North
  205.     // For yaw we can use either mAzimuth or orientationValues[0].
  206.     //
  207.     // pitch = degrees up or down. -90 is looking straight up,
  208.     // +90 is looking straight down
  209.     // except that pitch doesn't work properly
  210.     Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(
  211.     "google.streetview:cbll=30.32454,-81.6584&cbp=1," +
  212.     Math.round(orientationValues[0]) + ",,0,1.0"
  213.     ));
  214.     startActivity(intent);
  215.     return;
  216.     }
  217.     }