-
Notifications
You must be signed in to change notification settings - Fork 0
/
myn2kpilot.js
406 lines (352 loc) · 11.7 KB
/
myn2kpilot.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
*********************************
Autopilot communication methods.
*********************************
This is where communication with the autopilot device is managed.
*************************************************************
Update the sections with values relevant to the autopilot
device as per the comments below.
*************************************************************
*/
/*****************************************************************
* 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. * apType= 'mypilot'
*
* POST "./steering/autopilot/mypilotid/engage"
* ***************************************************************/
const apType = 'myN2kPilot'
/***********************************************************
* Define the text used to discover the autopilot device.
* The specify the string to match (startsWith) in the
* n2k.hardwareVersion attribute of entries found under the
* /signalk/sources path.
* e.g. 'Raymarine EV-1 Course Computer'
***********************************************************/
const apDeviceSearchText = 'Raymarine EV-1 Course Computer'
/*********************************************************
* Define the PGNs to be processed from incoming messages.
* Processed by onStreamEvent() method.
*********************************************************/
const pgns = [
65345, 65360, 65379,
65288,
127237
]
/** Alarm text to map to Autopilot API alarm notifications */
apAlarms = ['WP Arrival','Pilot Way Point Advance','Pilot Route Complete']
/** Available autopilot device options.
* These are returned to the API.
* See API documentation.
*/
const apOptions = {
states: [
{name: 'auto', engaged: true},
{name: 'wind', engaged: true},
{name: 'route', engaged: true},
{name: 'standby', engaged: false}
],
modes: []
}
/***********************************************************
* Define the states to use for engage / disengage commands
***********************************************************/
const defaultState = {
engaged: 'auto',
disengaged: 'standby'
}
/*******************************************
* Replace these example N2K command values
******************************************/
const n2k_commands = {
state: { // Should be one entry for each name entry in apOptions.states.
"auto": "%s,3,126208,%s,%s,17,01,63,ff,00,f8,04,01,3b,07,03,04,04,40,00,05,ff,ff",
"wind": "%s,3,126208,%s,%s,17,01,63,ff,00,f8,04,01,3b,07,03,04,04,00,01,05,ff,ff",
"route": "%s,3,126208,%s,%s,17,01,63,ff,00,f8,04,01,3b,07,03,04,04,80,01,05,ff,ff",
"standby": "%s,3,126208,%s,%s,17,01,63,ff,00,f8,04,01,3b,07,03,04,04,00,00,05,ff,ff"
},
mode: "%s,3,126208,%s,%s,17,01,63,ff,00,f8,04,01,3b,07,03,04,04,00,00,05,ff,ff",
heading: "%s,3,126208,%s,%s,14,01,50,ff,00,f8,03,01,3b,07,03,04,06,%s,%s",
input: "%s,7,126720,%s,%s,22,3b,9f,f0,81,86,21,%s,ff,ff,ff,ff,ff,c1,c2,cd,66,80,d3,42,b1,c8",
input_keys : {
"+1": "07,f8",
"+10": "08,f7",
"-1": "05,fa",
"-10": "06,f9",
"-1-10": "21,de",
"+1+10": "22,dd"
}
}
const util = require('util')
const _ = require('lodash')
const default_src = '1'
const autopilot_dst = '204' // fallback N2K destination id
const everyone_dst = '255'
let deviceId // target device id
let discovered = false // true if target device discovered
const apStatus = { // device status
state: null,
mode: null,
engaged: null,
target: null
}
module.exports = function(app) {
let pilot = {id: null, type: apType }
pilot.start = (props) => {
deviceId = props.deviceId
app.debug('props.deviceId =', props.deviceId)
app.debug('deviceId =', deviceId)
app.debug('**** intialise n2k stream listener *****')
app.on('N2KAnalyzerOut', onStreamEvent)
}
pilot.stop = () => {
}
pilot.status = () => {
return {...apOptions, ...apStatus}
}
pilot.setState = (value) => {
if ( !n2k_commands.state[value] ) {
throw new Error(`Invalid state: ${value}`)
} else {
let msg = util.format(n2k_commands.state[value], (new Date()).toISOString(), default_src, deviceId)
sendN2k([msg])
return !n2k_commands.state[value][engaged]
}
}
pilot.engage = () => {
return setState(defaultState.engaged)
}
pilot.disengage = () => {
return setState(defaultState.disengaged)
}
/**********************************************
* Update function for target autopilot device
***********************************************/
pilot.setTarget = (value) => {
// Check autopilot is in the appropriate state.
if (!n2k_commands.state[apStatus.state][engaged]) {
throw new Error(`Autopilot current state (${apState} does not support this operation!`)
} else { // ** example **
const new_value = Math.trunc(value * 10000)
const msg = util.format(
n2k_commands.heading,
(new Date()).toISOString(),
default_src,
autopilot_dst,
padd((new_value & 0xff).toString(16), 2),
padd(((new_value >> 8) & 0xff).toString(16), 2)
)
sendN2k([msg])
return
}
}
/**********************************************
* Update function for target autopilot device
***********************************************/
pilot.adjustTarget = (value) => {
// Check autopilot is in the appropriate state.
if (!n2k_commands.state[apStatus.state][engaged]) {
throw new Error(`Autopilot current state (${apStatus.state} does not support this operation!`)
} else { // ** example **
let aString
switch (value) {
case 10:
aString = '+10'
break
case -10:
aString = '-10'
break
case 1:
aString = '+1'
break
case -1:
aString = '-1'
break
default:
throw new Error(`Invalid adjustment: ${value}`)
}
sendN2k(
[util.format(
n2k_commands.input,
(new Date()).toISOString(),
default_src,
everyone_dst,
n2k_commands.input_keys[aString])]
)
return
}
}
/*****************************
* Autopilot device discovery
******************************/
pilot.properties = () => {
let discId = deviceId ?? autopilot_dst
let description = ''
app.debug('***pre-discovery -> discId', discId)
if (!discovered) {
const sources = app.getPath('/sources')
if ( sources ) {
_.values(sources).forEach(v => {
if ( typeof v === 'object' ) {
_.keys(v).forEach(id => {
if ( v[id] && v[id].n2k && v[id].n2k.hardwareVersion && v[id].n2k.hardwareVersion
.startsWith(apDeviceSearchText) ) {
discId = id
discovered = true
}
})
}
})
}
}
if (discovered) {
deviceId = discId
description = `Discovered autopilot device with id ${discId}`
app.debug(description)
} else {
description = `No device found! Using ID: ${discId}.`
app.debug(description)
}
app.debug('*** post-discovery -> deviceId', deviceId)
return {
properties: {
deviceId: {
type: "string",
title: "Autopilot NMEA2000 id.",
description,
default: deviceId
}
}
}
}
/***********************************
* NMEA2000 stream event handler.
* Parse NMEA2000 stream input
*
* Update to process target PGNs
* in the required maanner.
* *********************************/
const onStreamEvent = (evt) => {
if (!pgns.includes(evt.pgn) || String(evt.src) !== deviceId) {
return
}
// 127237 `Heading / Track control (Rudder, etc.)`
if (evt.pgn === 127237) {
//app.debug('n2k pgn=', evt.pgn, evt.fields, evt.description)
}
// 65288 = notifications.autopilot.<alarmName>
if (evt.pgn === 65288) {
if (evt.fields['Manufacturer Code'] !== 'Raymarine'
|| typeof evt.fields['Alarm Group'] === 'Autopilot'
|| typeof evt.fields['Alarm Status'] === 'undefined') {
return
}
const method = [ 'visual' ]
let state = evt.fields['Alarm Status']
if ( state === 'Alarm condition met and not silenced' ) {
method.push('sound')
}
if ( state === 'Alarm condition not met' ) {
state = 'normal'
} else {
state = 'alarm'
}
let alarmId = evt.fields['Alarm ID']
if ( typeof alarmId !== 'string' ) {
alarmId = `Unknown Alarm ${alarmId}`
} else if (
state === 'alarm' &&
apAlarms.includes(alarmId)
) {
state = 'alert'
}
// normalise alarm name
let alarmName = normaliseAlarmId(alarmId)
if (!alarmName) {
app.debug(`*** Normalise Alarm Failed: ${alarmId}`)
return
}
const msg = {
message: alarmName,
method: method,
state: state
}
app.autopilotAlarm(apType, alarmName, msg)
}
// 65345 = 'steering.autopilot.target (windAngleApparent)'
if (evt.pgn === 65345) {
let angle = evt.fields['Wind Datum'] ? Number(evt.fields['Wind Datum']) : null
angle = ( typeof angle === 'number' && angle > Math.PI ) ? angle-(Math.PI*2) : angle
apStatus.target = angle
app.autopilotUpdate(apType, 'target', angle)
}
// 65360 = 'steering.autopilot.target (true/magnetic)'
if (evt.pgn === 65360) {
const targetTrue = evt.fields['Target Heading True'] ? Number(evt.fields['Target Heading True']) : null
const targetMagnetic = evt.fields['Target Heading Magnetic'] ? Number(evt.fields['Target Heading Magnetic']) : null
const target = typeof targetTrue === 'number' ? targetTrue :
typeof targetMagnetic === 'number' ? targetMagnetic: null
apStatus.target = target
app.autopilotUpdate(apType, 'target', target)
}
// 65379 = 'steering.autopilot.state', 'steering.autopilot.engaged'
if (evt.pgn === 65379) {
const mode = evt.fields['Pilot Mode'] ? Number(evt.fields['Pilot Mode']) : null
const subMode = evt.fields['Sub Mode'] ? Number(evt.fields['Sub Mode']) : null
if ( mode !== null || subMode !== null) {
if ( mode === 0 && subMode === 0 ) {
apStatus.state = 'standby'
apStatus.engaged = false
}
else if ( mode == 0 && subMode == 1 ) {
apStatus.state = 'wind'
apStatus.engaged = true
}
else if ( (mode == 128 || mode == 129) && subMode == 1 ) {
apStatus.state = 'route'
apStatus.engaged = true
}
else if ( mode == 64 && subMode == 0 ) {
apStatus.state = 'auto'
apStatus.engaged = true
}
else {
apStatus.state = 'standby'
apStatus.engaged = false
}
app.autopilotUpdate(apType, 'state', apStatus.state)
app.autopilotUpdate(apType, 'engaged', apStatus.engaged)
}
}
}
// normalise SK alarm path
const normaliseAlarmId = (id) => {
switch (id) {
case 'WP Arrival':
return 'waypointArrival'
case 'Pilot Way Point Advance':
return 'waypointAdvance'
case 'Pilot Route Complete':
return 'routeComplete'
default:
return ''
}
}
// Send NMEA2000 message.
const sendN2k = (msgs) => {
if (!Array.isArray(msgs)) {
return
}
app.debug(`sendN2k -> ${msgs}`)
msgs.map((msg) => { app.emit('nmea2000out', msg) })
}
// format N2K msg data
const padd = (n, p, c) => {
var pad_char = typeof c !== 'undefined' ? c : '0';
var pad = new Array(1 + p).join(pad_char);
return (pad + n).slice(-pad.length);
}
return pilot
}