Wed, 23 Nov 2011 08:16:38 +0100
added some more android sensor testing
--- a/.hgignore Tue Nov 22 21:36:35 2011 +0100 +++ b/.hgignore Wed Nov 23 08:16:38 2011 +0100 @@ -6,4 +6,6 @@ *.s *.elf -*.pyc \ No newline at end of file +*.pyc + +*.apk \ No newline at end of file
--- a/trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java Tue Nov 22 21:36:35 2011 +0100 +++ b/trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java Wed Nov 23 08:16:38 2011 +0100 @@ -13,21 +13,37 @@ private long lastUpdate; private EditText mEdit; private EditText mEdit2; + private float[] linear_acceleration = new float[3]; + private float[] gravity = new float[3]; + private float[] mags = new float[3]; + private float[] accels = new float[3]; + private int matrix_size = 16; + // matrices for letting SensorManager do its magic + private float[] RotationMatrix = new float[matrix_size]; + private float[] InclinationMatrix = new float[matrix_size]; + /** 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_FASTEST; + delay = SensorManager.SENSOR_DELAY_GAME; + + sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), - SensorManager.SENSOR_DELAY_NORMAL); + delay); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), - SensorManager.SENSOR_DELAY_NORMAL); + delay); lastUpdate = System.currentTimeMillis(); @@ -41,47 +57,64 @@ public void onSensorChanged(SensorEvent event) { long actualTime = System.currentTimeMillis(); if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { - float[] values = event.values; - // Movement - float x = values[0]; - float y = values[1]; - float z = values[2]; - - float accelerationSquareRoot = (x * x + y * y + z * z) - / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH); - - mEdit.setText("Accelerometer action: "+accelerationSquareRoot+ - "\nDelta: "+(actualTime-lastUpdate)+"ms"+ - "\naX: "+x+ - "\naY: "+y+ - "\naZ: "+z); - - /* - final float alpha = 0.8; - float[] linear_acceleration; - - float[] gravity; + //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]; + 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]; -*/ + 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 + */ } - 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:\n"+ - "\nX = "+x+ - "\nY = "+y+ - "\nZ = "+z ); - } + + switch (event.sensor.getType()) { + case Sensor.TYPE_MAGNETIC_FIELD: + mags[0] = event.values[0]; + mags[1] = event.values[1]; + mags[2] = event.values[2]; + break; + case Sensor.TYPE_ACCELEROMETER: + accels[0] = event.values[0]; + accels[1] = event.values[1]; + accels[2] = event.values[2]; + break; + } + SensorManager.getRotationMatrix(RotationMatrix, InclinationMatrix, + accels, mags); + float magHeading = SensorManager.getInclination(InclinationMatrix); + + 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; }