Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructured to allow sentences to be loaded from files #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
465 changes: 67 additions & 398 deletions index.js

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions nmea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module.exports = function() {
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];
};

function radsToDeg(radians) {
return radians * 180 / Math.PI
}

function msToKnots(v) {
return v*3600/1852.0;
}

function msToKM(v) {
return v*3600.0/1000.0;
}

function mToNm(v) {
return v*0.000539957;
}


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 toNmeaDegrees(val)
{
val = Math.abs(val)
var minutes = Math.floor(val)
var minutes_decimal = val % 1
minutes_decimal *= 60.0;
return padd(minutes.toFixed(0),2) + padd(minutes_decimal.toFixed(4), 7)
}

function fixAngle(d) {
if ( d > Math.PI ) d = d - Math.PI;
if ( d < -Math.PI) d = d + Math.PI;
return d;
}
return {
toSentence: toSentence,
radsToDeg: radsToDeg,
msToKnots: msToKnots,
msToKM: msToKM,
toNmeaDegrees: toNmeaDegrees,
fixAngle: fixAngle
};
}();
62 changes: 62 additions & 0 deletions sentences/APB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
------------------------------------------------------------------------------
13 15
1 2 3 4 5 6 7 8 9 10 11 12| 14|
| | | | | | | | | | | | | | |
$--APB,A,A,x.x,a,N,A,A,x.x,a,c--c,x.x,a,x.x,a*hh<CR><LF>
------------------------------------------------------------------------------

Field Number:

1. Status
V = LORAN-C Blink or SNR warning
V = general warning flag or other navigation systems when a reliable
fix is not available
2. Status
V = Loran-C Cycle Lock warning flag
A = OK or not used
3. Cross Track Error Magnitude
4. Direction to steer, L or R
5. Cross Track Units, N = Nautical Miles
6. Status
A = Arrival Circle Entered
7. Status
A = Perpendicular passed at waypoint
8. Bearing origin to destination
9. M = Magnetic, T = True
10. Destination Waypoint ID
11. Bearing, present position to Destination
12. M = Magnetic, T = True
13. Heading to steer to destination waypoint
14. M = Magnetic, T = True
15. Checksum

Example: $GPAPB,A,A,0.10,R,N,V,V,011,M,DEST,011,M,011,M*82
*/
// to verify
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "APB - Autopilot info",
keys: [
'navigation.courseGreatCircle.crossTrackError', 'navigation.courseGreatCircle.bearingTrackTrue', 'navigation.courseGreatCircle.nextPoint'
],
f: function(xte, originToDest, nextPoint) {
return nmea.toSentence([
'$SKAPB', 'A', 'A', Math.abs(xte), xte > 0
? 'L'
: 'R',
'M',
'V',
'V',
nmea.radsToDeg(originToDest).toFixed(0),
'T',
'00',
nmea.radsToDeg(nextPoint.bearingTrue).toFixed(0),
'T',
nmea.radsToDeg(nextPoint.bearingMagnetic).toFixed(0),
'M'
]);
}
};
}
23 changes: 23 additions & 0 deletions sentences/DBK.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// NMEA0183 Encoder DBK $IIDBK,102.9,f,31.38,M,17.2,F*39
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "DBK - Depth Below Keel",
keys: [
'environment.depth.belowKeel'
],
f: function mwv(depth) {
var feet = depth * 3.28084
var fathoms = depth * 0.546807
return nmea.toSentence([
'$IIDBK',
feet.toFixed(1),
'f',
depth.toFixed(2),
'M',
fathoms.toFixed(1),
'F'
]);
}
};
}
23 changes: 23 additions & 0 deletions sentences/DBS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// to verify
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "DBS - Depth Below Surface",
keys: [
'environment.depth.belowSurface'
],
f: function mwv(depth) {
var feet = depth * 3.28084
var fathoms = depth * 0.546807
return nmea.toSentence([
'$IIDBS',
feet.toFixed(1),
'f',
depth.toFixed(2),
'M',
fathoms.toFixed(1),
'F'
]);
}
};
}
23 changes: 23 additions & 0 deletions sentences/DBT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// NMEA0183 Encoder DBT $IIDBT,103.0,f,31.38,M,17.2,F*2E
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "DBT - Depth Below Transducer",
keys: [
'environment.depth.belowTransducer'
],
f: function mwv(depth) {
var feet = depth * 3.28084
var fathoms = depth * 0.546807
return nmea.toSentence([
'$IIDBT',
feet.toFixed(1),
'f',
depth.toFixed(2),
'M',
fathoms.toFixed(1),
'F'
]);
}
};
};
28 changes: 28 additions & 0 deletions sentences/DPT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/**
Depth:
$IIDPT,x.x,x.x,,*hh
I I_Sensor offset, >0 = surface transducer distance, >0 = keel transducer distance.
I_Bottom transducer distance



*/
// NMEA0183 Encoder DPT $IIDPT,69.21,-0.001*60
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "DPT - Depth",
keys: [
'environment.depth.belowTransducer', 'environment.depth.transducerToKeel'
],
f: function dpt(belowTransducer, transducerToKeel) {
return nmea.toSentence([
'$IIDPT',
belowTransducer.toFixed(2),
transducerToKeel.toFixed(3)
]);
}
};
}

37 changes: 37 additions & 0 deletions sentences/GLL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@



/*
Geographical position, latitude and longitude:
$IIGLL,IIII.II,a,yyyyy.yy,a,hhmmss.ss,A,A*hh
I I I I I I_Statut, A= valid data, V= non valid data
I I I I I_UTC time
I I I___ I_Longitude, E/W
I__I_Latidude, N/S
*/
// NMEA0183 Encoder GLL $IIGLL,5943.4970,N,2444.1983,E,200001.020,A*16

const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "GLL - Geographical position, latitude and longitude",
keys: [
'navigation.datetime', 'navigation.position'
],
f: function gll(datetime8601, position) {
var datetime = new Date(datetime8601);
var hours = ('00' + datetime.getHours()).slice(-2);
var minutes = ('00' + datetime.getMinutes()).slice(-2);
var seconds = ('00' + datetime.getSeconds()).slice(-2);
return nmea.toSentence([
'$IIGLL',
nmea.toNmeaDegrees(position.latitude),
position.latitude < 0 ? 'S' : 'N',
nmea.toNmeaDegrees(position.longitude),
position.longitude < 0 ? 'W' : 'E',
hours + minutes + seconds + '.020',
'A'
]);
}
};
}
29 changes: 29 additions & 0 deletions sentences/HDG.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@



/*
Heading magnetic:
$IIHDG,x.x,,,,*hh
I_Heading magnetic
*/
// NMEA0183 Encoder HDG $IIHDG,206.71,,,,*7B

const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "HDG - Heading magnetic:.",
keys: [
'navigation.headingMagnetic'
],
f: function hdg(headingMagnetic) {
return nmea.toSentence([
'$IIHDG',
nmea.radsToDeg(headingMagnetic).toFixed(2),
'',
'',
'',
''
]);
}
};
}
18 changes: 18 additions & 0 deletions sentences/HDM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// NMEA0183 Encoder HDM $IIHDM,206.7,M*21
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "HDM - Heading Magnetic",
keys: [
'navigation.headingMagnetic'
],
f: function mwv(heading) {
return nmea.toSentence([
'$IIHDM',
nmea.radsToDeg(heading).toFixed(1),
'M'
]);
}
};
}

18 changes: 18 additions & 0 deletions sentences/HDMC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// NMEA0183 Encoder HDMC $IIHDM,212.2,M*21
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "HDM - Heading Magnetic, calculated from True",
keys: [
'navigation.headingTrue', 'navigation.magneticVariation'
],
f: function mwv(headingTrue, magneticVariation) {
var heading = headingTrue + magneticVariation;
return nmea.toSentence([
'$IIHDM',
nmea.radsToDeg(heading).toFixed(1),
'M'
]);
}
};
}
20 changes: 20 additions & 0 deletions sentences/HDT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

//NMEA0183 Encoder HDT $IIHDT,200.1,T*21
const nmea = require('../nmea.js');
module.exports = function(app) {
return {
title: "HDT - Heading True",
keys: [
'navigation.headingTrue'
],
f: function mwv(heading) {
return nmea.toSentence([
'$IIHDT',
nmea.radsToDeg(heading).toFixed(1),
'T'
]);
}
};
}


Loading