done compass heading calculation to trackscanner

Sun, 27 Nov 2011 20:11:42 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sun, 27 Nov 2011 20:11:42 +0100
changeset 16
0250ceddee1b
parent 15
7d4c0c816465
child 18
cc2274b92647

done compass heading calculation to trackscanner

trackscanner/bin/classes.dex file | annotate | diff | comparison | revisions
trackscanner/bin/classes/de/neosoft/trackscanner/TrackScannerActivity.class file | annotate | diff | comparison | revisions
trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java file | annotate | diff | comparison | revisions
Binary file trackscanner/bin/classes.dex has changed
Binary file trackscanner/bin/classes/de/neosoft/trackscanner/TrackScannerActivity.class has changed
--- a/trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java	Wed Nov 23 14:22:28 2011 +0100
+++ b/trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java	Sun Nov 27 20:11:42 2011 +0100
@@ -23,17 +23,18 @@
     private float[] RotationMatrix = new float[matrix_size];
     private float[] InclinationMatrix = new float[matrix_size];
     
+    private int delay;
+    
 	/** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         
-        int delay;
-        //delay = SensorManager.SENSOR_DELAY_NORMAL;
-        //delay = SensorManager.SENSOR_DELAY_FASTER;
+        delay = SensorManager.SENSOR_DELAY_NORMAL;
+        // delay = SensorManager.SENSOR_DELAY_FASTER;
         // delay = SensorManager.SENSOR_DELAY_FASTEST;
-        delay = SensorManager.SENSOR_DELAY_GAME;
+        // delay = SensorManager.SENSOR_DELAY_GAME;
         
         
 		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
@@ -55,37 +56,7 @@
     
 	@Override
 	public void onSensorChanged(SensorEvent event) {
-		long actualTime = System.currentTimeMillis();
-		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
-			//SensorManager.GRAVITY_EARTH
-	        float  alpha = 0.8f;
-			gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
-	        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
-	        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
 
-			linear_acceleration[0] = event.values[0] - gravity[0];
-	        linear_acceleration[1] = event.values[1] - gravity[1];
-	        linear_acceleration[2] = event.values[2] - gravity[2];
-
-	        mEdit.setText("Accelerometer action: "+
-					"\nDelta: "+(actualTime-lastUpdate)+"ms"+
-					"\nElevation Front/Back: "+gravity[0]+
-					"\nElevation Left/Right: "+gravity[1]+
-					"\nDrive Acceleration: "+linear_acceleration[0]+
-					"\nDrift Force: "+linear_acceleration[1]+
-					"\nVertical Force: "+linear_acceleration[2]
-					
-	        		);
-	        	/*
-	        	 *  GravX = front/back elevation grade (recalc to degrees)
-	        	 *  GravY =  left/right evelavtion grade
-	        	 *  LinAccX = driving acceleration
-	        	 *  LinAccY = drift acceleration
-	        	 *  LinAccZ = vertical acceleration
-	        	 */
-		}
-
-	
 		 switch (event.sensor.getType()) {
 		    case Sensor.TYPE_MAGNETIC_FIELD:
 		      mags[0] = event.values[0];
@@ -96,26 +67,50 @@
 		      accels[0] = event.values[0];
 		      accels[1] = event.values[1];
 		      accels[2] = event.values[2];
+
+		      //SensorManager.GRAVITY_EARTH
+		        float  alpha = 0.8f;
+				gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
+		        gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
+		        gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
+
+				linear_acceleration[0] = event.values[0] - gravity[0];
+		        linear_acceleration[1] = event.values[1] - gravity[1];
+		        linear_acceleration[2] = event.values[2] - gravity[2];
 		      break;
 		 }
 		 SensorManager.getRotationMatrix(RotationMatrix, InclinationMatrix,
 				        accels, mags);
-		 float magHeading = SensorManager.getInclination(InclinationMatrix);
+		 float orientation[] = new float[3];
+		 SensorManager.getOrientation(RotationMatrix, orientation);
+		 float azimut = orientation[0]; // orientation contains: azimut, p
+		 float heading = (-azimut*360/(2*3.14159f)); // 0 = North -90=West, 90= East
 
-		 if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
-				float[] values = event.values;
-				// Orientation (compass)
-				float x = values[0];
-				float y = values[1];
-				float z = values[2];
-				mEdit2.setText("Magnetic Field:"+
-						"\nX = "+x+
-						"\nY = "+y+
-						"\nZ = "+z+
-						"\nMagnetic Heading (rad): "+magHeading);
-			}
-		 
-		lastUpdate = actualTime;
+		 mEdit.setText("Accelerometer action: "+
+					"\nElevation Front/Back: "+gravity[0]+
+					"\nElevation Left/Right: "+gravity[1]+
+					"\nDrive Acceleration: "+linear_acceleration[0]+
+					"\nDrift Force: "+linear_acceleration[1]+
+					"\nVertical Force: "+linear_acceleration[2]
+					
+	        		);
+	        	/*
+	        	 *  GravX = front/back elevation grade (recalc to degrees)
+	        	 *  GravY =  left/right evlevation grade
+	        	 *  LinAccX = driving acceleration
+	        	 *  LinAccY = drift acceleration
+	        	 *  LinAccZ = vertical acceleration
+	        	 */
+
+		 float x = mags[0];
+		 float y = mags[1];
+		 float z = mags[2];
+		 mEdit2.setText("Magnetic Field:"+
+					"\nX = "+x+
+					"\nY = "+y+
+					"\nZ = "+z+
+					"\nHeading: "+heading);
+
 	}
 
 	@Override
@@ -131,7 +126,11 @@
 		// accelerometer sensors
 		sensorManager.registerListener(this,
 				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
-				SensorManager.SENSOR_DELAY_NORMAL);
+				delay);
+
+		sensorManager.registerListener(this,
+				sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
+				delay);
 	}
 
 	@Override

mercurial