-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathDeviceLocationProvider.cs
346 lines (282 loc) · 12.9 KB
/
DeviceLocationProvider.cs
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
namespace Mapbox.Unity.Location
{
using System.Collections;
using UnityEngine;
using Mapbox.Utils;
using Mapbox.CheapRulerCs;
using System;
using System.Linq;
/// <summary>
/// The DeviceLocationProvider is responsible for providing real world location and heading data,
/// served directly from native hardware and OS.
/// This relies on Unity's <see href="https://docs.unity3d.com/ScriptReference/LocationService.html">LocationService</see> for location
/// and <see href="https://docs.unity3d.com/ScriptReference/Compass.html">Compass</see> for heading.
/// </summary>
public class DeviceLocationProvider : AbstractLocationProvider
{
/// <summary>
/// Using higher value like 500 usually does not require to turn GPS chip on and thus saves battery power.
/// Values like 5-10 could be used for getting best accuracy.
/// </summary>
[SerializeField]
[Tooltip("Using higher value like 500 usually does not require to turn GPS chip on and thus saves battery power. Values like 5-10 could be used for getting best accuracy.")]
public float _desiredAccuracyInMeters = 1.0f;
/// <summary>
/// The minimum distance (measured in meters) a device must move laterally before Input.location property is updated.
/// Higher values like 500 imply less overhead.
/// </summary>
[SerializeField]
[Tooltip("The minimum distance (measured in meters) a device must move laterally before Input.location property is updated. Higher values like 500 imply less overhead.")]
public float _updateDistanceInMeters = 0.0f;
[SerializeField]
[Tooltip("The minimum time interval between location updates, in milliseconds. It's reasonable to not go below 500ms.")]
public long _updateTimeInMilliSeconds = 500;
[SerializeField]
[Tooltip("Smoothing strategy to be applied to the UserHeading.")]
public AngleSmoothingAbstractBase _userHeadingSmoothing;
[SerializeField]
[Tooltip("Smoothing strategy to applied to the DeviceOrientation.")]
public AngleSmoothingAbstractBase _deviceOrientationSmoothing;
[Serializable]
public struct DebuggingInEditor
{
[Header("Set 'EditorLocationProvider' to 'DeviceLocationProvider' and connect device with UnityRemote.")]
[SerializeField]
[Tooltip("Mock Unity's 'Input.Location' to route location log files through this class (eg fresh calculation of 'UserHeading') instead of just replaying them. To use set 'Editor Location Provider' in 'Location Factory' to 'Device Location Provider' and select a location log file below.")]
public bool _mockUnityInputLocation;
[SerializeField]
[Tooltip("Also see above. Location log file to mock Unity's 'Input.Location'.")]
public TextAsset _locationLogFile;
}
[Space(20)]
public DebuggingInEditor _editorDebuggingOnly;
private IMapboxLocationService _locationService;
private Coroutine _pollRoutine;
private double _lastLocationTimestamp;
private WaitForSeconds _wait1sec;
private WaitForSeconds _waitUpdateTime;
/// <summary>list of positions to keep for calculations</summary>
private CircularBuffer<Vector2d> _lastPositions;
/// <summary>number of last positons to keep</summary>
private int _maxLastPositions = 5;
/// <summary>minimum needed distance between oldest and newest position before UserHeading is calculated</summary>
private double _minDistanceOldestNewestPosition = 1.5;
// Android 6+ permissions have to be granted during runtime
// these are the callbacks for requesting location permission
// TODO: show message to users in case they accidentallly denied permission
#if UNITY_ANDROID
private bool _gotPermissionRequestResponse = false;
private void OnAllow() { _gotPermissionRequestResponse = true; }
private void OnDeny() { _gotPermissionRequestResponse = true; }
private void OnDenyAndNeverAskAgain() { _gotPermissionRequestResponse = true; }
#endif
protected virtual void Awake()
{
#if UNITY_EDITOR
if (_editorDebuggingOnly._mockUnityInputLocation)
{
if (null == _editorDebuggingOnly._locationLogFile || null == _editorDebuggingOnly._locationLogFile.bytes)
{
throw new ArgumentNullException("Location Log File");
}
_locationService = new MapboxLocationServiceMock(_editorDebuggingOnly._locationLogFile.bytes);
}
else
{
#endif
_locationService = new MapboxLocationServiceUnityWrapper();
#if UNITY_EDITOR
}
#endif
_currentLocation.Provider = "unity";
_wait1sec = new WaitForSeconds(1f);
_waitUpdateTime = _updateTimeInMilliSeconds < 500 ? new WaitForSeconds(0.5f) : new WaitForSeconds((float)_updateTimeInMilliSeconds / 1000.0f);
if (null == _userHeadingSmoothing) { _userHeadingSmoothing = transform.gameObject.AddComponent<AngleSmoothingNoOp>(); }
if (null == _deviceOrientationSmoothing) { _deviceOrientationSmoothing = transform.gameObject.AddComponent<AngleSmoothingNoOp>(); }
_lastPositions = new CircularBuffer<Vector2d>(_maxLastPositions);
if (_pollRoutine == null)
{
_pollRoutine = StartCoroutine(PollLocationRoutine());
}
}
/// <summary>
/// Enable location and compass services.
/// Sends continuous location and heading updates based on
/// _desiredAccuracyInMeters and _updateDistanceInMeters.
/// </summary>
/// <returns>The location routine.</returns>
IEnumerator PollLocationRoutine()
{
#if UNITY_EDITOR
while (!UnityEditor.EditorApplication.isRemoteConnected)
{
// exit if we are not the selected location provider
if (null != LocationProviderFactory.Instance && null != LocationProviderFactory.Instance.DefaultLocationProvider)
{
if (!this.Equals(LocationProviderFactory.Instance.DefaultLocationProvider))
{
yield break;
}
}
Debug.LogWarning("Remote device not connected via 'Unity Remote'. Waiting ..." + Environment.NewLine + "If Unity seems to be stuck here make sure 'Unity Remote' is running and restart Unity with your device already connected.");
yield return _wait1sec;
}
#endif
//request runtime fine location permission on Android if not yet allowed
#if UNITY_ANDROID
if (!_locationService.isEnabledByUser)
{
UniAndroidPermission.RequestPermission(AndroidPermission.ACCESS_FINE_LOCATION);
//wait for user to allow or deny
while (!UniAndroidPermission.IsPermitted(AndroidPermission.ACCESS_FINE_LOCATION)) { yield return _wait1sec; }
}
#endif
if (!_locationService.isEnabledByUser)
{
Debug.LogError("DeviceLocationProvider: Location is not enabled by user!");
_currentLocation.IsLocationServiceEnabled = false;
SendLocation(_currentLocation);
yield break;
}
_currentLocation.IsLocationServiceInitializing = true;
_locationService.Start(_desiredAccuracyInMeters, _updateDistanceInMeters);
Input.compass.enabled = true;
int maxWait = 20;
while (_locationService.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return _wait1sec;
maxWait--;
}
if (maxWait < 1)
{
Debug.LogError("DeviceLocationProvider: " + "Timed out trying to initialize location services!");
_currentLocation.IsLocationServiceInitializing = false;
_currentLocation.IsLocationServiceEnabled = false;
SendLocation(_currentLocation);
yield break;
}
if (_locationService.status == LocationServiceStatus.Failed)
{
Debug.LogError("DeviceLocationProvider: " + "Failed to initialize location services!");
_currentLocation.IsLocationServiceInitializing = false;
_currentLocation.IsLocationServiceEnabled = false;
SendLocation(_currentLocation);
yield break;
}
_currentLocation.IsLocationServiceInitializing = false;
_currentLocation.IsLocationServiceEnabled = true;
#if UNITY_EDITOR
// HACK: this is to prevent Android devices, connected through Unity Remote,
// from reporting a location of (0, 0), initially.
yield return _wait1sec;
#endif
System.Globalization.CultureInfo invariantCulture = System.Globalization.CultureInfo.InvariantCulture;
while (true)
{
var lastData = _locationService.lastData;
var timestamp = lastData.timestamp;
///////////////////////////////
// oh boy, Unity what are you doing???
// on some devices it seems that
// Input.location.status != LocationServiceStatus.Running
// nevertheless new location is available
//////////////////////////////
//Debug.LogFormat("Input.location.status: {0}", Input.location.status);
_currentLocation.IsLocationServiceEnabled =
_locationService.status == LocationServiceStatus.Running
|| timestamp > _lastLocationTimestamp;
_currentLocation.IsUserHeadingUpdated = false;
_currentLocation.IsLocationUpdated = false;
if (!_currentLocation.IsLocationServiceEnabled)
{
yield return _waitUpdateTime;
continue;
}
// device orientation, user heading get calculated below
_deviceOrientationSmoothing.Add(Input.compass.trueHeading);
_currentLocation.DeviceOrientation = (float)_deviceOrientationSmoothing.Calculate();
//_currentLocation.LatitudeLongitude = new Vector2d(lastData.latitude, lastData.longitude);
// HACK to get back to double precision, does this even work?
// https://forum.unity.com/threads/precision-of-location-longitude-is-worse-when-longitude-is-beyond-100-degrees.133192/#post-1835164
double latitude = double.Parse(lastData.latitude.ToString("R", invariantCulture), invariantCulture);
double longitude = double.Parse(lastData.longitude.ToString("R", invariantCulture), invariantCulture);
Vector2d previousLocation = new Vector2d(_currentLocation.LatitudeLongitude.x, _currentLocation.LatitudeLongitude.y);
_currentLocation.LatitudeLongitude = new Vector2d(latitude, longitude);
_currentLocation.Accuracy = (float)Math.Floor(lastData.horizontalAccuracy);
// sometimes Unity's timestamp doesn't seem to get updated, or even jump back in time
// do an additional check if location has changed
_currentLocation.IsLocationUpdated = timestamp > _lastLocationTimestamp || !_currentLocation.LatitudeLongitude.Equals(previousLocation);
_currentLocation.Timestamp = timestamp;
_lastLocationTimestamp = timestamp;
if (_currentLocation.IsLocationUpdated)
{
if (_lastPositions.Count > 0)
{
// only add position if user has moved +1m since we added the previous position to the list
CheapRuler cheapRuler = new CheapRuler(_currentLocation.LatitudeLongitude.x, CheapRulerUnits.Meters);
Vector2d p = _currentLocation.LatitudeLongitude;
double distance = cheapRuler.Distance(
new double[] { p.y, p.x },
new double[] { _lastPositions[0].y, _lastPositions[0].x }
);
if (distance > 1.0)
{
_lastPositions.Add(_currentLocation.LatitudeLongitude);
}
}
else
{
_lastPositions.Add(_currentLocation.LatitudeLongitude);
}
}
// if we have enough positions calculate user heading ourselves.
// Unity does not provide bearing based on GPS locations, just
// device orientation based on Compass.Heading.
// nevertheless, use compass for intial UserHeading till we have
// enough values to calculate ourselves.
if (_lastPositions.Count < _maxLastPositions)
{
_currentLocation.UserHeading = _currentLocation.DeviceOrientation;
_currentLocation.IsUserHeadingUpdated = true;
}
else
{
Vector2d newestPos = _lastPositions[0];
Vector2d oldestPos = _lastPositions[_maxLastPositions - 1];
CheapRuler cheapRuler = new CheapRuler(newestPos.x, CheapRulerUnits.Meters);
// distance between last and first position in our buffer
double distance = cheapRuler.Distance(
new double[] { newestPos.y, newestPos.x },
new double[] { oldestPos.y, oldestPos.x }
);
// positions are minimum required distance apart (user is moving), calculate user heading
if (distance >= _minDistanceOldestNewestPosition)
{
float[] lastHeadings = new float[_maxLastPositions - 1];
for (int i = 1; i < _maxLastPositions; i++)
{
// atan2 increases angle CCW, flip sign of latDiff to get CW
double latDiff = -(_lastPositions[i].x - _lastPositions[i - 1].x);
double lngDiff = _lastPositions[i].y - _lastPositions[i - 1].y;
// +90.0 to make top (north) 0°
double heading = (Math.Atan2(latDiff, lngDiff) * 180.0 / Math.PI) + 90.0f;
// stay within [0..360]° range
if (heading < 0) { heading += 360; }
if (heading >= 360) { heading -= 360; }
lastHeadings[i - 1] = (float)heading;
}
_userHeadingSmoothing.Add(lastHeadings[0]);
float finalHeading = (float)_userHeadingSmoothing.Calculate();
//fix heading to have 0° for north, 90° for east, 180° for south and 270° for west
finalHeading = finalHeading >= 180.0f ? finalHeading - 180.0f : finalHeading + 180.0f;
_currentLocation.UserHeading = finalHeading;
_currentLocation.IsUserHeadingUpdated = true;
}
}
_currentLocation.TimestampDevice = UnixTimestampUtils.To(DateTime.UtcNow);
SendLocation(_currentLocation);
yield return _waitUpdateTime;
}
}
}
}