signalk-polar-performance-plugin 0.0.33 → 0.0.35

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-polar-performance-plugin",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
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
@@ -164,30 +164,30 @@ module.exports = function (app) {
164
164
  deltas.forEach(delta => {
165
165
  // app.debug('handleData: %s', JSON.stringify(delta))
166
166
  if (delta.path == 'navigation.speedThroughWater') {
167
- STW = applyDamping (delta.value, 'BSP', options.dampingBSP || 0)
167
+ STW = applyDamping (delta.value, 'STW', options.dampingBSP || 0)
168
168
  BSP = STW
169
169
  // app.debug('speedThroughWater (STW): %d', STW)
170
170
  } else if (delta.path == 'navigation.speedOverGround') {
171
- SOG = applyDamping (delta.value, 'BSP', options.dampingBSP || 0)
171
+ SOG = applyDamping (delta.value, 'SOG', options.dampingBSP || 0)
172
172
  BSP = SOG
173
173
  // app.debug('speedThroughWater (STW): %d', STW)
174
174
  } else if (delta.path == 'navigation.headingTrue') {
175
175
  HDG = delta.value
176
176
  // app.debug('heading (HDG): %d', HDG)
177
177
  } else if (delta.path == 'environment.wind.speedTrue') {
178
- // applyDamping (Xn, unit, a, n)
179
178
  TWS = applyDamping (delta.value, 'TWS', options.dampingTWS || 0)
180
179
  // app.debug('environment.wind.speedTrue (TWS): %d applyDamping: %d', delta.value, TWS)
181
180
  // app.debug('TWS: %d TWA: %d BSP: %d', msToKts(TWS), radToDeg(TWA)*port, msToKts(BSP))
182
181
  sendUpdates(getPerformanceData(TWS, TWA, BSP))
183
182
  } else if (delta.path == 'environment.wind.angleTrueWater') {
184
- if (delta.value < 0) {
183
+ let TWAtmp = applyDamping (delta.value, 'TWA', options.dampingTWA || 0)
184
+ if (TWAtmp < 0) {
185
185
  port = -1
186
186
  } else {
187
187
  port = 1
188
188
  }
189
- TWA = Math.abs(applyDamping (delta.value, 'TWA', options.dampingTWA || 0))
190
- app.debug('environment.wind.angleTrueWater (TWA): %s applyDamping: %s', radToDeg(delta.value).toFixed(0), radToDeg(TWA).toFixed(0))
189
+ TWA = Math.abs(TWAtmp)
190
+ app.debug('environment.wind.angleTrueWater (TWA): %s applyDamping: %s port: %d', delta.value, TWA, port)
191
191
  }
192
192
  })
193
193
  }
@@ -353,15 +353,15 @@ module.exports = function (app) {
353
353
  for (let indexTWA = 0 ; indexTWA < lowerTWA.length-1; indexTWA++) {
354
354
  let lowerTWAlower = lowerTWA[indexTWA]
355
355
  let lowerTWAupper = lowerTWA[indexTWA+1]
356
- // app.debug('lowerTWAlower: %s lowerTWAupper: %s', lowerTWAlower, lowerTWAupper)
357
356
  if (TWA >= lowerTWAlower.twa && TWA <= lowerTWAupper.twa) {
357
+ // app.debug('lowerTWAlower: %s lowerTWAupper: %s', JSON.stringify(lowerTWAlower), JSON.stringify(lowerTWAupper))
358
358
  // Now find upperTWA
359
359
  for (let indexTWA = 0 ; indexTWA < upperTWA.length-1; indexTWA++) {
360
360
  let upperTWAlower = upperTWA[indexTWA]
361
361
  let upperTWAupper = upperTWA[indexTWA+1]
362
362
  if (TWA >= upperTWAlower.twa && TWA <= upperTWAupper.twa) {
363
363
  // Found the 4 points
364
- // app.debug('lowerTWAlower: %d TWA: %d lowerTWAupper: %d', lowerTWAlower.twa, TWA, lowerTWAupper.twa)
364
+ // app.debug('lowerTWAlower: %d TWA: %d lowerTWAupper: %d', radToDeg(lowerTWAlower.twa), radToDeg(TWA), radToDeg(lowerTWAupper.twa))
365
365
  // Calculate gap ratio
366
366
  let gap = lowerTWAupper.twa - lowerTWAlower.twa
367
367
  let twaGapRatio = (1 / gap) * (TWA - lowerTWAlower.twa)
@@ -372,7 +372,7 @@ module.exports = function (app) {
372
372
  let upperTBS = upperTWAlower.tbs + ((upperTWAupper.tbs - upperTWAlower.tbs) * twaGapRatio)
373
373
  // Calculate polar boat speed
374
374
  performance.polarSpeed = lowerTBS + ((upperTBS - lowerTBS) * twsGapRatio)
375
- // app.debug('lowerTBS: %d TBS: %d upperTBS: %d', lowerTBS, performance.polarSpeed, upperTBS)
375
+ // app.debug('lowerTBS: %d TBS: %d upperTBS: %d', msToKts(lowerTBS), performance.polarSpeed, msToKts(upperTBS))
376
376
  break
377
377
  }
378
378
  }
@@ -409,6 +409,7 @@ module.exports = function (app) {
409
409
  }
410
410
  // Calculate polar performance ratio
411
411
  if (typeof performance.polarSpeed != 'undefined') {
412
+ app.debug('performance.polarSpeedRatio = BSP (%s)/ performance.polarSpeed (%s)', msToKts(BSP).toFixed(2), msToKts(performance.polarSpeed).toFixed(2))
412
413
  performance.polarSpeedRatio = BSP / performance.polarSpeed
413
414
  if (options.VMG == true) {
414
415
  performance.velocityMadeGood = Math.abs(BSP * Math.cos(TWA))
@@ -680,12 +681,18 @@ module.exports = function (app) {
680
681
  // Doesn't exist yet, make obj for unit
681
682
  damping[unit] = {Yn: Xn, dt: Date.now()}
682
683
  }
683
- // Edge case when hovering around -pi and +pi
684
- if (Xn > halfPi && damping[unit].Yn < (0-halfPi)) {
685
- Xn = Xn - (2*Math.PI)
686
- } else if (Xn < (0-Math.PI) && damping[unit].Yn > Math.PI) {
687
- Xn = Xn + (2*Math.PI)
684
+ // Edge case when hovering around -pi and +pi for TWA
685
+ if (unit == 'TWA') {
686
+ if (Xn > halfPi && damping[unit].Yn < (halfPi * -1)) {
687
+ // app.debug('applyDamping: unit: %s Xn: %d > %d Yn: %d < %d -2Pi', unit, Xn, halfPi, damping[unit].Yn, halfPi * -1)
688
+ Xn = Xn - (2*Math.PI)
689
+ } else if (Xn < (halfPi * -1) && damping[unit].Yn > halfPi) {
690
+ // app.debug('applyDamping: unit: %s Xn: %d < %d Yn: %d > %d +2Pi', unit, Xn, halfPi * -1, damping[unit].Yn, halfPi)
691
+ Xn = Xn + (2*Math.PI)
692
+ }
688
693
  }
694
+
695
+ // app.debug('applyDamping: unit: %s Xn: %d Yn: %d ', unit, Xn, damping[unit].Yn)
689
696
  // Calculate a
690
697
  let dt = (Date.now() - damping[unit].dt)/1000
691
698
  damping[unit].dt = Date.now()
@@ -693,16 +700,19 @@ module.exports = function (app) {
693
700
  // Yn = (1-a) * Yn-1 + a * Xn
694
701
  let Yn = (1-a) * (damping[unit].Yn || Xn) + a * Xn
695
702
 
696
- // Fix is we go outside -pi to pi
697
- if (Yn > Math.PI) {
698
- Yn = Yn - Math.PI
699
- } else if (Yn < (0-Math.PI)) {
700
- Yn = Yn + Math.PI
703
+ // Fix if we go outside -pi to pi
704
+ if (unit == 'TWA') {
705
+ if (Yn > Math.PI) {
706
+ Yn = Yn - 2*Math.PI
707
+ } else if (Yn < Math.PI * -1) {
708
+ Yn = Yn + 2*Math.PI
709
+ }
701
710
  }
702
711
 
703
712
  // Remember Yn
704
713
  damping[unit].Yn = Yn
705
714
  // app.debug('Unit: %s dt: %d a: %d obj: %s', unit, dt, a, JSON.stringify(damping[unit]))
715
+ // app.debug('applyDamping: unit: %s new Yn: %d ', unit, damping[unit].Yn)
706
716
  return Yn
707
717
  }
708
718
  }