-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock-pilot.js
246 lines (218 loc) · 6.4 KB
/
mock-pilot.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
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
/*********************************
Autopilot communication methods.
********************************
This is where communication with the autopilot device
is managed.
Update the sections below as required to for the autopilot
device in use.
**********************************************************/
/***********************************************************
* autopilot type identifier - set a value that is unique.
* The value must be valid for use in URI path as it is
* used to target commands to a specific device.
*
* e.g. * AP_TYPE= 'mypilot'
*
* usage "./autopilots/mypilot/*"
************************************************************/
const AP_TYPE = 'mockPilotSK'
/** Available autopilot device options.
* These are returned to the API.
* See API documentation.
*/
const AP_OPTIONS = {
states: [
{name: 'on', engaged: true},
{name: 'off', engaged: false}
],
modes: ['compass','wind','route', 'gps']
}
/***********************************************************
* Define the states to use for engage / disengage commands
***********************************************************/
const defaultState = {
engaged: 'on',
disengaged: 'off'
}
// device status
const apStatus = {
state: null,
mode: null,
engaged: false,
target: null
}
module.exports = function(app) {
let pilot = {type: AP_TYPE }
let preDodgeMode
pilot.start = () => {
app.debug(`**** Intialising autopilot device (${pilot.type}) *****`)
// connect to autopilot
try {
// connect to autopilot and update apStatus
apStatus.state= defaultState.disengaged
apStatus.mode = 'compass'
} catch (err) {
app.debug(`**** ERROR connecting to autopilot device (${pilot.type}) *****`)
app.debug(err)
setState('off-line')
} finally {
sendUpdateToSignalK()
raiseAlarm()
}
}
pilot.stop = () => {
app.debug('**** stopping mock autopilot *****')
// clean up here
}
pilot.status = () => {
return {options: {...AP_OPTIONS}, ...apStatus}
}
setState = (value) => {
if (apStatus.target === null) {
apStatus.target = 0
}
apStatus.state = value
apStatus.engaged = defaultState.engaged === value ? true : false
}
pilot.setState = (value) => {
// Check for valid state value
if ( AP_OPTIONS.states.filter( i => i.name === value).length === 0 ) {
throw new Error(`Invalid state: ${value}`)
} else {
setState(value)
sendUpdateToSignalK()
return
}
}
pilot.engage = () => {
// Determine the state to set when engage is requested
setState(defaultState.engaged)
sendUpdateToSignalK()
return
}
pilot.disengage = () => {
// Determine the state to set when dis-engage is requested
setState(defaultState.disengaged)
sendUpdateToSignalK()
return
}
pilot.setMode = (value) => {
// Check for valid mode value
if ( !AP_OPTIONS.modes.includes(value)) {
throw new Error(`Invalid mode: ${value}`)
} else {
apStatus.mode = value
sendUpdateToSignalK()
return
}
}
/**********************************************
* Update function for target autopilot device
***********************************************/
pilot.setTarget = (value) => {
// validate value against current mode, throw if invalid.
if (value < 0 && apStatus.mode !== 'wind') {
const errMsg = `I** ERROR: nvalid value ${value} for current mode ${apStatus.mode}`
console.log(errMsg)
throw new Error(errMsg)
}
apStatus.target = value
sendUpdateToSignalK()
return
}
/**********************
* Adjust target value
***********************/
pilot.adjustTarget = (value) => {
// validate resultant target value against current mode, throw if invalid.
const v = apStatus.target + value
const errMsg = `** ERROR: Value ${v} out of bounds for current mode ${apStatus.mode}`
if (apStatus.mode === 'wind') {
if (v > Math.PI || v < (0 - Math.PI)) {
console.log(errMsg)
throw new Error(errMsg)
}
} else {
if (v > (2 * Math.PI) || v < 0) {
console.log(errMsg)
throw new Error(errMsg)
}
}
apStatus.target = v
sendUpdateToSignalK()
return
}
/**********************
* Dodge mode operation
***********************/
pilot.dodge = (value) => {
// determine operation.
if (typeof value === 'number') {
if (apStatus.mode !== 'dodge') {
preDodgeMode = apStatus.mode
app.debug(`** Enter Dodge Mode (${value}) <-`, preDodgeMode)
apStatus.mode = 'dodge'
}
} else {
app.debug('** Exit Dodge Mode ->', preDodgeMode)
apStatus.mode = preDodgeMode || apStatus.mode
preDodgeMode = null
}
sendUpdateToSignalK()
return
}
/*****************************
* Autopilot provider settings
******************************/
pilot.properties = () => {
return {
properties: {}
}
}
/***************************************
* Send update from Autopilot to SKserver
****************************************/
const sendUpdateToSignalK = () => {
// pilot.state should be set to 'off-line' if device is unavailable
app.autopilotUpdate(pilot.type, {
target: typeof apStatus.target === 'number' ? apStatus.target : null,
mode: apStatus.mode,
state: apStatus.state,
engaged: apStatus.engaged
})
}
// Raise alarm message
const raiseAlarm = () => {
setTimeout(
() => {
const alarm = {
alarm: {
path: 'waypointArrival',
value: {
state: 'alert',
method: ['visual'],
message: 'Soon to be here....'
}
}
}
app.autopilotUpdate(pilot.type, alarm)
}, Math.random() * 20000
)
}
/**********************************************
* Normalise incoming alarm name from Autopilot
***********************************************/
const normaliseAlarm = (alarmId) => {
/** AP device alarm text mapped to Autopilot API alarm ids */
AP_ALARMS = {
'WP Arrival': 'waypointArrival',
'Pilot Way Point Advance': 'waypointAdvance',
'Pilot Route Complete': 'routeComplete',
'XTE Alarm': 'xte',
'Heading Drift Alarm': 'heading',
'Wind Alarm': 'wind'
}
return !(alarmId in AP_ALARMS) ? 'unknown' : AP_ALARMS[alarmId]
}
return pilot
}