From 2a2f2ffef90b1a1d5f6fb709afd322e3cb7863e1 Mon Sep 17 00:00:00 2001 From: Joel Hulen Date: Sun, 21 Jan 2018 21:07:23 -0500 Subject: [PATCH 1/2] Improve gps location simulation The previous version never actually changed the truck's location after the first pass. This version randomizes the changed distance, plus it adds a rounding function that handles the positive and negative values of the lat/long coordinates equally well. This results in an improved randomized route for the trucks. Not 100% realistic (data would need to be incorporated for that), but good enough for a simulation. --- .../devicemodels/scripts/truck-01-state.js | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Services/data/devicemodels/scripts/truck-01-state.js b/Services/data/devicemodels/scripts/truck-01-state.js index c75828d1..69f50799 100644 --- a/Services/data/devicemodels/scripts/truck-01-state.js +++ b/Services/data/devicemodels/scripts/truck-01-state.js @@ -52,11 +52,29 @@ function varylocation(latitude, longitude, distance) { // Convert to meters, use Earth radius, convert to radians var radians = (distance * 1609.344 / 6378137) * (180 / Math.PI); return { - latitude: latitude + radians, - longitude: longitude + radians / Math.cos(latitude * Math.PI / 180) + latitude: roundTo((latitude + radians), 6), + longitude: roundTo((longitude + radians / Math.cos(latitude * Math.PI / 180)), 6) }; } +function roundTo(n, digits) { + var negative = false; + if (digits === undefined) { + digits = 0; + } + if (n < 0) { + negative = true; + n = n * -1; + } + var multiplicator = Math.pow(10, digits); + n = parseFloat((n * multiplicator).toFixed(11)); + n = (Math.round(n) / multiplicator).toFixed(digits); + if (negative) { + n = (n * -1).toFixed(digits); + } + return n; +} + /** * Entry point function called by the simulation engine. * @@ -70,8 +88,9 @@ function main(context, previousState) { // the telemetry can apply changes using the previous function state. restoreState(previousState); - // 0.1 miles around some location - var coords = varylocation(center_latitude, center_longitude, 0.1); + // Between -1.5 and 1.5 miles around start location + var distance = roundTo(vary(0.05, 2500, -1.5, 1.5), 2); + var coords = varylocation(center_latitude, center_longitude, distance); state.latitude = coords.latitude; state.longitude = coords.longitude; From 7f8d8da331ea59c2c9ca6ae36692070f9178540b Mon Sep 17 00:00:00 2001 From: Joel Hulen Date: Sun, 21 Jan 2018 21:09:17 -0500 Subject: [PATCH 2/2] Improved gps location simulation --- .../devicemodels/scripts/truck-02-state.js | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Services/data/devicemodels/scripts/truck-02-state.js b/Services/data/devicemodels/scripts/truck-02-state.js index 54eb2c48..7e26d70e 100644 --- a/Services/data/devicemodels/scripts/truck-02-state.js +++ b/Services/data/devicemodels/scripts/truck-02-state.js @@ -52,11 +52,29 @@ function varylocation(latitude, longitude, distance) { // Convert to meters, use Earth radius, convert to radians var radians = (distance * 1609.344 / 6378137) * (180 / Math.PI); return { - latitude: latitude + radians, - longitude: longitude + radians / Math.cos(latitude * Math.PI / 180) + latitude: roundTo((latitude + radians), 6), + longitude: roundTo((longitude + radians / Math.cos(latitude * Math.PI / 180)), 6) }; } +function roundTo(n, digits) { + var negative = false; + if (digits === undefined) { + digits = 0; + } + if (n < 0) { + negative = true; + n = n * -1; + } + var multiplicator = Math.pow(10, digits); + n = parseFloat((n * multiplicator).toFixed(11)); + n = (Math.round(n) / multiplicator).toFixed(digits); + if (negative) { + n = (n * -1).toFixed(digits); + } + return n; +} + /** * Entry point function called by the simulation engine. * @@ -70,8 +88,9 @@ function main(context, previousState) { // the telemetry can apply changes using the previous function state. restoreState(previousState); - // 0.1 miles around some location - var coords = varylocation(center_latitude, center_longitude, 0.1); + // Between -1.5 and 1.5 miles around start location + var distance = roundTo(vary(0.05, 2500, -1.5, 1.5), 2); + var coords = varylocation(center_latitude, center_longitude, distance); state.latitude = coords.latitude; state.longitude = coords.longitude;