signalk-polar-performance-plugin 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/plugin/index.js +38 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-polar-performance-plugin",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "A plugin that calculates performance information based on a (CSV) polar diagram.",
5
5
  "main": "plugin/index.js",
6
6
  "scripts": {
package/plugin/index.js CHANGED
@@ -52,18 +52,19 @@ module.exports = function (app) {
52
52
  },
53
53
  dampingTWA: {
54
54
  type: 'number',
55
- title: 'True Wind Angle damping factor (0 = no damping, 1 = full damping)',
56
- default: 0
55
+ description: 'If data appears erratic or too sensitive, damping may be applied to amke information appear more stable. With damping set to 0, the data is presented in raw form wih no damping applied.',
56
+ title: 'True Wind Angle damping seconds',
57
+ default: 1
57
58
  },
58
59
  dampingTWS: {
59
60
  type: 'number',
60
- title: 'True Wind Speed damping factor (0 = no damping, 1 = full damping)',
61
- default: 0
61
+ title: 'True Wind Speed damping seconds',
62
+ default: 1
62
63
  },
63
64
  dampingBSP: {
64
65
  type: 'number',
65
- title: 'Boat speed damping factor (0 = no damping, 1 = full damping)',
66
- default: 0
66
+ title: 'Boat speed damping damping',
67
+ default: 1
67
68
  },
68
69
  csvTable: {
69
70
  type: "string",
@@ -157,11 +158,11 @@ module.exports = function (app) {
157
158
  deltas.forEach(delta => {
158
159
  // app.debug('handleData: %s', JSON.stringify(delta))
159
160
  if (delta.path == 'navigation.speedThroughWater') {
160
- STW = applyDamping (delta.value, 'BSP', options.dampingBSP || 0, 3)
161
+ STW = applyDamping (delta.value, 'BSP', options.dampingBSP || 0)
161
162
  BSP = STW
162
163
  // app.debug('speedThroughWater (STW): %d', STW)
163
164
  } else if (delta.path == 'navigation.speedOverGround') {
164
- SOG = applyDamping (delta.value, 'BSP', options.dampingBSP || 0, 3)
165
+ SOG = applyDamping (delta.value, 'BSP', options.dampingBSP || 0)
165
166
  BSP = SOG
166
167
  // app.debug('speedThroughWater (STW): %d', STW)
167
168
  } else if (delta.path == 'navigation.headingTrue') {
@@ -169,7 +170,7 @@ module.exports = function (app) {
169
170
  // app.debug('heading (HDG): %d', HDG)
170
171
  } else if (delta.path == 'environment.wind.speedTrue') {
171
172
  // applyDamping (Xn, unit, a, n)
172
- TWS = applyDamping (delta.value, 'TWS', options.dampingTWS || 0, 3)
173
+ TWS = applyDamping (delta.value, 'TWS', options.dampingTWS || 0)
173
174
  // app.debug('environment.wind.speedTrue (TWS): %d applyDamping: %d', delta.value, TWS)
174
175
  // app.debug('TWS: %d TWA: %d BSP: %d', msToKts(TWS), radToDeg(TWA)*port, msToKts(BSP))
175
176
  sendUpdates(getPerformanceData(TWS, TWA, BSP))
@@ -179,8 +180,8 @@ module.exports = function (app) {
179
180
  } else {
180
181
  port = 1
181
182
  }
182
- TWA = Math.abs(applyDamping (delta.value, 'TWA', options.dampingTWA || 0, 1))
183
- // app.debug('environment.wind.angleTrueWater (TWA): %s applyDamping: %s', radToDeg(delta.value).toFixed(0), radToDeg(TWA).toFixed(0))
183
+ TWA = Math.abs(applyDamping (delta.value, 'TWA', options.dampingTWA || 0))
184
+ app.debug('environment.wind.angleTrueWater (TWA): %s applyDamping: %s', radToDeg(delta.value).toFixed(0), radToDeg(TWA).toFixed(0))
184
185
  }
185
186
  })
186
187
  }
@@ -637,22 +638,26 @@ module.exports = function (app) {
637
638
  return data
638
639
  }
639
640
 
640
- function applyDamping (Xn, unit, a, n) {
641
- if (typeof damping[unit] == 'undefined') {
642
- // Doesn't exist yet, make array for unit
643
- damping[unit] = {Yn: []}
644
- }
645
- // Yn = a * Yn-1 + (1-a) * Xn
646
- let Yn = a * (damping[unit]['Yn'][n-1] || Xn) + (1-a) * Xn
647
- // Add Yn to begin of Yn array
648
- damping[unit]['Yn'].push(Yn)
649
- // Remove last value from Yn array if needed
650
- if (damping[unit]['Yn'].length > n+1) {
651
- damping[unit]['Yn'].shift()
652
- }
653
- // app.debug('Unit: %s array: %s', unit, damping[unit]['Yn'].join(','))
654
- return Yn
655
- }
641
+ function applyDamping (Xn, unit, RC) {
642
+ if (RC == 0) {
643
+ return Xn
644
+ } else {
645
+ if (typeof damping[unit] == 'undefined') {
646
+ // Doesn't exist yet, make obj for unit
647
+ damping[unit] = {Yn: Xn, dt: Date.now()}
648
+ }
649
+ // Calculate a
650
+ let dt = (Date.now() - damping[unit].dt)/1000
651
+ damping[unit].dt = Date.now()
652
+ let a = dt / (RC + dt)
653
+ // Yn = (1-a) * Yn-1 + a * Xn
654
+ let Yn = (1-a) * (damping[unit].Yn || Xn) + a * Xn
655
+ // Remember Yn
656
+ damping[unit].Yn = Yn
657
+ // app.debug('Unit: %s dt: %d a: %d obj: %s', unit, dt, a, JSON.stringify(damping[unit]))
658
+ return Yn
659
+ }
660
+ }
656
661
  }
657
662
 
658
663
  plugin.stop = function () {
@@ -689,7 +694,11 @@ function msToKts(ms) {
689
694
  }
690
695
 
691
696
  function roundDec (value) {
692
- value = Number(value.toFixed(3))
693
- return value
697
+ if (typeof value == 'undefined') {
698
+ return undefined
699
+ } else {
700
+ value = Number(value.toFixed(3))
701
+ return value
702
+ }
694
703
  }
695
704