diff --git a/src/org/hitlabnz/sensor_fusion_demo/OrientationVisualisationFragment.java b/src/org/hitlabnz/sensor_fusion_demo/OrientationVisualisationFragment.java new file mode 100644 index 0000000..012cdd9 --- /dev/null +++ b/src/org/hitlabnz/sensor_fusion_demo/OrientationVisualisationFragment.java @@ -0,0 +1,110 @@ +package org.hitlabnz.sensor_fusion_demo; + +import org.hitlabnz.sensor_fusion_demo.orientationProvider.AccelerometerCompassProvider; +import org.hitlabnz.sensor_fusion_demo.orientationProvider.CalibratedGyroscopeProvider; +import org.hitlabnz.sensor_fusion_demo.orientationProvider.GravityCompassProvider; +import org.hitlabnz.sensor_fusion_demo.orientationProvider.ImprovedOrientationSensor1Provider; +import org.hitlabnz.sensor_fusion_demo.orientationProvider.ImprovedOrientationSensor2Provider; +import org.hitlabnz.sensor_fusion_demo.orientationProvider.OrientationProvider; +import org.hitlabnz.sensor_fusion_demo.orientationProvider.RotationVectorProvider; + +import android.hardware.SensorManager; +import android.opengl.GLSurfaceView; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.View.OnLongClickListener; + +/** + * A fragment that contains the same visualisation for different orientation providers + */ +public class OrientationVisualisationFragment extends Fragment { + /** + * The surface that will be drawn upon + */ + private GLSurfaceView mGLSurfaceView; + /** + * The class that renders the cube + */ + private CubeRenderer mRenderer; + /** + * The current orientation provider that delivers device orientation. + */ + private OrientationProvider currentOrientationProvider; + + /** + * The fragment argument representing the section number for this + * fragment. + */ + public static final String ARG_SECTION_NUMBER = "section_number"; + + @Override + public void onResume() { + // Ideally a game should implement onResume() and onPause() + // to take appropriate action when the activity looses focus + super.onResume(); + currentOrientationProvider.start(); + mGLSurfaceView.onResume(); + } + + @Override + public void onPause() { + // Ideally a game should implement onResume() and onPause() + // to take appropriate action when the activity looses focus + super.onPause(); + currentOrientationProvider.stop(); + mGLSurfaceView.onPause(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // Initialise the orientationProvider + switch (getArguments().getInt(ARG_SECTION_NUMBER)) { + case 1: + currentOrientationProvider = new ImprovedOrientationSensor1Provider((SensorManager) getActivity() + .getSystemService(SensorSelectionActivity.SENSOR_SERVICE)); + break; + case 2: + currentOrientationProvider = new ImprovedOrientationSensor2Provider((SensorManager) getActivity() + .getSystemService(SensorSelectionActivity.SENSOR_SERVICE)); + break; + case 3: + currentOrientationProvider = new RotationVectorProvider((SensorManager) getActivity().getSystemService( + SensorSelectionActivity.SENSOR_SERVICE)); + break; + case 4: + currentOrientationProvider = new CalibratedGyroscopeProvider((SensorManager) getActivity() + .getSystemService(SensorSelectionActivity.SENSOR_SERVICE)); + break; + case 5: + currentOrientationProvider = new GravityCompassProvider((SensorManager) getActivity().getSystemService( + SensorSelectionActivity.SENSOR_SERVICE)); + break; + case 6: + currentOrientationProvider = new AccelerometerCompassProvider((SensorManager) getActivity() + .getSystemService(SensorSelectionActivity.SENSOR_SERVICE)); + break; + default: + break; + } + + // Create our Preview view and set it as the content of our Activity + mRenderer = new CubeRenderer(); + mRenderer.setOrientationProvider(currentOrientationProvider); + mGLSurfaceView = new GLSurfaceView(getActivity()); + mGLSurfaceView.setRenderer(mRenderer); + + mGLSurfaceView.setOnLongClickListener(new OnLongClickListener() { + + @Override + public boolean onLongClick(View v) { + mRenderer.toggleShowCubeInsideOut(); + return true; + } + }); + + return mGLSurfaceView; + } +} \ No newline at end of file diff --git a/src/org/hitlabnz/sensor_fusion_demo/SensorSelectionActivity.java b/src/org/hitlabnz/sensor_fusion_demo/SensorSelectionActivity.java index 3b38bc4..a36e50e 100644 --- a/src/org/hitlabnz/sensor_fusion_demo/SensorSelectionActivity.java +++ b/src/org/hitlabnz/sensor_fusion_demo/SensorSelectionActivity.java @@ -2,31 +2,18 @@ package org.hitlabnz.sensor_fusion_demo; import java.util.Locale; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.AccelerometerCompassProvider; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.CalibratedGyroscopeProvider; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.GravityCompassProvider; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.ImprovedOrientationSensor1Provider; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.ImprovedOrientationSensor2Provider; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.OrientationProvider; -import org.hitlabnz.sensor_fusion_demo.orientationProvider.RotationVectorProvider; - import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.hardware.SensorManager; -import android.opengl.GLSurfaceView; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; -import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.view.View.OnLongClickListener; -import android.view.ViewGroup; /** * The main activity where the user can select which sensor-fusion he wants to try out @@ -158,96 +145,4 @@ public class SensorSelectionActivity extends FragmentActivity { } } - /** - * A fragment that contains the same visualisation for different orientation providers - */ - public static class OrientationVisualisationFragment extends Fragment { - /** - * The surface that will be drawn upon - */ - private GLSurfaceView mGLSurfaceView; - /** - * The class that renders the cube - */ - private CubeRenderer mRenderer; - /** - * The current orientation provider that delivers device orientation. - */ - private OrientationProvider currentOrientationProvider; - - /** - * The fragment argument representing the section number for this - * fragment. - */ - public static final String ARG_SECTION_NUMBER = "section_number"; - - @Override - public void onResume() { - // Ideally a game should implement onResume() and onPause() - // to take appropriate action when the activity looses focus - super.onResume(); - currentOrientationProvider.start(); - mGLSurfaceView.onResume(); - } - - @Override - public void onPause() { - // Ideally a game should implement onResume() and onPause() - // to take appropriate action when the activity looses focus - super.onPause(); - currentOrientationProvider.stop(); - mGLSurfaceView.onPause(); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Initialise the orientationProvider - switch (getArguments().getInt(ARG_SECTION_NUMBER)) { - case 1: - currentOrientationProvider = new ImprovedOrientationSensor1Provider((SensorManager) getActivity() - .getSystemService(SENSOR_SERVICE)); - break; - case 2: - currentOrientationProvider = new ImprovedOrientationSensor2Provider((SensorManager) getActivity() - .getSystemService(SENSOR_SERVICE)); - break; - case 3: - currentOrientationProvider = new RotationVectorProvider((SensorManager) getActivity().getSystemService( - SENSOR_SERVICE)); - break; - case 4: - currentOrientationProvider = new CalibratedGyroscopeProvider((SensorManager) getActivity() - .getSystemService(SENSOR_SERVICE)); - break; - case 5: - currentOrientationProvider = new GravityCompassProvider((SensorManager) getActivity().getSystemService( - SENSOR_SERVICE)); - break; - case 6: - currentOrientationProvider = new AccelerometerCompassProvider((SensorManager) getActivity() - .getSystemService(SENSOR_SERVICE)); - break; - default: - break; - } - - // Create our Preview view and set it as the content of our Activity - mRenderer = new CubeRenderer(); - mRenderer.setOrientationProvider(currentOrientationProvider); - mGLSurfaceView = new GLSurfaceView(getActivity()); - mGLSurfaceView.setRenderer(mRenderer); - - mGLSurfaceView.setOnLongClickListener(new OnLongClickListener() { - - @Override - public boolean onLongClick(View v) { - mRenderer.toggleShowCubeInsideOut(); - return true; - } - }); - - return mGLSurfaceView; - } - } - } diff --git a/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java b/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java index ac733d8..e60acc6 100644 --- a/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java +++ b/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java @@ -6,6 +6,7 @@ package org.hitlabnz.sensor_fusion_demo.orientationProvider; import java.util.ArrayList; import java.util.List; +import org.hitlabnz.sensor_fusion_demo.representation.EulerAngles; import org.hitlabnz.sensor_fusion_demo.representation.Matrixf4x4; import org.hitlabnz.sensor_fusion_demo.representation.Quaternion; @@ -14,8 +15,9 @@ import android.hardware.SensorEventListener; import android.hardware.SensorManager; /** - * Classes implementing this interface provide an orientation of the device either by directly accessing hardware, using - * Android sensor fusion or fusing sensors itself. + * Classes implementing this interface provide an orientation of the device + * either by directly accessing hardware, using Android sensor fusion or fusing + * sensors itself. * * The orientation can be provided as rotation matrix or quaternion. * @@ -24,7 +26,8 @@ import android.hardware.SensorManager; */ public abstract class OrientationProvider implements SensorEventListener { /** - * Sync-token for syncing read/write to sensor-data from sensor manager and fusion algorithm + * Sync-token for syncing read/write to sensor-data from sensor manager and + * fusion algorithm */ protected final Object syncToken = new Object(); @@ -51,7 +54,8 @@ public abstract class OrientationProvider implements SensorEventListener { /** * Initialises a new OrientationProvider * - * @param sensorManager The android sensor manager + * @param sensorManager + * The android sensor manager */ public OrientationProvider(SensorManager sensorManager) { this.sensorManager = sensorManager; @@ -72,7 +76,8 @@ public abstract class OrientationProvider implements SensorEventListener { for (Sensor sensor : sensorList) { // enable our sensors when the activity is resumed, ask for // 20 ms updates (Sensor_delay_game) - sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME); + sensorManager.registerListener(this, sensor, + SensorManager.SENSOR_DELAY_GAME); } } @@ -92,7 +97,8 @@ public abstract class OrientationProvider implements SensorEventListener { } /** - * @return Returns the current rotation of the device in the rotation matrix format (4x4 matrix) + * @return Returns the current rotation of the device in the rotation matrix + * format (4x4 matrix) */ public Matrixf4x4 getRotationMatrix() { synchronized (syncToken) { @@ -101,11 +107,24 @@ public abstract class OrientationProvider implements SensorEventListener { } /** - * @return Returns the current rotation of the device in the quaternion format (vector4f) + * @return Returns the current rotation of the device in the quaternion + * format (vector4f) */ public Quaternion getQuaternion() { synchronized (syncToken) { return currentOrientationQuaternion.clone(); } } + + /** + * @return Returns the current rotation of the device in the Euler-Angles + */ + public EulerAngles getEulerAngles() { + synchronized (syncToken) { + + float[] angles = new float[3]; + SensorManager.getOrientation(currentOrientationRotationMatrix.matrix, angles); + return new EulerAngles(angles[0], angles[1], angles[2]); + } + } } diff --git a/src/org/hitlabnz/sensor_fusion_demo/representation/EulerAngles.java b/src/org/hitlabnz/sensor_fusion_demo/representation/EulerAngles.java new file mode 100644 index 0000000..51296b9 --- /dev/null +++ b/src/org/hitlabnz/sensor_fusion_demo/representation/EulerAngles.java @@ -0,0 +1,26 @@ +package org.hitlabnz.sensor_fusion_demo.representation; + +public class EulerAngles { + + private float yaw; + private float pitch; + private float roll; + + public EulerAngles(float yaw, float pitch, float roll) { + this.yaw = yaw; + this.pitch = pitch; + this.roll = roll; + } + + public float getYaw() { + return yaw; + } + + public float getPitch() { + return pitch; + } + + public float getRoll() { + return roll; + } +}