-
Notifications
You must be signed in to change notification settings - Fork 14
/
raymarinest.js
358 lines (307 loc) · 8.73 KB
/
raymarinest.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
/*
* Copyright 2019 Scott Bender <[email protected]> and Joachim Bakke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const util = require('util')
const state_path = "steering.autopilot.state.value"
const SUCCESS_RES = { state: 'SUCCESS' }
const FAILURE_RES = { state: 'FAILURE' }
const state_commands = {
"auto": "86,11,01,FE",
"wind": "86,11,23,DC",
"route": "86,11,03,FC",
"standby": "86,11,02,FD"
}
const keys_code = {
"+1": "86,11,07,F8",
"+10": "86,11,08,F7",
"-1": "86,11,05,FA",
"-10": "86,11,06,F9",
"-1-10": "86,11,21,DE",
"+1+10": "86,11,22,DD"
}
const wind_direction_command = "10,01,%s,%s"
const heading_command = "89,%s,%s,%s,%s"
const wp_change = "82,05,%s,%s,%s,%s,%s,%s"
const targetHeading = "89,%s,%s,$s,%s"
const keep_alive = "90,00,A3" //identify as NMEA-ST bridge
const raymarine_ttw_Mode = "85,06,00,VU,4W,06,10,00,FF"//1NM, vw mag heading
const raymarine_ttw = "82,05,XX,xx,YY,yy,ZZ,zz"//wp name
module.exports = function(app) {
var outputEvent
var pilot = {}
var timers = []
var msgs
var discovered
pilot.start = (props) => {
outputEvent = props.outputEvent
}
pilot.stop = () => {
}
function sendDatagram(datagram) {
var sentence = toSentence([
'$STALK',
datagram
])
app.debug("datagram: " + datagram)
app.emit(outputEvent, sentence)
var sentence = toSentence([
'$PSMDST',
datagram
])
app.emit(outputEvent, sentence)
}
pilot.putTargetHeading = (context, path, value, cb) => {
var state = app.getSelfPath(state_path)
if ( state !== 'auto' ) {
return { message: 'Autopilot not in auto mode', ...FAILURE_RES }
} else {
var new_value = Math.trunc(radsToDeg(value))
var quadrant = (parseInt(new_value/90))
rest = (parseInt(new_value - quadrant *90))
var VW = parseInt((rest)/2)
rest = rest-VW*2
var U = 0x02
U |= quadrant << 4 + 2;
U |= (new_value % 2) << 7;
var VW = 0x3F & ((new_value - quadrant * 90) / 2);
var XY = 0x00
var Z = 0x20
var msg = util.format(targetHeading,U.toString(16),VW.toString(16),XY.toString(16),Z.toString(16))
sendDatagram([msg])
return SUCCESS_RES
}
}
pilot.putState = (context, path, value, cb) => {
if ( !state_commands[value] ) {
return { message: `Invalid state: ${value}`, ...FAILURE_RES }
} else {
var msg = state_commands[value]
sendDatagram([msg])
return SUCCESS_RES
}
}
pilot.putTargetWind = (context, path, value, cb) => {
var state = app.getSelfPath(state_path)
if ( state !== 'wind' ) {
return { message: 'Autopilot not in wind vane mode', ...FAILURE_RES }
} else {
var new_value = Math.trunc(value)
if (new_value > 180) new_value-=180
var XX = 2* parseInt(new_value/256)
var YY = 2*(new_value-XX)
var msg = util.format(wind_direction_command,XX.toString(16),YY.toString(16))
/*
10 01 XX YY Apparent Wind Angle: XXYY/2 degrees right of bow
Used for autopilots Vane Mode (WindTrim)
Corresponding NMEA sentence: MWV
*/
/*
var msg = util.format(command_format, (new Date()).toISOString(), default_src,
autopilot_dst, padd((new_value & 0xff).toString(16), 2), padd(((new_value >> 8) & 0xff).toString(16), 2))
*/
var msg = targetWind //@TODO
sendDatagram([msg])
return SUCCESS_RES
}
}
pilot.putAdjustHeading = (context, path, value, cb) => {
var state = app.getSelfPath(state_path)
if ( state !== 'auto' && state != 'standby' && state != 'route') {
return { message: 'Autopilot not in auto, standby or route mode', ...FAILURE_RES }
} else {
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:
return { message: `Invalid adjustment: ${value}`, ...FAILURE_RES }
}
sendDatagram(changeHeadingByKey(app, outputEvent, {value: aString}))
return SUCCESS_RES
}
}
pilot.putTack = (context, path, value, cb) => {
var state = app.getSelfPath(state_path)
if ( state !== 'wind' ) {
return { message: 'Autopilot not in wind vane mode', ...FAILURE_RES }
} else {
sendDatagram(tackTo(app, outputEvent, {value: value}))
return SUCCESS_RES
}
}
pilot.putAdvanceWaypoint = (context, path, value, cb) => {
var state = app.getSelfPath(state_path)
if ( state !== 'track' ) {
return { message: 'Autopilot not in track mode', ...FAILURE_RES }
} else {
return { message: 'Autopilot next waypoint not implemented', ...FAILURE_RES }
}
}
pilot.sendCommand = (req, res) => {
if ( typeof outputEvent != "undefined" )
{
sendCommand(app, outputEvent, req.body)
res.send("Executed command")
}
}
pilot.properties = () => {
var eventList = []
let defaultEvent = 'seatalkOut'
return {
outputEvent: {
type: "string",
title: "NMEA 0183 outputEvent for the Seatalk Connection",
default: defaultEvent
}
}
}
return pilot
}
function 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);
}
function changeHeading(app, outputEvent, command_json)
{
var ammount = command_json["value"]
var state = app.getSelfPath(state_path)
var new_value
var command_format
var datagram
app.debug("changeHeading: " + state + " " + ammount)
if ( state == "auto" )
{
var current = app.getSelfPath(target_heading_path)
new_value = radsToDeg(current) + ammount
if ( new_value < 0 ) {
new_value = 360 + new_value
} else if ( new_value > 360 ) {
new_value = new_value - 360
}
app.debug(`current heading: ${radsToDeg(current)} new value: ${new_value}`)
command_format = heading_command
}
else if ( state == "wind" )
{
var current = app.getSelfPath(target_wind_path)
new_value = radsToDeg(current) + ammount
if ( new_value < 0 )
new_value = 360 + new_value
else if ( new_value > 360 )
new_value = new_value - 360
app.debug(`current wind angle: ${radsToDeg(current)} new value: ${new_value}`)
command_format = wind_direction_command
}
else
{
//error
}
if ( new_value )
{
new_value = Math.trunc(degsToRad(new_value) * 10000)
return // @TODO ??
/*
nmea0183_msgs = [util.format(command_format, (new Date()).toISOString(), default_src,
autopilot_dst, padd((new_value & 0xff).toString(16), 2), padd(((new_value >> 8) & 0xff).toString(16), 2))]
*/
}
return datagram
}
function setState(app, outputEvent, command_json)
{
var state = command_json["value"]
app.debug("setState: " + state)
return state_commands[state]}
function tackTo(app, outputEvent, command_json)
{
var tackTo = command_json["value"]
app.debug("tackTo: " + tackTo)
if (tackTo === "port")
{
return keys_code["-1-10"]
}
else if (tackTo === "starboard")
{
return keys_code["+1+10"]
}
else
{
app.debug("tackTo: unknown " + tackTo)
}
}
function changeHeadingByKey(app, outputEvent, command_json)
{
var key = command_json["value"]
app.debug("changeHeadingByKey: " + key)
return keys_code[key]}
function radsToDeg(radians) {
return radians * 180 / Math.PI
}
function degsToRad(degrees) {
return degrees * (Math.PI/180.0);
}
function toSentence(parts) {
var base = parts.join(',');
return base + computeChecksum(base);
}
var m_hex = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
];
function computeChecksum(sentence) {
var c1;
var i;
// skip the $
i = 1;
// init to first character var count;
c1 = sentence.charCodeAt(i);
// process rest of characters, zero delimited
for (i = 2; i < sentence.length; ++i) {
c1 = c1 ^ sentence.charCodeAt(i);
}
return '*' + toHexString(c1);
};
function toHexString(v) {
var lsn;
var msn;
msn = (v >> 4) & 0x0f;
lsn = (v >> 0) & 0x0f;
return m_hex[msn] + m_hex[lsn];
};