-
Notifications
You must be signed in to change notification settings - Fork 3
/
StateMachine.js
159 lines (148 loc) · 6.11 KB
/
StateMachine.js
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
const { Point } = require('where');
const CircularBuffer = require('circular-buffer');
const debug = require('debug')('signalk-autostate:statemachine:update');
const debugFallback = require('debug')('signalk-autostate:statemachine:fallback');
const moored = 'moored';
const anchored = 'anchored';
const sailing = 'sailing';
const motoring = 'motoring';
class StateMachine {
constructor(positionUpdateMinutes = 10, underWayThresholdMeters = 100, defaultPropulsion = 'sailing', motorStoppedSpeed = 0) {
this.stateChangeTime = null;
this.stateChangePosition = null;
this.positions = new CircularBuffer(positionUpdateMinutes + 1);
this.lastState = null;
this.positionUpdateMinutes = positionUpdateMinutes;
this.underWayThresholdMeters = underWayThresholdMeters;
this.defaultPropulsion = defaultPropulsion;
this.currentPropulsion = defaultPropulsion;
this.motorStoppedSpeed = motorStoppedSpeed;
this.currentSpeed = 0;
}
setState(state, update) {
if (state !== this.lastState) {
debug(`State has changed from ${this.lastState} to ${state}`);
this.stateChangeTime = update.time;
if (update.path === 'navigation.position') {
this.setPosition(update.value);
}
this.lastState = state;
} else if (state === sailing || state === motoring) {
this.stateChangeTime = update.time;
if (update.path === 'navigation.position') {
this.setPosition(update.value);
}
}
return state;
}
setPosition(position) {
debug(`Set state position to ${position}`);
this.stateChangePosition = position;
}
switchMotoringSailing(newPropulsion, update) {
const oldPropulsion = this.currentPropulsion;
if (oldPropulsion === newPropulsion) {
return this.lastState;
}
this.currentPropulsion = newPropulsion;
if (this.lastState === motoring && this.currentSpeed <= this.motorStoppedSpeed) {
// Special-case when motor is stopped and speed is zero
debug(`Motor stopped while speed is ${this.currentSpeed}, assuming moored`);
return this.setState(moored, update);
}
if (this.lastState === motoring || this.lastState === sailing) {
// Under way, switch state to new propulsion method
debug(`Under way and switched from ${oldPropulsion} to ${newPropulsion}`);
return this.setState(newPropulsion, update);
}
return this.lastState;
}
update(update) {
if (update.path === 'navigation.speedOverGround') {
this.currentSpeed = update.value;
}
if (update.path === 'navigation.anchor.position') {
if (update.value) {
// anchor position has a value, we have dropped the anchor
return this.setState(anchored, update);
}
// With null value the anchor is hoisted
return this.setState(this.currentPropulsion, update);
}
if (update.path.match(/propulsion\.([A-Za-z0-9]+)\.state/)) {
if (update.value === 'started') {
return this.switchMotoringSailing(motoring, update);
}
return this.switchMotoringSailing(this.defaultPropulsion, update);
}
if (update.path.match(/propulsion\.([A-Za-z0-9]+)\.revolutions/)) {
if (update.value > 0) {
return this.switchMotoringSailing(motoring, update);
}
return this.switchMotoringSailing(this.defaultPropulsion, update);
}
if (update.path === 'navigation.position' && this.lastState !== anchored) {
// inHarbour we have moved less than 100 meters in 10 minutes
const positionUpdate = {
time: update.time,
path: update.path,
value: new Point(update.value.latitude, update.value.longitude),
speed: this.currentSpeed,
};
if (this.positions.size() > 0) {
// Ensure that a minute has elapsed
if ((positionUpdate.time - this.positions.get(0).time) / 1000 < 60) {
return this.lastState;
}
}
this.positions.enq(positionUpdate);
if (!this.stateChangeTime) {
debug(`First state change ${positionUpdate.value} ${positionUpdate.time}`);
return this.setState(moored, positionUpdate);
}
if (this.positions.size() < this.positionUpdateMinutes
&& (positionUpdate.time - this.stateChangeTime) / 60000 < this.positionUpdateMinutes) {
debugFallback(`Only ${Math.round((positionUpdate.time - this.stateChangeTime) / 60000)} minutes elapsed since last state change, returning old state`);
return this.lastState;
}
const distance = this.positions.toarray().reduce((d, u, idx, arr) => {
if (idx === 0) {
// Skip first entry as we're counting distances
return d;
}
if (Math.round((positionUpdate.time - u.time) / 60000) > this.positionUpdateMinutes) {
// Stale entry
return d;
}
const previous = arr[idx - 1];
const elapsed = (previous.time - u.time) / 1000;
const dist = previous.value.distanceTo(u.value) * 1000;
return {
dist: d.dist + dist,
time: d.time + elapsed,
speed: d.speed,
};
}, {
dist: 0,
time: 0,
speed: 0,
});
if (distance.time && distance.dist) {
distance.speed = distance.dist / distance.time;
}
if (distance.dist < this.underWayThresholdMeters) {
debug(`Has only moved ${Math.round(distance.dist)} meters in ${Math.round(distance.time / 60)} minutes (${distance.speed.toFixed(2)}m/s)`);
return this.setState(moored, positionUpdate);
}
if (this.lastState === 'moored' && this.currentSpeed === 0 && (positionUpdate.time - this.stateChangeTime) / 60000 < 10) {
debug(`Has moved > ${this.underWayThresholdMeters}m (${Math.round(distance.dist)} meters) but speed is zero, assuming staying moored`);
return this.lastState;
}
// If we are not in harbour we are sailing or motoring
debug(`Has moved > ${this.underWayThresholdMeters}m (${Math.round(distance.dist)} meters in ${Math.round(distance.time / 60)} minutes, ${distance.speed.toFixed(2)}m/s)`);
return this.setState(this.currentPropulsion, positionUpdate);
}
return this.lastState;
}
}
module.exports = StateMachine;