Sun, 28 Oct 2012 18:53:03 +0100
typo...
package de.neosoft.trackscanner; import android.app.Activity; import android.os.Bundle; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.widget.EditText; public class TrackScannerActivity extends Activity implements SensorEventListener { private SensorManager sensorManager; 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]; private int delay; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 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), delay); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), delay); lastUpdate = System.currentTimeMillis(); mEdit = (EditText)findViewById(R.id.editText1); mEdit2 = (EditText)findViewById(R.id.editText2); } @Override public void onSensorChanged(SensorEvent event) { 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]; //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 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 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 public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override protected void onResume() { super.onResume(); // register this class as a listener for the orientation and // accelerometer sensors sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), delay); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), delay); } @Override protected void onPause() { // unregister listener sensorManager.unregisterListener(this); super.onStop(); } }