Skip to content

Commit

Permalink
fix: MWV angles should be positive
Browse files Browse the repository at this point in the history
Also add tests.
  • Loading branch information
tkurki committed Mar 16, 2019
1 parent 2c34cb1 commit f134ee9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
11 changes: 10 additions & 1 deletion nmea.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,21 @@ function fixAngle (d) {
return result
}

function toPositiveRadians (d) {
return d < 0 ? d + 2 * Math.PI : d
}

function radsToPositiveDeg(r) {
return radsToDeg(toPositiveRadians(r))
}

module.exports = {
toSentence: toSentence,
radsToDeg: radsToDeg,
msToKnots: msToKnots,
msToKM: msToKM,
toNmeaDegreesLatitude: toNmeaDegreesLatitude,
toNmeaDegreesLongitude: toNmeaDegreesLongitude,
fixAngle: fixAngle
fixAngle: fixAngle,
radsToPositiveDeg
}
2 changes: 1 addition & 1 deletion sentences/MWVR.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function (app) {
f: function (angle, speed) {
return nmea.toSentence([
'$INMWV',
nmea.radsToDeg(angle).toFixed(2),
nmea.radsToPositiveDeg(angle).toFixed(2),
'R',
speed.toFixed(2),
'M',
Expand Down
2 changes: 1 addition & 1 deletion sentences/MWVT.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (app) {
f: function (angle, speed) {
return nmea.toSentence([
'$INMWV',
nmea.radsToDeg(angle).toFixed(2),
nmea.radsToPositiveDeg(angle).toFixed(2),
'T',
speed.toFixed(2),
'M',
Expand Down
57 changes: 57 additions & 0 deletions test/MWV.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const assert = require('assert')

const { createAppWithPlugin } = require('./testutil')

describe('MWV relative', function () {
it('works with positive angle', done => {
const onEmit = (event, value) => {
assert.equal(value, '$INMWV,180.00,R,2.00,M,A*32')
done()
}
const app = createAppWithPlugin(onEmit, 'MWVR')
app.streambundle
.getSelfStream('environment.wind.angleApparent')
.push(Math.PI)
app.streambundle.getSelfStream('environment.wind.speedApparent').push(2)
})

it('works with negative angle', done => {
const onEmit = (event, value) => {
assert.equal(value, '$INMWV,270.00,R,2.00,M,A*3E')
done()
}
const app = createAppWithPlugin(onEmit, 'MWVR')
app.streambundle
.getSelfStream('environment.wind.angleApparent')
.push(-Math.PI / 2)
app.streambundle.getSelfStream('environment.wind.speedApparent').push(2)
})
})

describe('MWV true', function () {
it('works with positive angle', done => {
const onEmit = (event, value) => {
assert.equal(value, '$INMWV,180.00,T,2.00,M,A*34')
done()
}

const app = createAppWithPlugin(onEmit, 'MWVT')
app.streambundle
.getSelfStream('environment.wind.angleTrueWater')
.push(Math.PI)
app.streambundle.getSelfStream('environment.wind.speedTrue').push(2)
})

it('works with negative angle', done => {
const onEmit = (event, value) => {
assert.equal(value, '$INMWV,270.00,T,2.00,M,A*38')
done()
}

const app = createAppWithPlugin(onEmit, 'MWVT')
app.streambundle
.getSelfStream('environment.wind.angleTrueWater')
.push(-Math.PI / 2)
app.streambundle.getSelfStream('environment.wind.speedTrue').push(2)
})
})

0 comments on commit f134ee9

Please sign in to comment.