-
Notifications
You must be signed in to change notification settings - Fork 59
/
Sensors.java
324 lines (246 loc) · 9.42 KB
/
Sensors.java
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* MultiWii EZ-GUI
Copyright (C) <2012> Bartosz Szczygiel (eziosoft)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ezio.multiwii.helpers;
import java.lang.reflect.Method;
import java.util.Iterator;
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.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
public class Sensors implements SensorEventListener, LocationListener {
private Listener mListener = null;
Location location, oldLocation;
LowPassFilter filterYaw = new LowPassFilter(0.03f);
LowPassFilter filterPitch = new LowPassFilter(0.03f);
LowPassFilter filterRoll = new LowPassFilter(0.03f);
private LocationManager locationManager;
private String provider;
GeomagneticField geoField;
public int PhoneNumSat = 0;
public double PhoneLatitude = 0;
public double PhoneLongitude = 0;
public double PhoneAltitude = 0;
public double PhoneSpeed = 0;
public int PhoneFix = 0;
public float PhoneAccuracy = 0;
public float Declination = 0;
public LatLng MapCurrentPosition = new LatLng(0, 0);
SensorManager m_sensorManager;
float[] m_lastMagFields = new float[3];;
float[] m_lastAccels = new float[3];;
private float[] m_rotationMatrix = new float[16];
private float[] m_orientation = new float[4];
public float Pitch = 0.f;
public float Heading = 0.f;
public float Roll = 0.f;
private Context context;
String mocLocationProvider;
public boolean MockLocationWorking = false;
public interface Listener {
public void onSensorsStateChangeMagAcc();
public void onSensorsStateGPSLocationChange();
public void onSensorsStateGPSStatusChange();
}
public void registerListener(Listener listener) {
mListener = listener;
}
public void initMOCKLocation() {
mocLocationProvider = LocationManager.GPS_PROVIDER;
locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);
MockLocationWorking = true;
}
public boolean isMockEnabled() {
try {
int mock_location = Settings.Secure.getInt(context.getContentResolver(), "mock_location");
if (mock_location == 0) {
try {
Settings.Secure.putInt(context.getContentResolver(), "mock_location", 1);
} catch (Exception ex) {
}
mock_location = Settings.Secure.getInt(context.getContentResolver(), "mock_location");
}
if (mock_location == 0) {
Toast.makeText(context, "Turn on the mock locations in your Android settings", Toast.LENGTH_LONG).show();
return false;
} else {
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
public void setMOCKLocation(double Latitude, double Longitude, float Altitude, float Heading, float speed) {
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(Latitude); // double
mockLocation.setLongitude(Longitude);
mockLocation.setAltitude(Altitude);
mockLocation.setTime(System.currentTimeMillis());
mockLocation.setAccuracy(1);
mockLocation.setBearing(Heading);
mockLocation.setSpeed(speed * 0.01f);
try {
Method locationJellyBeanFixMethod = Location.class.getMethod("makeComplete");
if (locationJellyBeanFixMethod != null) {
locationJellyBeanFixMethod.invoke(mockLocation);
}
} catch (Exception e) {
// TODO: handle exception
}
locationManager.setTestProviderLocation(mocLocationProvider, mockLocation);
}
public void ClearMOCKLocation() {
if (mocLocationProvider != null) {
Log.d("aaa", "ClearMOCKLocation");
locationManager.clearTestProviderEnabled(mocLocationProvider);
locationManager.clearTestProviderLocation(mocLocationProvider);
locationManager.clearTestProviderStatus(mocLocationProvider);
locationManager.removeTestProvider(mocLocationProvider);
start();
MockLocationWorking = false;
}
}
public Sensors(Context context) {
this.context = context;
m_sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
// if (!app.D)
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
Declination = geoField.getDeclination();
MapCurrentPosition = new LatLng(location.getLatitude(), location.getLongitude());
oldLocation = location;
}
locationManager.addGpsStatusListener(new GpsStatus.Listener() {
@Override
public void onGpsStatusChanged(int event) {
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
GpsStatus status = locationManager.getGpsStatus(null);
Iterable<GpsSatellite> sats = status.getSatellites();
Iterator<GpsSatellite> it = sats.iterator();
PhoneNumSat = 0;
while (it.hasNext()) {
GpsSatellite oSat = (GpsSatellite) it.next();
if (oSat.usedInFix())
PhoneNumSat++;
}
}
if (event == GpsStatus.GPS_EVENT_FIRST_FIX)
PhoneFix = 1;
if (mListener != null)
mListener.onSensorsStateGPSStatusChange();
}
});
}
public void start() {
registerListeners();
}
public void startMagACC() {
m_sensorManager.registerListener(this, m_sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME);
m_sensorManager.registerListener(this, m_sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
public void stopMagACC() {
m_sensorManager.unregisterListener(this);
}
private void registerListeners() {
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
private void unregisterListeners() {
stopMagACC();
locationManager.removeUpdates(this);
}
public void stop() {
unregisterListeners();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, m_lastAccels, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, m_lastMagFields, 0, 3);
break;
default:
return;
}
computeOrientation();
}
private void computeOrientation() {
if (SensorManager.getRotationMatrix(m_rotationMatrix, null, m_lastAccels, m_lastMagFields)) {
SensorManager.getOrientation(m_rotationMatrix, m_orientation);
float yaw = (float) (Math.toDegrees(m_orientation[0]) + Declination);
float pitch = (float) Math.toDegrees(m_orientation[1]);
float roll = (float) Math.toDegrees(m_orientation[2]);
Heading = filterYaw.lowPass(yaw);
Pitch = filterPitch.lowPass(pitch);
Roll = filterRoll.lowPass(roll);
if (mListener != null)
mListener.onSensorsStateChangeMagAcc();
}
}
@Override
public void onLocationChanged(Location location) {
oldLocation = this.location;
this.location = location;
PhoneLatitude = location.getLatitude();
PhoneLongitude = location.getLongitude();
PhoneAltitude = location.getAltitude();
PhoneSpeed = location.getSpeed() * 100f;
PhoneAccuracy = location.getAccuracy() * 100f;
MapCurrentPosition = new LatLng(location.getLatitude(), location.getLongitude());
geoField = new GeomagneticField(Double.valueOf(location.getLatitude()).floatValue(), Double.valueOf(location.getLongitude()).floatValue(), Double.valueOf(location.getAltitude()).floatValue(), System.currentTimeMillis());
Declination = geoField.getDeclination();
if (mListener != null)
mListener.onSensorsStateGPSLocationChange();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public LatLng getNextPredictedLocationOnlineMap() {
if (location != null && oldLocation != null) {
int lat = (int) ((location.getLatitude() + (location.getLatitude() - oldLocation.getLatitude())));
int lon = (int) ((location.getLongitude() + (location.getLongitude() - oldLocation.getLongitude())));
return new LatLng(lat, lon);
} else
return new LatLng(0, 0);
}
}