signalk-polar-performance-plugin 0.0.32 → 0.0.34

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 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signalk-polar-performance-plugin",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
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
@@ -175,19 +175,19 @@ module.exports = function (app) {
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
  }
@@ -285,6 +285,7 @@ module.exports = function (app) {
285
285
  if (typeof polar[indexTWS]['Beat angle'] != 'undefined') {
286
286
  let beatLower = polar[indexTWS]['Beat angle']
287
287
  let beatUpper = polar[indexTWS+1]['Beat angle']
288
+ //app.debug('beatLower: %s beatUpper: %s', beatLower, beatUpper)
288
289
  performance.beatAngle = beatLower + ((beatUpper - beatLower) * twsGapRatio)
289
290
  targetTWA = performance.beatAngle
290
291
  }
@@ -298,6 +299,7 @@ module.exports = function (app) {
298
299
  if (typeof polar[indexTWS]['Beat VMG'] != 'undefined') {
299
300
  let VMGLower = polar[indexTWS]['Beat VMG']
300
301
  let VMGUpper = polar[indexTWS+1]['Beat VMG']
302
+ //app.debug('VMGLower: %s VMGUpper: %s', VMGLower, VMGUpper)
301
303
  performance.beatVMG = VMGLower + ((VMGUpper - VMGLower) * twsGapRatio)
302
304
  performance.targetVMG = performance.beatVMG
303
305
  }
@@ -321,7 +323,7 @@ module.exports = function (app) {
321
323
  if (typeof polar[indexTWS]['Run VMG'] != 'undefined') {
322
324
  let VMGLower = polar[indexTWS]['Run VMG']
323
325
  let VMGUpper = polar[indexTWS+1]['Run VMG']
324
- //app.debug('VMGLower: %s VMGUpper: %s', VMGLower, VMGUpper)
326
+ //app.debug('VMGLower: %s VMGUpper: %s', VMGLower, VMGUpper)
325
327
  performance.runVMG = VMGLower + ((VMGUpper - VMGLower) * twsGapRatio)
326
328
  performance.targetVMG = performance.runVMG
327
329
  }
@@ -346,20 +348,20 @@ module.exports = function (app) {
346
348
  // Interpolate Define the 4 near data points
347
349
  let lowerTWA = polar[indexTWS].twa
348
350
  let upperTWA = polar[indexTWS+1].twa
349
- // app.debug('TWAlower: %s', JSON.stringify(TWAlower))
351
+ // app.debug('lowerTWA: %s', JSON.stringify(lowerTWA))
350
352
  // First find lowerTWA
351
353
  for (let indexTWA = 0 ; indexTWA < lowerTWA.length-1; indexTWA++) {
352
354
  let lowerTWAlower = lowerTWA[indexTWA]
353
355
  let lowerTWAupper = lowerTWA[indexTWA+1]
354
- // app.debug('lowerTWAlower: %s lowerTWAupper: %s', lowerTWAlower, lowerTWAupper)
355
356
  if (TWA >= lowerTWAlower.twa && TWA <= lowerTWAupper.twa) {
357
+ // app.debug('lowerTWAlower: %s lowerTWAupper: %s', JSON.stringify(lowerTWAlower), JSON.stringify(lowerTWAupper))
356
358
  // Now find upperTWA
357
359
  for (let indexTWA = 0 ; indexTWA < upperTWA.length-1; indexTWA++) {
358
360
  let upperTWAlower = upperTWA[indexTWA]
359
361
  let upperTWAupper = upperTWA[indexTWA+1]
360
362
  if (TWA >= upperTWAlower.twa && TWA <= upperTWAupper.twa) {
361
363
  // Found the 4 points
362
- // 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))
363
365
  // Calculate gap ratio
364
366
  let gap = lowerTWAupper.twa - lowerTWAlower.twa
365
367
  let twaGapRatio = (1 / gap) * (TWA - lowerTWAlower.twa)
@@ -370,7 +372,7 @@ module.exports = function (app) {
370
372
  let upperTBS = upperTWAlower.tbs + ((upperTWAupper.tbs - upperTWAlower.tbs) * twaGapRatio)
371
373
  // Calculate polar boat speed
372
374
  performance.polarSpeed = lowerTBS + ((upperTBS - lowerTBS) * twsGapRatio)
373
- // 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))
374
376
  break
375
377
  }
376
378
  }
@@ -420,7 +422,7 @@ module.exports = function (app) {
420
422
  if (options.VMG == true) {
421
423
  performance.velocityMadeGood = 0
422
424
  performance.polarVelocityMadeGood = 0
423
- performance.polarRatioVelocityMadeGood = 0
425
+ performance.polarVelocityMadeGoodRatio = 0
424
426
  }
425
427
  }
426
428
  return performance
@@ -478,9 +480,11 @@ module.exports = function (app) {
478
480
  }
479
481
  let Obj = {"twa": angle, "tbs": tbs, "vmg": vmg}
480
482
  polar[index]['twa'] = polar[index]['twa'].concat(Obj)
483
+ app.debug('Finding max speed')
481
484
  if (typeof polar[index]['Max speed'] == 'undefined' || tbs > polar[index]['Max speed']) {
482
485
  polar[index]['Max speed'] = tbs
483
486
  polar[index]['Max speed angle'] = angle
487
+ app.debug('Found max speed: %s', JSON.stringify(polar[index]))
484
488
  }
485
489
  }
486
490
  }
@@ -503,6 +507,7 @@ module.exports = function (app) {
503
507
  if (typeof polar[index]['Max speed'] == 'undefined' || tbs > polar[index]['Max speed']) {
504
508
  polar[index]['Max speed'] = tbs
505
509
  polar[index]['Max speed angle'] = angle
510
+ app.debug('Found max speed: %s', JSON.stringify(polar[index]))
506
511
  }
507
512
  }
508
513
  }
@@ -563,10 +568,12 @@ module.exports = function (app) {
563
568
  'Beat VMG': polar[0]['Beat VMG'],
564
569
  'Run angle': polar[0]['Run angle'],
565
570
  'Run VMG': polar[0]['Run VMG'],
571
+ 'Max speed': 0,
572
+ 'Max speed angle': polar[0]['Max speed angle'],
566
573
  twa: []
567
574
  }
568
- Object.keys(polar[0].twa).forEach(twaObj => {
569
- Obj.twa.push({twa: twaObj.twa, tbs: 0})
575
+ Object.values(polar[0].twa).forEach(twaObj => {
576
+ Obj.twa.push({twa: twaObj.twa, tbs: 0, vmg: 0})
570
577
  })
571
578
  polar.unshift(Obj)
572
579
  }
@@ -673,12 +680,18 @@ module.exports = function (app) {
673
680
  // Doesn't exist yet, make obj for unit
674
681
  damping[unit] = {Yn: Xn, dt: Date.now()}
675
682
  }
676
- // Edge case when hovering around -pi and +pi
677
- if (Xn > halfPi && damping[unit].Yn < (0-halfPi)) {
678
- Xn = Xn - (2*Math.PI)
679
- } else if (Xn < (0-Math.PI) && damping[unit].Yn > Math.PI) {
680
- Xn = Xn + (2*Math.PI)
683
+ // Edge case when hovering around -pi and +pi for TWA
684
+ if (unit == 'TWA') {
685
+ if (Xn > halfPi && damping[unit].Yn < (halfPi * -1)) {
686
+ // app.debug('applyDamping: unit: %s Xn: %d > %d Yn: %d < %d -2Pi', unit, Xn, halfPi, damping[unit].Yn, halfPi * -1)
687
+ Xn = Xn - (2*Math.PI)
688
+ } else if (Xn < (halfPi * -1) && damping[unit].Yn > halfPi) {
689
+ // app.debug('applyDamping: unit: %s Xn: %d < %d Yn: %d > %d +2Pi', unit, Xn, halfPi * -1, damping[unit].Yn, halfPi)
690
+ Xn = Xn + (2*Math.PI)
691
+ }
681
692
  }
693
+
694
+ // app.debug('applyDamping: unit: %s Xn: %d Yn: %d ', unit, Xn, damping[unit].Yn)
682
695
  // Calculate a
683
696
  let dt = (Date.now() - damping[unit].dt)/1000
684
697
  damping[unit].dt = Date.now()
@@ -686,16 +699,19 @@ module.exports = function (app) {
686
699
  // Yn = (1-a) * Yn-1 + a * Xn
687
700
  let Yn = (1-a) * (damping[unit].Yn || Xn) + a * Xn
688
701
 
689
- // Fix is we go outside -pi to pi
690
- if (Yn > Math.PI) {
691
- Yn = Yn - Math.PI
692
- } else if (Yn < (0-Math.PI)) {
693
- Yn = Yn + Math.PI
702
+ // Fix if we go outside -pi to pi
703
+ if (unit == 'TWA') {
704
+ if (Yn > Math.PI) {
705
+ Yn = Yn - 2*Math.PI
706
+ } else if (Yn < Math.PI * -1) {
707
+ Yn = Yn + 2*Math.PI
708
+ }
694
709
  }
695
710
 
696
711
  // Remember Yn
697
712
  damping[unit].Yn = Yn
698
713
  // app.debug('Unit: %s dt: %d a: %d obj: %s', unit, dt, a, JSON.stringify(damping[unit]))
714
+ // app.debug('applyDamping: unit: %s new Yn: %d ', unit, damping[unit].Yn)
699
715
  return Yn
700
716
  }
701
717
  }