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

Sat, 03 Dec 2011 16:42:53 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Sat, 03 Dec 2011 16:42:53 +0100
changeset 37
136a79772098
parent 16
0250ceddee1b
permissions
-rw-r--r--

implemented race prepare phase, countdown and jumpstart detection

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
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
26 private int delay;
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
27
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
28 /** Called when the activity is first created. */
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
29 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
30 public void onCreate(Bundle savedInstanceState) {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31 super.onCreate(savedInstanceState);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
32 setContentView(R.layout.main);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
33
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
34 delay = SensorManager.SENSOR_DELAY_NORMAL;
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
35 // delay = SensorManager.SENSOR_DELAY_FASTER;
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
36 // delay = SensorManager.SENSOR_DELAY_FASTEST;
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
37 // delay = SensorManager.SENSOR_DELAY_GAME;
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
38
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
39
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
40 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
41 sensorManager.registerListener(this,
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
42 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
43 delay);
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
44
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
45 sensorManager.registerListener(this,
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
46 sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
47 delay);
13
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
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
50 lastUpdate = System.currentTimeMillis();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
51
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
52 mEdit = (EditText)findViewById(R.id.editText1);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
53 mEdit2 = (EditText)findViewById(R.id.editText2);
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
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
58 public void onSensorChanged(SensorEvent event) {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
59
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
60 switch (event.sensor.getType()) {
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
61 case Sensor.TYPE_MAGNETIC_FIELD:
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
62 mags[0] = event.values[0];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
63 mags[1] = event.values[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
64 mags[2] = event.values[2];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
65 break;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
66 case Sensor.TYPE_ACCELEROMETER:
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
67 accels[0] = event.values[0];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
68 accels[1] = event.values[1];
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
69 accels[2] = event.values[2];
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
70
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
71 //SensorManager.GRAVITY_EARTH
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
72 float alpha = 0.8f;
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
73 gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
74 gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
75 gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
76
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
77 linear_acceleration[0] = event.values[0] - gravity[0];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
78 linear_acceleration[1] = event.values[1] - gravity[1];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
79 linear_acceleration[2] = event.values[2] - gravity[2];
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
80 break;
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
81 }
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
82 SensorManager.getRotationMatrix(RotationMatrix, InclinationMatrix,
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
83 accels, mags);
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
84 float orientation[] = new float[3];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
85 SensorManager.getOrientation(RotationMatrix, orientation);
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
86 float azimut = orientation[0]; // orientation contains: azimut, p
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
87 float heading = (-azimut*360/(2*3.14159f)); // 0 = North -90=West, 90= East
14
d115c088b508 added some more android sensor testing
Malte Bayer <mbayer@neo-soft.org>
parents: 13
diff changeset
88
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
89 mEdit.setText("Accelerometer action: "+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
90 "\nElevation Front/Back: "+gravity[0]+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
91 "\nElevation Left/Right: "+gravity[1]+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
92 "\nDrive Acceleration: "+linear_acceleration[0]+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
93 "\nDrift Force: "+linear_acceleration[1]+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
94 "\nVertical Force: "+linear_acceleration[2]
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
95
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
96 );
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
97 /*
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
98 * GravX = front/back elevation grade (recalc to degrees)
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
99 * GravY = left/right evlevation grade
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
100 * LinAccX = driving acceleration
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
101 * LinAccY = drift acceleration
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
102 * LinAccZ = vertical acceleration
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
103 */
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
104
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
105 float x = mags[0];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
106 float y = mags[1];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
107 float z = mags[2];
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
108 mEdit2.setText("Magnetic Field:"+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
109 "\nX = "+x+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
110 "\nY = "+y+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
111 "\nZ = "+z+
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
112 "\nHeading: "+heading);
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
113
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
114 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
115
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
116 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
117 public void onAccuracyChanged(Sensor sensor, int accuracy) {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
118 // TODO Auto-generated method stub
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
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
122 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
123 protected void onResume() {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
124 super.onResume();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
125 // register this class as a listener for the orientation and
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
126 // accelerometer sensors
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
127 sensorManager.registerListener(this,
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
128 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
16
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
129 delay);
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
130
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
131 sensorManager.registerListener(this,
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
132 sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
0250ceddee1b done compass heading calculation to trackscanner
Malte Bayer <mbayer@neo-soft.org>
parents: 14
diff changeset
133 delay);
13
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
134 }
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 @Override
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
137 protected void onPause() {
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
138 // unregister listener
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
139 sensorManager.unregisterListener(this);
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
140 super.onStop();
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
141 }
0bb6f12124fd added android trackscanner project
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
142 }

mercurial