Advertisement
Guest User

Android rotation example

a guest
Jan 1st, 2011
5,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package com.example.android.hello;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorEvent;
  7. import android.hardware.SensorEventListener;
  8. import android.hardware.SensorManager;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.widget.TextView;
  12.  
  13. public class HelloWorld extends Activity implements SensorEventListener {
  14.     /* sensor data */
  15.     SensorManager m_sensorManager;
  16.     float []m_lastMagFields;
  17.     float []m_lastAccels;
  18.     private float[] m_rotationMatrix = new float[16];
  19.     private float[] m_remappedR = new float[16];
  20.     private float[] m_orientation = new float[4];
  21.  
  22.     /* fix random noise by averaging tilt values */
  23.     final static int AVERAGE_BUFFER = 30;
  24.     float []m_prevPitch = new float[AVERAGE_BUFFER];
  25.     float m_lastPitch = 0.f;
  26.     float m_lastYaw = 0.f;
  27.     /* current index int m_prevEasts */
  28.     int m_pitchIndex = 0;
  29.  
  30.     float []m_prevRoll = new float[AVERAGE_BUFFER];
  31.     float m_lastRoll = 0.f;
  32.     /* current index into m_prevTilts */
  33.     int m_rollIndex = 0;
  34.  
  35.     /* center of the rotation */
  36.     private float m_tiltCentreX = 0.f;
  37.     private float m_tiltCentreY = 0.f;
  38.     private float m_tiltCentreZ = 0.f;
  39.  
  40.     /** Called when the activity is first created. */
  41.     @Override
  42.     public void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.main);
  45.         m_sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  46.         registerListeners();
  47.     }
  48.  
  49.     private void registerListeners() {
  50.         m_sensorManager.registerListener(this, m_sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
  51.         m_sensorManager.registerListener(this, m_sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
  52.     }
  53.  
  54.     private void unregisterListeners() {
  55.         m_sensorManager.unregisterListener(this);
  56.     }
  57.  
  58.     @Override
  59.     public void onDestroy() {
  60.         unregisterListeners();
  61.         super.onDestroy();
  62.     }
  63.  
  64.     @Override
  65.     public void onPause() {
  66.         unregisterListeners();
  67.         super.onPause();
  68.     }
  69.  
  70.     @Override
  71.     public void onResume() {
  72.         registerListeners();
  73.         super.onResume();
  74.     }
  75.  
  76.     @Override
  77.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  78.     }
  79.  
  80.     @Override
  81.     public void onSensorChanged(SensorEvent event) {
  82.         if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
  83.             accel(event);
  84.         }
  85.         if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
  86.             mag(event);
  87.         }
  88.     }
  89.  
  90.     private void accel(SensorEvent event) {
  91.         if (m_lastAccels == null) {
  92.             m_lastAccels = new float[3];
  93.         }
  94.  
  95.         System.arraycopy(event.values, 0, m_lastAccels, 0, 3);
  96.  
  97.         /*if (m_lastMagFields != null) {
  98.             computeOrientation();
  99.         }*/
  100.     }
  101.  
  102.     private void mag(SensorEvent event) {
  103.         if (m_lastMagFields == null) {
  104.             m_lastMagFields = new float[3];
  105.         }
  106.  
  107.         System.arraycopy(event.values, 0, m_lastMagFields, 0, 3);
  108.  
  109.         if (m_lastAccels != null) {
  110.             computeOrientation();
  111.         }
  112.     }
  113.  
  114.     Filter [] m_filters = { new Filter(), new Filter(), new Filter() };
  115.  
  116.     private class Filter {
  117.         static final int AVERAGE_BUFFER = 10;
  118.         float []m_arr = new float[AVERAGE_BUFFER];
  119.         int m_idx = 0;
  120.  
  121.         public float append(float val) {
  122.             m_arr[m_idx] = val;
  123.             m_idx++;
  124.             if (m_idx == AVERAGE_BUFFER)
  125.                 m_idx = 0;
  126.             return avg();
  127.         }
  128.         public float avg() {
  129.             float sum = 0;
  130.             for (float x: m_arr)
  131.                 sum += x;
  132.             return sum / AVERAGE_BUFFER;
  133.         }
  134.  
  135.     }
  136.  
  137.     private void computeOrientation() {
  138.         if (SensorManager.getRotationMatrix(m_rotationMatrix, null, m_lastMagFields, m_lastAccels)) {
  139.             SensorManager.getOrientation(m_rotationMatrix, m_orientation);
  140.  
  141.             /* 1 radian = 57.2957795 degrees */
  142.             /* [0] : yaw, rotation around z axis
  143.              * [1] : pitch, rotation around x axis
  144.              * [2] : roll, rotation around y axis */
  145.             float yaw = m_orientation[0] * 57.2957795f;
  146.             float pitch = m_orientation[1] * 57.2957795f;
  147.             float roll = m_orientation[2] * 57.2957795f;
  148.  
  149.             m_lastYaw = m_filters[0].append(yaw);
  150.             m_lastPitch = m_filters[1].append(pitch);
  151.             m_lastRoll = m_filters[2].append(roll);
  152.             TextView rt = (TextView) findViewById(R.id.roll);
  153.             TextView pt = (TextView) findViewById(R.id.pitch);
  154.             TextView yt = (TextView) findViewById(R.id.yaw);
  155.             yt.setText("azi z: " + m_lastYaw);
  156.             pt.setText("pitch x: " + m_lastPitch);
  157.             rt.setText("roll y: " + m_lastRoll);
  158.         }
  159.     }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement