trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java

Wed, 23 Nov 2011 08:16:38 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Wed, 23 Nov 2011 08:16:38 +0100
changeset 14
d115c088b508
parent 13
0bb6f12124fd
child 16
0250ceddee1b
permissions
-rw-r--r--

added some more android sensor testing

13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
1 package de.neosoft.trackscanner;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
2
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
3 import android.app.Activity;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
4 import android.os.Bundle;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
5 import android.hardware.Sensor;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
6 import android.hardware.SensorEvent;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
7 import android.hardware.SensorEventListener;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
8 import android.hardware.SensorManager;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
9 import android.widget.EditText;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
10
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
11 public class TrackScannerActivity extends Activity implements SensorEventListener {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
12 private SensorManager sensorManager;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
13 private long lastUpdate;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
14 private EditText mEdit;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
15 private EditText mEdit2;
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
16 private float[] linear_acceleration = new float[3];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
17 private float[] gravity = new float[3];
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
18
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
19 private float[] mags = new float[3];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
20 private float[] accels = new float[3];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
21 private int matrix_size = 16;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
22 // matrices for letting SensorManager do its magic
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
23 private float[] RotationMatrix = new float[matrix_size];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
24 private float[] InclinationMatrix = new float[matrix_size];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
25
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
26 /** Called when the activity is first created. */
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
27 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
28 public void onCreate(Bundle savedInstanceState) {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
29 super.onCreate(savedInstanceState);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
30 setContentView(R.layout.main);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
32 int delay;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
33 //delay = SensorManager.SENSOR_DELAY_NORMAL;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
34 //delay = SensorManager.SENSOR_DELAY_FASTER;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
35 // delay = SensorManager.SENSOR_DELAY_FASTEST;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
36 delay = SensorManager.SENSOR_DELAY_GAME;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
37
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
38
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
39 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
40 sensorManager.registerListener(this,
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
41 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
42 delay);
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
43
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
44 sensorManager.registerListener(this,
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
45 sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
46 delay);
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
47
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
48
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
49 lastUpdate = System.currentTimeMillis();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
50
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
51 mEdit = (EditText)findViewById(R.id.editText1);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
52 mEdit2 = (EditText)findViewById(R.id.editText2);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
53
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
54 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
55
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
56 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57 public void onSensorChanged(SensorEvent event) {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
58 long actualTime = System.currentTimeMillis();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
59 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
60 //SensorManager.GRAVITY_EARTH
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
61 float alpha = 0.8f;
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
62 gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
63 gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
64 gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
65
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
66 linear_acceleration[0] = event.values[0] - gravity[0];
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
67 linear_acceleration[1] = event.values[1] - gravity[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
68 linear_acceleration[2] = event.values[2] - gravity[2];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
69
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
70 mEdit.setText("Accelerometer action: "+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
71 "\nDelta: "+(actualTime-lastUpdate)+"ms"+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
72 "\nElevation Front/Back: "+gravity[0]+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
73 "\nElevation Left/Right: "+gravity[1]+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
74 "\nDrive Acceleration: "+linear_acceleration[0]+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
75 "\nDrift Force: "+linear_acceleration[1]+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
76 "\nVertical Force: "+linear_acceleration[2]
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
77
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
78 );
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
79 /*
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
80 * GravX = front/back elevation grade (recalc to degrees)
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
81 * GravY = left/right evelavtion grade
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
82 * LinAccX = driving acceleration
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
83 * LinAccY = drift acceleration
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
84 * LinAccZ = vertical acceleration
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
85 */
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
86 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
87
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
88
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
89 switch (event.sensor.getType()) {
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
90 case Sensor.TYPE_MAGNETIC_FIELD:
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
91 mags[0] = event.values[0];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
92 mags[1] = event.values[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
93 mags[2] = event.values[2];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
94 break;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
95 case Sensor.TYPE_ACCELEROMETER:
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
96 accels[0] = event.values[0];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
97 accels[1] = event.values[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
98 accels[2] = event.values[2];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
99 break;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
100 }
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
101 SensorManager.getRotationMatrix(RotationMatrix, InclinationMatrix,
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
102 accels, mags);
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
103 float magHeading = SensorManager.getInclination(InclinationMatrix);
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
104
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
105 if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
106 float[] values = event.values;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
107 // Orientation (compass)
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
108 float x = values[0];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
109 float y = values[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
110 float z = values[2];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
111 mEdit2.setText("Magnetic Field:"+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
112 "\nX = "+x+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
113 "\nY = "+y+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
114 "\nZ = "+z+
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
115 "\nMagnetic Heading (rad): "+magHeading);
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
116 }
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
117
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
118 lastUpdate = actualTime;
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
119 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
120
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
121 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
122 public void onAccuracyChanged(Sensor sensor, int accuracy) {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
123 // TODO Auto-generated method stub
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
124
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
125 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
126
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
127 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
128 protected void onResume() {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
129 super.onResume();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
130 // register this class as a listener for the orientation and
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
131 // accelerometer sensors
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
132 sensorManager.registerListener(this,
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
133 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
134 SensorManager.SENSOR_DELAY_NORMAL);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
135 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
136
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
137 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
138 protected void onPause() {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
139 // unregister listener
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
140 sensorManager.unregisterListener(this);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
141 super.onStop();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
142 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
143 }

mercurial