@signalk/nmea0183-signalk
Version:
A node.js/javascript parser for NMEA0183 sentences. Sentences are parsed to Signal K format.
156 lines (154 loc) • 6.09 kB
JavaScript
;
/**
* Copyright 2016 Signal K <info@signalk.org> and contributors.
*
* 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.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const utils = __importStar(require("@signalk/nmea0183-utilities"));
/*
85 X6 XX VU ZW ZZ YF 00 yf Navigation to waypoint information
Cross Track Error: XXX / 100 nautical miles
Bearing to destination: (U & 0x3) * 90 + WV / 2 degrees
U & 8 = 8 → True, U & 8 = 0 → Magnetic
Distance to destination: If Y & 1 = 1: ZZZ / 100 nm (0-9.99nm)
If Y & 1 = 0: ZZZ / 10 nm (≥10nm)
Direction to steer: Y & 4 = 4 → steer right, Y & 4 = 0 → steer left
Flags (F): Bit 0: XTE present
Bit 1: Bearing present
Bit 2: Range present
Bit 3: XTE ≥ 0.3nm
References:
- http://www.thomasknauf.de/rap/seatalk2.htm
- https://opencpn.org/wiki/dokuwiki/doku.php?id=opencpn:supplementary_software:seatalk
*/
const S85 = function (input, _session) {
const { parts, tags } = input;
if (parts.length !== 9) {
return null;
}
// Parse hex values
const X = parseInt(parts[1].charAt(0), 16);
const XX = parseInt(parts[2], 16);
const V = parseInt(parts[3].charAt(0), 16);
const U = parseInt(parts[3].charAt(1), 16);
const Z_high = parseInt(parts[4].charAt(0), 16);
const W = parseInt(parts[4].charAt(1), 16);
const ZZ = parseInt(parts[5], 16);
const Y = parseInt(parts[6].charAt(0), 16);
const F = parseInt(parts[6].charAt(1), 16);
// parts[7]! is always 00. parts[8]! (`yf`) is the bitwise complement of the
// flags byte; parsed here only as a sanity check, not currently used.
void parseInt(parts[8], 16);
const inputs = [X, XX, V, U, Z_high, W, ZZ, Y, F];
if (!inputs.every((x) => !isNaN(x))) {
return null;
}
const pathValues = [];
// Cross Track Error: XXX / 100 nm
// XXX is formed from X (high nibble of byte 1) and XX (byte 2)
const xtePresent = (F & 0x1) === 0x1;
if (xtePresent) {
const XXX = (X << 8) | XX;
const xteNm = XXX / 100;
// Direction to steer: Y & 4 = 4 → steer right (negative XTE), Y & 4 = 0 → steer left (positive XTE)
const steerRight = (Y & 0x4) === 0x4;
const xteValue = steerRight ? -xteNm : xteNm;
pathValues.push({
path: 'navigation.courseRhumbline.crossTrackError',
value: utils.transform(xteValue, 'nm', 'm')
});
}
// Bearing to destination: (U & 0x3) * 90 + WV / 2 degrees
// WV is formed from W (low nibble of byte 4) and V (high nibble of byte 3)
const bearingPresent = (F & 0x2) === 0x2;
if (bearingPresent) {
const WV = (W << 4) | V;
const bearingDeg = (U & 0x3) * 90 + WV / 2;
const bearingRad = utils.transform(bearingDeg, 'deg', 'rad');
const isTrue = (U & 0x8) === 0x8;
if (isTrue) {
pathValues.push({
path: 'navigation.courseRhumbline.bearingToDestinationTrue',
value: bearingRad
});
}
else {
pathValues.push({
path: 'navigation.courseRhumbline.bearingToDestinationMagnetic',
value: bearingRad
});
}
}
// Distance to destination
// ZZZ is formed from ZZ (byte 5) and Z_high (high nibble of byte 4)
// Byte order is low-nibble-last: ZZZ = (ZZ << 4) | Z_high
const rangePresent = (F & 0x4) === 0x4;
if (rangePresent) {
const ZZZ = (ZZ << 4) | Z_high;
// If Y & 1 = 1: ZZZ / 100 nm (0-9.99nm)
// If Y & 1 = 0: ZZZ / 10 nm (≥10nm)
const distanceNm = (Y & 0x1) === 0x1 ? ZZZ / 100 : ZZZ / 10;
pathValues.push({
path: 'navigation.courseRhumbline.nextPoint.distance',
value: utils.transform(distanceNm, 'nm', 'm')
});
}
if (pathValues.length === 0) {
return null;
}
return {
updates: [
{
source: tags.source,
timestamp: tags.timestamp,
values: pathValues
}
]
};
};
exports.default = S85;
//# sourceMappingURL=0x85.js.map