Skip to content

Commit

Permalink
Add DTG calculation (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj authored Sep 10, 2020
1 parent 1f253cd commit 1dce6b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ It currently calculates:
* Depth Below Keel (based on depth.belowSurface and design.draft.maximum)
* Depth Below Keel (based on depth.belowTransducer and depth.transducerToKeel)
* Depth Below Surface (based on depth.belowKeel and design.draft.maximum)
* Distance To Go (based on courseGreatCircle.nextPoint.position)
* Outside air dew point (based on humidity and temperature)
* Fuel economy (based on speed over ground, fuel rate)
* Propeller Slip calculation (requires defaults.json to include propulsion.\*.drive.propeller.pitch and propulsion.\*.transmission.gearRatio)
Expand Down
34 changes: 34 additions & 0 deletions calcs/distanceToGo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = function(app) {
return {
group: 'dtg',
optionKey: 'dtg',
title: "Distance To Go (based on courseGreatCircle.nextPoint.position)",
derivedFrom: [ "navigation.courseGreatCircle.nextPoint.position",
"navigation.position" ],
calculator: function (nextPointPosition, vesselPosition) {
let distance= (!nextPointPosition || !vesselPosition) ?
null : calcDistance(vesselPosition, nextPointPosition)

//** Calculate the great circle distance between two points in metres
function calcDistance(srcpt, destpt) {
let Rk= 6373 // mean radius of the earth (km) at 39 degrees from the equator
let lat1= degreesToRadians(srcpt.latitude)
let lon1= degreesToRadians(srcpt.longitude)
let lat2= degreesToRadians(destpt.latitude)
let lon2= degreesToRadians(destpt.longitude)
let dlat= lat2 - lat1
let dlon= lon2 - lon1
let a= Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2)
let c= 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)) // great circle distance in radians
return (c * Rk) * 1000 // great circle distance in m
}

function degreesToRadians(val) { return val * Math.PI/180 }

return [{
path: "navigation.courseGreatCircle.nextPoint.distance",
value: distance
}]
}
};
}

0 comments on commit 1dce6b0

Please sign in to comment.