Run: fixed distance calculation
parent
3b80c5e6a6
commit
8470049601
|
|
@ -5,6 +5,7 @@ var fontHeading = "6x8:2";
|
||||||
var fontValue = B2 ? "6x15:2" : "6x8:3";
|
var fontValue = B2 ? "6x15:2" : "6x8:3";
|
||||||
var headingCol = "#888";
|
var headingCol = "#888";
|
||||||
var running = false;
|
var running = false;
|
||||||
|
var fixCount = 0;
|
||||||
var startTime;
|
var startTime;
|
||||||
var startSteps;
|
var startSteps;
|
||||||
// This & previous GPS readings
|
// This & previous GPS readings
|
||||||
|
|
@ -102,8 +103,6 @@ var layout = new Layout( {
|
||||||
clearState();
|
clearState();
|
||||||
layout.render();
|
layout.render();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function onTimer() {
|
function onTimer() {
|
||||||
layout.clock.label = locale.time(new Date(),1);
|
layout.clock.label = locale.time(new Date(),1);
|
||||||
if (!running) {
|
if (!running) {
|
||||||
|
|
@ -125,21 +124,36 @@ function onTimer() {
|
||||||
stepHistory[0]=0;
|
stepHistory[0]=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function radians(a) {
|
||||||
|
return a*Math.PI/180;
|
||||||
|
}
|
||||||
|
|
||||||
|
// distance between 2 lat and lons, in meters, Mean Earth Radius = 6371km
|
||||||
|
// https://www.movable-type.co.uk/scripts/latlong.html
|
||||||
|
function calcDistance(a,b) {
|
||||||
|
var x = radians(a.lon-b.lon) * Math.cos(radians((a.lat+b.lat)/2));
|
||||||
|
var y = radians(b.lat-a.lat);
|
||||||
|
return Math.round(Math.sqrt(x*x + y*y) * 6371000);
|
||||||
|
}
|
||||||
|
|
||||||
Bangle.on("GPS", function(fix) {
|
Bangle.on("GPS", function(fix) {
|
||||||
layout.gps.bgCol = fix.fix ? "#0f0" : "#f00";
|
layout.gps.bgCol = fix.fix ? "#0f0" : "#f00";
|
||||||
lastGPS = thisGPS;
|
if (!fix.fix) { return; } // only process actual fixes
|
||||||
|
if (fixCount++ == 0) {
|
||||||
|
Bangle.buzz(); // first fix, does not need to respect quiet mode
|
||||||
|
lastGPS = fix; // initialise on first fix
|
||||||
|
}
|
||||||
|
|
||||||
thisGPS = fix;
|
thisGPS = fix;
|
||||||
if (running && fix.fix && lastGPS.fix) {
|
|
||||||
// work out distance - moving from a to b
|
if (running) {
|
||||||
var a = Bangle.project(lastGPS);
|
var d = calcDistance(lastGPS, thisGPS);
|
||||||
var b = Bangle.project(thisGPS);
|
|
||||||
var dx = a.x-b.x, dy = a.y-b.y;
|
|
||||||
var d = Math.sqrt(dx*dx+dy*dy); // this should be the distance in meters
|
|
||||||
distance += d;
|
distance += d;
|
||||||
layout.dist.label=locale.distance(distance);
|
layout.dist.label=locale.distance(distance);
|
||||||
var duration = Date.now() - startTime; // in ms
|
var duration = Date.now() - startTime; // in ms
|
||||||
var speed = distance * 1000 / duration; // meters/sec
|
var speed = distance * 1000 / duration; // meters/sec
|
||||||
layout.pace.label = formatPace(speed);
|
layout.pace.label = formatPace(speed);
|
||||||
|
lastGPS = fix;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Bangle.on("HRM", function(h) {
|
Bangle.on("HRM", function(h) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue