Skip to content

Commit

Permalink
Add real lat/lon to RMC sentence (#3)
Browse files Browse the repository at this point in the history
* Add real lat/lon to RMC sentence
  • Loading branch information
sbender9 authored and tkurki committed Apr 24, 2017
1 parent 45003a4 commit f8d1b48
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function(app) {

function mapToNmea(encoder) {
const selfStreams = encoder.keys.map(app.streambundle.getSelfStream, app.streambundle)
plugin.unsubscribes.push(Bacon.combineWith(encoder.f, selfStreams).changes().debounceImmediate(20).log().onValue(nmeaString => {
plugin.unsubscribes.push(Bacon.combineWith(encoder.f, selfStreams).changes().debounceImmediate(20).onValue(nmeaString => {
app.emit('nmea0183out', nmeaString)
}))
}
Expand Down Expand Up @@ -195,22 +195,23 @@ Field Number:

var RMC = {
keys: [
'navigation.datetime', 'navigation.speedOverGround', 'navigation.courseOverGroundTrue'
'navigation.datetime', 'navigation.speedOverGround', 'navigation.courseOverGroundTrue', 'navigation.position'
],
f: function(datetime8601, sog, cog) {
f: function(datetime8601, sog, cog, 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 toSentence([
'$SKRMC', hours + minutes + seconds + '.020',
'A',
'0000.00',
'N',
'0000.00',
'E',
toNmeaDegrees(position.latitude),
position.latitude < 0 ? 'S' : 'N',
toNmeaDegrees(position.longitude),
position.longitude < 0 ? 'W' : 'E',
(sog * 1.94384).toFixed(1),
cog.toFixed(1),
radsToDeg(cog).toFixed(1),
'0000',
'7.0',
'E'
Expand Down Expand Up @@ -321,3 +322,23 @@ function toHexString(v) {
lsn = (v >> 0) & 0x0f;
return m_hex[msn] + m_hex[lsn];
};

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

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)
}

0 comments on commit f8d1b48

Please sign in to comment.