@wemap/providers 4.0.0 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/debug/dist/index.html +1 -0
  2. package/debug/dist/turn-detection.html +19 -0
  3. package/debug/index.js +2 -0
  4. package/debug/src/AbsoluteAttitudeComponent.jsx +3 -10
  5. package/debug/src/StepDetectionComponent.jsx +3 -3
  6. package/debug/src/TurnDetectionComponent.jsx +47 -0
  7. package/debug/src/Utils.js +20 -0
  8. package/package.json +7 -9
  9. package/src/Providers.js +26 -81
  10. package/src/ProvidersInterface.js +5 -6
  11. package/src/errors/AskImuOnDesktopError.js +4 -3
  12. package/src/errors/ContainsIgnoredProviderError.js +4 -3
  13. package/src/errors/GeolocationApiMissingError.js +4 -3
  14. package/src/errors/GeolocationPermissionDeniedError.js +4 -3
  15. package/src/errors/GeolocationPositionUnavailableError.js +4 -3
  16. package/src/errors/IpResolveServerError.js +4 -3
  17. package/src/errors/MissingAccelerometerError.js +3 -3
  18. package/src/errors/MissingArCoreError.js +4 -3
  19. package/src/errors/MissingGyroscopeError.js +3 -3
  20. package/src/errors/MissingNativeInterfaceError.js +4 -3
  21. package/src/errors/MissingSensorError.js +4 -3
  22. package/src/errors/NoProviderFoundError.js +4 -3
  23. package/src/events/EventType.js +5 -1
  24. package/src/events/ProviderEvent.js +7 -0
  25. package/src/events/ProvidersLoggerOld.js +35 -34
  26. package/src/mapmatching/MapMatchingHandler.js +335 -69
  27. package/src/providers/FakeProvider.spec.js +15 -15
  28. package/src/providers/Provider.js +44 -27
  29. package/src/providers/Provider.spec.js +18 -29
  30. package/src/providers/attitude/TurnDectector.js +71 -0
  31. package/src/providers/attitude/absolute/AbsoluteAttitude.js +232 -0
  32. package/src/providers/attitude/absolute/{AbsoluteAttitudeFromBrowserProvider.js → AbsoluteAttitudeFromBrowser.js} +4 -4
  33. package/src/providers/attitude/relative/{RelativeAttitudeProvider.js → RelativeAttitude.js} +5 -8
  34. package/src/providers/attitude/relative/{RelativeAttitudeFromBrowserProvider.js → RelativeAttitudeFromBrowser.js} +4 -5
  35. package/src/providers/attitude/relative/{RelativeAttitudeFromEkfProvider.js → RelativeAttitudeFromEkf.js} +7 -8
  36. package/src/providers/attitude/relative/{RelativeAttitudeFromInertialProvider.js → RelativeAttitudeFromInertial.js} +24 -7
  37. package/src/providers/imu/{AccelerometerProvider.js → Accelerometer.js} +3 -3
  38. package/src/providers/imu/{GyroscopeProvider.js → Gyroscope.js} +3 -3
  39. package/src/providers/imu/HighRotationsDetector.js +62 -0
  40. package/src/providers/imu/{ImuProvider.js → Imu.js} +3 -3
  41. package/src/providers/inclination/{InclinationProvider.js → Inclination.js} +5 -6
  42. package/src/providers/inclination/{InclinationFromAccProvider.js → InclinationFromAcc.js} +4 -3
  43. package/src/providers/inclination/{InclinationFromRelativeAttitudeProvider.js → InclinationFromRelativeAttitude.js} +4 -3
  44. package/src/providers/legacy/helpers/ThugDetector.js +7 -7
  45. package/src/providers/others/{BarcodeProvider.js → Barcode.js} +3 -3
  46. package/src/providers/others/{CameraNativeProvider.js → CameraNative.js} +3 -3
  47. package/src/providers/others/{CameraProjectionMatrixProvider.js → CameraProjectionMatrix.js} +4 -4
  48. package/src/providers/position/absolute/AbsolutePosition.js +217 -0
  49. package/src/providers/position/absolute/{GnssWifiProvider.js → GnssWifi.js} +9 -8
  50. package/src/providers/position/absolute/{IpProvider.js → Ip.js} +2 -2
  51. package/src/providers/position/relative/{ArCoreProvider.js → ArCore.js} +28 -30
  52. package/src/providers/position/relative/{GeoRelativePositionProvider.js → GeoRelativePosition.js} +6 -6
  53. package/src/providers/position/relative/{GeoRelativePositionFromArCoreProvider.js → GeoRelativePositionFromArCore.js} +4 -5
  54. package/src/providers/position/relative/{PdrProvider.js → Pdr.js} +8 -8
  55. package/src/providers/steps/StepDetectionLadetto.js +12 -13
  56. package/src/providers/steps/StepDetectionMinMaxPeaks.js +19 -17
  57. package/src/providers/steps/StepDetectionMinMaxPeaks2.js +17 -18
  58. package/src/providers/steps/{StepDetectionProvider.js → StepDetector.js} +8 -7
  59. package/src/providers/steps/StraightLineDetector.js +80 -0
  60. package/src/smoothers/AttitudeSmoother.js +94 -59
  61. package/src/providers/MetaProvider.js +0 -44
  62. package/src/providers/attitude/absolute/AbsoluteAttitudeFromRelAttProvider.js +0 -132
  63. package/src/providers/attitude/absolute/AbsoluteAttitudeFusedProvider.js +0 -166
  64. package/src/providers/attitude/absolute/AbsoluteAttitudeProvider.js +0 -175
  65. package/src/providers/position/absolute/AbsolutePositionFromRelProvider.js +0 -107
  66. package/src/providers/position/absolute/AbsolutePositionProvider.js +0 -189
@@ -10,28 +10,26 @@ import EventType from '../../../events/EventType.js';
10
10
  import MissingArCoreError from '../../../errors/MissingArCoreError.js';
11
11
  import MissingNativeInterfaceError from '../../../errors/MissingNativeInterfaceError.js';
12
12
  import ProviderState from '../../ProviderState.js';
13
-
14
-
15
- const Payload = {
16
- Pose: {
17
- ref: 2 ** 0,
18
- size: 7
19
- },
20
- Barcode: {
21
- ref: 2 ** 1,
22
- size: 1
23
- },
24
- ProjMat: {
25
- ref: 2 ** 2,
26
- size: 16
27
- },
28
- ImageRef: {
29
- ref: 2 ** 3,
30
- size: 1
31
- }
32
- };
33
-
34
- class ArCoreProvider extends Provider {
13
+ class ArCore extends Provider {
14
+
15
+ static Payload = {
16
+ Pose: {
17
+ ref: 2 ** 0,
18
+ size: 7
19
+ },
20
+ Barcode: {
21
+ ref: 2 ** 1,
22
+ size: 1
23
+ },
24
+ ProjMat: {
25
+ ref: 2 ** 2,
26
+ size: 16
27
+ },
28
+ ImageRef: {
29
+ ref: 2 ** 3,
30
+ size: 1
31
+ }
32
+ };
35
33
 
36
34
  /**
37
35
  * default relative attitude drift in rad.second-1
@@ -97,7 +95,7 @@ class ArCoreProvider extends Provider {
97
95
 
98
96
  const time = TimeUtils.preciseTime() / 1e3;
99
97
 
100
- if (ref & Payload.Pose.ref) {
98
+ if (ref & ArCore.Payload.Pose.ref) {
101
99
 
102
100
  const attitude = new Attitude(
103
101
  payload.slice(bufferIndex, bufferIndex + 4),
@@ -117,18 +115,18 @@ class ArCoreProvider extends Provider {
117
115
  this.previousPosition = newPosition;
118
116
  events.push(this.createEvent(EventType.RelativePosition, position));
119
117
 
120
- bufferIndex += Payload.Pose.size;
118
+ bufferIndex += ArCore.Payload.Pose.size;
121
119
  }
122
120
 
123
- if (ref & Payload.Barcode.ref) {
121
+ if (ref & ArCore.Payload.Barcode.ref) {
124
122
  events.push(this.createEvent(EventType.Barcode, payload[bufferIndex]));
125
- bufferIndex += Payload.Barcode.size;
123
+ bufferIndex += ArCore.Payload.Barcode.size;
126
124
  }
127
125
 
128
- if (ref & Payload.ProjMat.ref) {
129
- const projMatrix = payload.slice(bufferIndex, bufferIndex + Payload.ProjMat.size);
126
+ if (ref & ArCore.Payload.ProjMat.ref) {
127
+ const projMatrix = payload.slice(bufferIndex, bufferIndex + ArCore.Payload.ProjMat.size);
130
128
  events.push(this.createEvent(EventType.CameraProjectionMatrix, projMatrix));
131
- bufferIndex += Payload.ProjMat.size;
129
+ bufferIndex += ArCore.Payload.ProjMat.size;
132
130
  }
133
131
 
134
132
  if (events.length !== 0) {
@@ -197,4 +195,4 @@ class ArCoreProvider extends Provider {
197
195
  }
198
196
  }
199
197
 
200
- export default ArCoreProvider;
198
+ export default new ArCore();
@@ -1,12 +1,12 @@
1
1
  import { PromiseUtils } from '@wemap/utils';
2
2
 
3
- import MetaProvider from '../../MetaProvider.js';
3
+ import Provider from '../../Provider.js';
4
4
  import EventType from '../../../events/EventType.js';
5
- import {
6
- Pdr, GeoRelativePositionFromArCore
7
- } from '../../../Providers.js';
5
+ import GeoRelativePositionFromArCore from './GeoRelativePositionFromArCore.js';
6
+ import Pdr from './Pdr.js';
8
7
 
9
- class GeoRelativePositionProvider extends MetaProvider {
8
+
9
+ class GeoRelativePosition extends Provider {
10
10
 
11
11
  /**
12
12
  * @override
@@ -65,4 +65,4 @@ class GeoRelativePositionProvider extends MetaProvider {
65
65
  }
66
66
  }
67
67
 
68
- export default GeoRelativePositionProvider;
68
+ export default new GeoRelativePosition();
@@ -1,12 +1,11 @@
1
1
  import { GeoRelativePosition } from '@wemap/geo';
2
2
 
3
3
  import EventType from '../../../events/EventType.js';
4
+ import AbsoluteAttitude from '../../attitude/absolute/AbsoluteAttitude.js';
4
5
  import Provider from '../../Provider.js';
5
- import {
6
- ArCore, AbsoluteAttitude
7
- } from '../../../Providers.js';
6
+ import ArCore from './ArCore.js';
8
7
 
9
- class GeoRelativePositionFromArCoreProvider extends Provider {
8
+ class GeoRelativePositionFromArCore extends Provider {
10
9
 
11
10
  /**
12
11
  * @override
@@ -89,4 +88,4 @@ class GeoRelativePositionFromArCoreProvider extends Provider {
89
88
  }
90
89
  }
91
90
 
92
- export default GeoRelativePositionFromArCoreProvider;
91
+ export default new GeoRelativePositionFromArCore();
@@ -3,11 +3,11 @@ import { deg2rad } from '@wemap/maths';
3
3
 
4
4
  import Provider from '../../Provider.js';
5
5
  import EventType from '../../../events/EventType.js';
6
- import {
7
- StepDetection, AbsoluteAttitude
8
- } from '../../../Providers.js';
6
+ import StepDetector from '../../steps/StepDetector.js';
7
+ import AbsoluteAttitude from '../../attitude/absolute/AbsoluteAttitude.js';
9
8
 
10
- class PdrProvider extends Provider {
9
+
10
+ class Pdr extends Provider {
11
11
 
12
12
  // https://ieeexplore.ieee.org/document/7346766
13
13
  misalignment = [1, 0, 0, 0];
@@ -32,7 +32,7 @@ class PdrProvider extends Provider {
32
32
  */
33
33
  get _availability() {
34
34
  return Promise.all([
35
- StepDetection.availability,
35
+ StepDetector.availability,
36
36
  AbsoluteAttitude.availability
37
37
  ]);
38
38
  }
@@ -43,7 +43,7 @@ class PdrProvider extends Provider {
43
43
  */
44
44
  start() {
45
45
 
46
- this.stepDetectionProviderId = StepDetection.addEventListener(
46
+ this.stepDetectionProviderId = StepDetector.addEventListener(
47
47
  events => this.onStepEvent(events[0]),
48
48
  error => this.notifyError(error)
49
49
  );
@@ -59,7 +59,7 @@ class PdrProvider extends Provider {
59
59
  * @override
60
60
  */
61
61
  stop() {
62
- StepDetection.removeEventListener(this.stepDetectionProviderId);
62
+ StepDetector.removeEventListener(this.stepDetectionProviderId);
63
63
  AbsoluteAttitude.removeEventListener(this.absoluteAttitudeProviderId);
64
64
  }
65
65
 
@@ -128,4 +128,4 @@ class PdrProvider extends Provider {
128
128
 
129
129
  }
130
130
 
131
- export default PdrProvider;
131
+ export default new Pdr();
@@ -1,20 +1,19 @@
1
+ class StepDetectionLadetto {
1
2
 
2
- // in seconds
3
- const MIN_TIME_BETWEEN_STEPS = 0.4;
4
-
5
- // in Hz
6
- const MAX_FRENQUENCY = 4;
7
- const MIN_FRENQUENCY = 1;
3
+ // in seconds
4
+ static MIN_TIME_BETWEEN_STEPS = 0.4;
8
5
 
9
- // in m.s-2
10
- const VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD = 1;
6
+ // in Hz
7
+ static MAX_FRENQUENCY = 4;
8
+ static MIN_FRENQUENCY = 1;
11
9
 
10
+ // in m.s-2
11
+ static VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD = 1;
12
12
 
13
- class StepDetectionLadetto {
14
13
 
15
14
  lastVerticalAcc = 0;
16
15
  maxAcceleration = 0;
17
- lastStepTimestamp = -MIN_TIME_BETWEEN_STEPS;
16
+ lastStepTimestamp = -StepDetectionLadetto.MIN_TIME_BETWEEN_STEPS;
18
17
 
19
18
  compute(timestamp, linearAcc) {
20
19
 
@@ -29,12 +28,12 @@ class StepDetectionLadetto {
29
28
  if (verticalAcc > this.maxAcceleration) {
30
29
  this.maxAcceleration = verticalAcc;
31
30
  }
32
- } else if (this.maxAcceleration > VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD
33
- && timeInterval > MIN_TIME_BETWEEN_STEPS) {
31
+ } else if (this.maxAcceleration > StepDetectionLadetto.VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD
32
+ && timeInterval > StepDetectionLadetto.MIN_TIME_BETWEEN_STEPS) {
34
33
  this.maxAcceleration = 0;
35
34
 
36
35
  const diffTime = timestamp - this.lastStepTimestamp;
37
- this.frequency = Math.min(Math.max((1 / diffTime), MIN_FRENQUENCY), MAX_FRENQUENCY);
36
+ this.frequency = Math.min(Math.max((1 / diffTime), StepDetectionLadetto.MIN_FRENQUENCY), StepDetectionLadetto.MAX_FRENQUENCY);
38
37
 
39
38
  stepDetected = true;
40
39
  this.lastStepTimestamp = timestamp;
@@ -1,38 +1,40 @@
1
1
 
2
- // in seconds
3
- const WINDOW_TIME = 0.3;
4
2
 
5
- // in seconds
6
- const MIN_TIME_BETWEEN_STEPS = 0.4;
3
+ class StepDetectionMinMaxPeaks {
7
4
 
8
- // in Hz
9
- const MAX_FRENQUENCY = 4;
10
- const MIN_FRENQUENCY = 1;
11
5
 
12
- // in m.s-2
13
- const VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD = 1.5;
14
- const VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD = -1;
6
+ // in seconds
7
+ static WINDOW_TIME = 0.3;
15
8
 
9
+ // in seconds
10
+ static MIN_TIME_BETWEEN_STEPS = 0.4;
11
+
12
+ // in Hz
13
+ static MAX_FRENQUENCY = 4;
14
+ static MIN_FRENQUENCY = 1;
15
+
16
+ // in m.s-2
17
+ static VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD = 1.5;
18
+ static VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD = -1;
16
19
 
17
- class StepDetectionMinMaxPeaks {
18
20
 
19
21
  constructor() {
20
22
  this.slidingWindow = [];
21
23
 
22
- this.lastStepTimestamp = -MIN_TIME_BETWEEN_STEPS;
24
+ this.lastStepTimestamp = -StepDetectionMinMaxPeaks.MIN_TIME_BETWEEN_STEPS;
23
25
  }
24
26
 
25
27
 
26
28
  compute(timestamp, linearAcc) {
27
29
 
28
- if (this.lastStepTimestamp && this.lastStepTimestamp + MIN_TIME_BETWEEN_STEPS > timestamp) {
30
+ if (this.lastStepTimestamp && this.lastStepTimestamp + StepDetectionMinMaxPeaks.MIN_TIME_BETWEEN_STEPS > timestamp) {
29
31
  return false;
30
32
  }
31
33
 
32
34
  let maxValue = Number.MIN_SAFE_INTEGER;
33
35
  let minValue = Number.MAX_SAFE_INTEGER;
34
36
 
35
- const windowTime = WINDOW_TIME;
37
+ const windowTime = StepDetectionMinMaxPeaks.WINDOW_TIME;
36
38
  this.slidingWindow.forEach(function(item, index, object) {
37
39
  if (item.timestamp < timestamp - windowTime) {
38
40
  object.splice(index, 1);
@@ -47,11 +49,11 @@ class StepDetectionMinMaxPeaks {
47
49
  });
48
50
 
49
51
 
50
- if (maxValue > VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD
51
- && minValue < VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD) {
52
+ if (maxValue > StepDetectionMinMaxPeaks.VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD
53
+ && minValue < StepDetectionMinMaxPeaks.VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD) {
52
54
 
53
55
  const timeInterval = this.lastStepTimestamp ? timestamp - this.lastStepTimestamp : 1;
54
- this.frequency = Math.min(Math.max((1 / timeInterval), MIN_FRENQUENCY), MAX_FRENQUENCY);
56
+ this.frequency = Math.min(Math.max((1 / timeInterval), StepDetectionMinMaxPeaks.MIN_FRENQUENCY), StepDetectionMinMaxPeaks.MAX_FRENQUENCY);
55
57
 
56
58
  this.lastStepTimestamp = timestamp;
57
59
  return true;
@@ -1,25 +1,24 @@
1
+ class StepDetectionMinMaxPeaks2 {
1
2
 
2
- // in seconds
3
- const WINDOW_TIME = 0.3;
4
-
5
- // in seconds
6
- const MIN_TIME_BETWEEN_STEPS = 0.4;
3
+ // in seconds
4
+ static WINDOW_TIME = 0.3;
7
5
 
8
- // in Hz
9
- const MAX_FRENQUENCY = 4;
10
- const MIN_FRENQUENCY = 1;
6
+ // in seconds
7
+ static MIN_TIME_BETWEEN_STEPS = 0.4;
11
8
 
12
- // in m.s-2
13
- const VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD = 0.75;
14
- const VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD = -0.3;
9
+ // in Hz
10
+ static MAX_FRENQUENCY = 4;
11
+ static MIN_FRENQUENCY = 1;
15
12
 
13
+ // in m.s-2
14
+ static VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD = 0.75;
15
+ static VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD = -0.3;
16
16
 
17
- class StepDetectionMinMaxPeaks2 {
18
17
 
19
18
  constructor() {
20
19
  this.slidingWindow = [];
21
20
 
22
- this.lastStepTimestamp = -MIN_TIME_BETWEEN_STEPS;
21
+ this.lastStepTimestamp = -StepDetectionMinMaxPeaks2.MIN_TIME_BETWEEN_STEPS;
23
22
  this.previousVerticalAcc = 0;
24
23
  this.influence = 0.2;
25
24
  }
@@ -35,7 +34,7 @@ class StepDetectionMinMaxPeaks2 {
35
34
  return false;
36
35
  }
37
36
 
38
- if (this.lastStepTimestamp && this.lastStepTimestamp + MIN_TIME_BETWEEN_STEPS > timestamp) {
37
+ if (this.lastStepTimestamp && this.lastStepTimestamp + StepDetectionMinMaxPeaks2.MIN_TIME_BETWEEN_STEPS > timestamp) {
39
38
  return false;
40
39
  }
41
40
 
@@ -43,7 +42,7 @@ class StepDetectionMinMaxPeaks2 {
43
42
  let minValue = Number.MAX_SAFE_INTEGER;
44
43
 
45
44
  this.slidingWindow.forEach(function(item, index, object) {
46
- if (item.timestamp < timestamp - WINDOW_TIME) {
45
+ if (item.timestamp < timestamp - StepDetectionMinMaxPeaks2.WINDOW_TIME) {
47
46
  object.splice(index, 1);
48
47
  } else {
49
48
  maxValue = Math.max(item.verticalAcc, maxValue);
@@ -56,11 +55,11 @@ class StepDetectionMinMaxPeaks2 {
56
55
  });
57
56
 
58
57
 
59
- if (maxValue > VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD
60
- && minValue < VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD) {
58
+ if (maxValue > StepDetectionMinMaxPeaks2.VERTICAL_ACC_POSITIVE_PEAK_THRESHOLD
59
+ && minValue < StepDetectionMinMaxPeaks2.VERTICAL_ACC_NEGATIVE_PEAK_THRESHOLD) {
61
60
 
62
61
  const timeInterval = this.lastStepTimestamp ? timestamp - this.lastStepTimestamp : 1;
63
- this.frequency = Math.min(Math.max((1 / timeInterval), MIN_FRENQUENCY), MAX_FRENQUENCY);
62
+ this.frequency = Math.min(Math.max((1 / timeInterval), StepDetectionMinMaxPeaks2.MIN_FRENQUENCY), StepDetectionMinMaxPeaks2.MAX_FRENQUENCY);
64
63
 
65
64
  this.lastStepTimestamp = timestamp;
66
65
  return true;
@@ -2,13 +2,14 @@ import { Constants as GeoConstants } from '@wemap/geo';
2
2
  import { Quaternion } from '@wemap/maths';
3
3
 
4
4
  import EventType from '../../events/EventType.js';
5
+ import RelativeAttitudeFromInertial from '../attitude/relative/RelativeAttitudeFromInertial.js';
6
+ import Accelerometer from '../imu/Accelerometer.js';
7
+ import Gyroscope from '../imu/Gyroscope.js';
5
8
  import Provider from '../Provider.js';
6
9
  import StepDetectionMinMaxPeaks2 from './StepDetectionMinMaxPeaks2.js';
7
- import {
8
- Accelerometer, Gyroscope, RelativeAttitudeFromInertial
9
- } from '../../Providers.js';
10
10
 
11
- class StepDetectionProvider extends Provider {
11
+
12
+ class StepDetector extends Provider {
12
13
 
13
14
  constructor() {
14
15
  super();
@@ -19,7 +20,7 @@ class StepDetectionProvider extends Provider {
19
20
  * @override
20
21
  */
21
22
  static get pname() {
22
- return 'StepDetection';
23
+ return 'StepDetector';
23
24
  }
24
25
 
25
26
  /**
@@ -86,7 +87,7 @@ class StepDetectionProvider extends Provider {
86
87
  const size = this.stepDetector.lastStepSize;
87
88
  this.numOfSteps++;
88
89
  this.notify(this.createEvent(
89
- EventType.STEP, {
90
+ EventType.Step, {
90
91
  size,
91
92
  number: this.numOfSteps
92
93
  },
@@ -103,4 +104,4 @@ class StepDetectionProvider extends Provider {
103
104
  }
104
105
  }
105
106
 
106
- export default StepDetectionProvider;
107
+ export default new StepDetector();
@@ -0,0 +1,80 @@
1
+ import Provider from '../Provider.js';
2
+ import StepDetector from './StepDetector.js';
3
+ import TurnDectector from '../attitude/TurnDectector.js';
4
+ import EventType from '../../events/EventType.js';
5
+
6
+ class StraightLineDetector extends Provider {
7
+
8
+ /** @type {number} */
9
+ static STEPS_CONSIDERED_FOR_STRAIGHT_LINE = 3;
10
+
11
+ /** @type {number?} */
12
+ _turnDetectorProviderId = null;
13
+
14
+ /** @type {number?} */
15
+ _stepDetectorProviderId = null;
16
+
17
+ /** @type {number} */
18
+ _countSteps = 0;
19
+
20
+ /**
21
+ * @override
22
+ */
23
+ static get pname() {
24
+ return 'StraightLineDetector';
25
+ }
26
+
27
+ /**
28
+ * @override
29
+ */
30
+ start() {
31
+ this._turnDetectorProviderId = TurnDectector.addEventListener(this._onTurn);
32
+ this._stepDetectorProviderId = StepDetector.addEventListener(this._onStep);
33
+ }
34
+
35
+ /**
36
+ * @override
37
+ */
38
+ stop() {
39
+ TurnDectector.removeEventListener(this._turnDetectorProviderId);
40
+ StepDetector.removeEventListener(this._stepDetectorProviderId);
41
+ }
42
+
43
+ _onTurn = (event) => {
44
+ if (this._countSteps >= StraightLineDetector.STEPS_CONSIDERED_FOR_STRAIGHT_LINE) {
45
+
46
+ const fromEvents = [event];
47
+ if (StepDetector.lastEvent !== null) {
48
+ fromEvents.push(StepDetector.lastEvent);
49
+ }
50
+
51
+ this.notify(this.createEvent(EventType.StraightLine, false, fromEvents));
52
+
53
+ }
54
+ this._countSteps = 0;
55
+ }
56
+
57
+ _onStep = (event) => {
58
+ this._countSteps++;
59
+
60
+ if (this._countSteps === StraightLineDetector.STEPS_CONSIDERED_FOR_STRAIGHT_LINE) {
61
+
62
+ const fromEvents = [event];
63
+ if (TurnDectector.lastEvent !== null) {
64
+ fromEvents.push(TurnDectector.lastEvent);
65
+ }
66
+ this.notify(this.createEvent(EventType.StraightLine, true, fromEvents));
67
+
68
+ }
69
+
70
+ }
71
+
72
+ /**
73
+ * @returns {boolean}
74
+ */
75
+ isStraight() {
76
+ return this._countSteps >= StraightLineDetector.STEPS_CONSIDERED_FOR_STRAIGHT_LINE;
77
+ }
78
+ }
79
+
80
+ export default new StraightLineDetector();