1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
package nextrek.arse.service;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import nextrek.math.LowPassFilter;
import nextrek.math.Matrix;
import android.content.Context;
import android.hardware.GeomagneticField;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class LocationModule implements SensorEventListener, LocationListener{
private static final String TAG = "SensorsActivity";
private static final AtomicBoolean computing = new AtomicBoolean(false);
private static final int MIN_TIME = 0;
private static final int MIN_DISTANCE = 0;
private static final int SENSOR_SPEED = SensorManager.SENSOR_DELAY_UI;
private static final float temp[] = new float[9]; // Temporary rotation matrix in Android format
private static final float rotation[] = new float[9]; // Final rotation matrix in Android format
private static final float grav[] = new float[3]; // Gravity (a.k.a accelerometer data)
private static final float mag[] = new float[3]; // Magnetic
/*
* Using Matrix operations instead. This was way too inaccurate,
* private static final float apr[] = new float[3]; //Azimuth, pitch, roll
*/
private static final Matrix worldCoord = new Matrix();
private static final Matrix magneticCompensatedCoord = new Matrix();
private static final Matrix xAxisRotation = new Matrix();
private static final Matrix mageticNorthCompensation = new Matrix();
private static GeomagneticField gmf = null;
private static float smooth[] = new float[3];
private static SensorManager sensorMgr = null;
private static List<Sensor> sensors = null;
private static Sensor sensorGrav = null;
private static Sensor sensorMag = null;
private static LocationManager locationMgr = null;
public LocationModule(Context context) {
sensorMgr = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
public void onStart() {
double angleX = Math.toRadians(-90);
double angleY = Math.toRadians(-90);
// Counter-clockwise rotation at -90 degrees around the x-axis
// [ 1, 0, 0 ]
// [ 0, cos, -sin ]
// [ 0, sin, cos ]
xAxisRotation.set(1f, 0f, 0f, 0f, (float) Math.cos(angleX), (float) -Math.sin(angleX), 0f, (float) Math.sin(angleX), (float) Math.cos(angleX));
try {
sensors = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0)
sensorGrav = sensors.get(0);
sensors = sensorMgr.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
if (sensors.size() > 0)
sensorMag = sensors.get(0);
sensorMgr.registerListener(this, sensorGrav, SENSOR_SPEED);
sensorMgr.registerListener(this, sensorMag, SENSOR_SPEED);
locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
try {
try {
Location gps = locationMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location network = locationMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gps != null)
onLocationChanged(gps);
else if (network != null)
onLocationChanged(network);
else
onLocationChanged(ARData.hardFix);
} catch (Exception ex2) {
onLocationChanged(ARData.hardFix);
}
gmf = new GeomagneticField((float) ARData.getCurrentLocation().getLatitude(), (float) ARData.getCurrentLocation().getLongitude(), (float) ARData.getCurrentLocation().getAltitude(), System.currentTimeMillis());
angleY = Math.toRadians(-gmf.getDeclination());
synchronized (mageticNorthCompensation) {
// Identity matrix
// [ 1, 0, 0 ]
// [ 0, 1, 0 ]
// [ 0, 0, 1 ]
mageticNorthCompensation.toIdentity();
// Counter-clockwise rotation at negative declination around the y-axis
// note: declination of the horizontal component of the magnetic field
// from true north, in degrees (i.e. positive means the magnetic
// field is rotated east that much from true north).
// note2: declination is the difference between true north and magnetic north
// [ cos, 0, sin ]
// [ 0, 1, 0 ]
// [ -sin, 0, cos ]
mageticNorthCompensation.set((float) Math.cos(angleY), 0f, (float) Math.sin(angleY), 0f, 1f, 0f, (float) -Math.sin(angleY), 0f, (float) Math.cos(angleY));
// Rotate the matrix to match the orientation
mageticNorthCompensation.prod(xAxisRotation);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex1) {
try {
if (sensorMgr != null) {
sensorMgr.unregisterListener(this, sensorGrav);
sensorMgr.unregisterListener(this, sensorMag);
sensorMgr = null;
}
if (locationMgr != null) {
locationMgr.removeUpdates(this);
locationMgr = null;
}
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
}
public void onStop() {
try {
try {
if (sensorMgr != null) {
sensorMgr.unregisterListener(this, sensorGrav);
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (sensorMgr != null) {
sensorMgr.unregisterListener(this, sensorMag);
}
} catch (Exception ex) {
ex.printStackTrace();
}
sensorMgr = null;
try {
if (locationMgr != null) {
locationMgr.removeUpdates(this);
}
} catch (Exception ex) {
ex.printStackTrace();
}
locationMgr = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* {@inheritDoc}
*/
@Override
public void onSensorChanged(SensorEvent evt) {
if (!computing.compareAndSet(false, true))
return;
if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
smooth = LowPassFilter.filter(0.5f, 1.0f, evt.values, grav);
grav[0] = smooth[0];
grav[1] = smooth[1];
grav[2] = smooth[2];
} else if (evt.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
smooth = LowPassFilter.filter(2.0f, 4.0f, evt.values, mag);
mag[0] = smooth[0];
mag[1] = smooth[1];
mag[2] = smooth[2];
}
// // Find real world position relative to phone location ////
// Get rotation matrix given the gravity and geomagnetic matrices
SensorManager.getRotationMatrix(temp, null, grav, mag);
// Translate the rotation matrices from Y and -X (landscape)
SensorManager.remapCoordinateSystem(temp, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotation);
/*
* Using Matrix operations instead. This was way too inaccurate,
* //Get the azimuth, pitch, roll
* SensorManager.getOrientation(rotation,apr);
* float floatAzimuth = (float)Math.toDegrees(apr[0]);
* if (floatAzimuth<0) floatAzimuth+=360;
* ARData.setAzimuth(floatAzimuth);
* ARData.setPitch((float)Math.toDegrees(apr[1]));
* ARData.setRoll((float)Math.toDegrees(apr[2]));
*/
// Convert from float[9] to Matrix
worldCoord.set(rotation[0], rotation[1], rotation[2], rotation[3], rotation[4], rotation[5], rotation[6], rotation[7], rotation[8]);
// // Find position relative to magnetic north ////
// Identity matrix
// [ 1, 0, 0 ]
// [ 0, 1, 0 ]
// [ 0, 0, 1 ]
magneticCompensatedCoord.toIdentity();
synchronized (mageticNorthCompensation) {
// Cross product the matrix with the magnetic north compensation
magneticCompensatedCoord.prod(mageticNorthCompensation);
}
// Cross product with the world coordinates
magneticCompensatedCoord.prod(worldCoord);
// Invert the matrix
magneticCompensatedCoord.invert();
// Set the rotation matrix (used to translate all object from lat/lon to x/y/z)
ARData.setRotationMatrix(magneticCompensatedCoord);
computing.set(false);
}
/**
* {@inheritDoc}
*/
@Override
public void onProviderDisabled(String provider) {
// Ignore
}
/**
* {@inheritDoc}
*/
@Override
public void onProviderEnabled(String provider) {
// Ignore
}
/**
* {@inheritDoc}
*/
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Ignore
}
/**
* {@inheritDoc}
*/
@Override
public void onLocationChanged(Location location) {
ARData.setCurrentLocation(location);
gmf = new GeomagneticField((float) ARData.getCurrentLocation().getLatitude(), (float) ARData.getCurrentLocation().getLongitude(), (float) ARData.getCurrentLocation().getAltitude(), System.currentTimeMillis());
double angleY = Math.toRadians(-gmf.getDeclination());
synchronized (mageticNorthCompensation) {
mageticNorthCompensation.toIdentity();
mageticNorthCompensation.set((float) Math.cos(angleY), 0f, (float) Math.sin(angleY), 0f, 1f, 0f, (float) -Math.sin(angleY), 0f, (float) Math.cos(angleY));
// Rotate the matrix to match the orientation
mageticNorthCompensation.prod(xAxisRotation);
}
}
/**
* {@inheritDoc}
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (sensor == null)
throw new NullPointerException();
if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
Log.e(TAG, "Compass data unreliable");
}
}
}
|