Skip to content

Commit 2b014d9

Browse files
committed
get average rates
1 parent eb0718c commit 2b014d9

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

js/tracker.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,7 @@ function addPosition(position) {
21022102
positions: [],
21032103
positions_ts: [],
21042104
positions_ids: [],
2105+
positions_alts: [],
21052106
path_length: 0,
21062107
curr_position: position,
21072108
line: [],
@@ -2210,6 +2211,7 @@ function addPosition(position) {
22102211
var new_latlng = new L.LatLng(position.gps_lat, position.gps_lon);
22112212
var new_ts = convert_time(position.gps_time);
22122213
var curr_ts = convert_time(vehicle.curr_position.gps_time);
2214+
var new_alt = position.gps_alt;
22132215
var dt = (new_ts - curr_ts) / 1000; // convert to seconds
22142216

22152217
if (typeof position.type !== 'undefined' && typeof vehicle.curr_position.type !== 'undefined') {
@@ -2220,18 +2222,39 @@ function addPosition(position) {
22202222

22212223
if(dt >= 0) {
22222224
if(vehicle.num_positions > 0) {
2223-
// calculate vertical rate
2224-
// TODO - Make this average over more points rather than use a FIR.
2225-
var rate = (position.gps_alt - vehicle.curr_position.gps_alt) / dt;
2226-
if (!isNaN(rate) && dt != 0) {
2227-
vehicle.ascent_rate = 0.7 * rate + 0.3 * vehicle.ascent_rate;
2225+
2226+
//average over 5s if available
2227+
var old_ts = vehicle.positions_ts[vehicle.positions_ts.length - 5];
2228+
var dtt = (new_ts - old_ts) / 1000; // convert to seconds
2229+
2230+
if (vehicle.positions_ts.length < 5) {
2231+
dtt = 1000;
22282232
}
22292233

2234+
// calculate vertical rate
2235+
if (dtt > 10) {
2236+
var rate = (position.gps_alt - vehicle.curr_position.gps_alt) / dt;
2237+
if (!isNaN(rate) && dt != 0) {
2238+
vehicle.ascent_rate = 0.7 * rate + 0.3 * vehicle.ascent_rate;
2239+
}
2240+
} else {
2241+
var rate = (position.gps_alt - vehicle.positions_alts[vehicle.positions_alts.length - 5]) / dtt;
2242+
if (!isNaN(rate)) {
2243+
vehicle.ascent_rate = 0.7 * rate + 0.3 * vehicle.ascent_rate;
2244+
}
2245+
}
22302246

22312247
// calculate horizontal rate
2232-
horizontal_rate_temp = new_latlng.distanceTo(new L.LatLng(vehicle.curr_position.gps_lat, vehicle.curr_position.gps_lon)) / dt;
2233-
if (!isNaN(horizontal_rate_temp) && dt != 0) {
2234-
vehicle.horizontal_rate = horizontal_rate_temp;
2248+
if (dtt > 10) {
2249+
horizontal_rate_temp = new_latlng.distanceTo(new L.LatLng(vehicle.curr_position.gps_lat, vehicle.curr_position.gps_lon)) / dt;
2250+
if (!isNaN(horizontal_rate_temp) && dt != 0) {
2251+
vehicle.horizontal_rate = horizontal_rate_temp;
2252+
}
2253+
} else {
2254+
horizontal_rate_temp = new_latlng.distanceTo(vehicle.positions[vehicle.positions.length - 5]) / dtt;
2255+
if (!isNaN(horizontal_rate_temp)) {
2256+
vehicle.horizontal_rate = horizontal_rate_temp;
2257+
}
22352258
}
22362259
}
22372260

@@ -2244,6 +2267,7 @@ function addPosition(position) {
22442267
vehicle.positions.push(new_latlng);
22452268
vehicle.positions_ts.push(new_ts);
22462269
vehicle.positions_ids.push(position.position_id);
2270+
vehicle.positions_alts.push(new_alt)
22472271
vehicle.num_positions++;
22482272
}
22492273

@@ -2324,6 +2348,7 @@ function addPosition(position) {
23242348

23252349
// insert the new position into our arrays
23262350
vehicle.positions.splice(idx, 0, new_latlng);
2351+
vehicle.positions_alts.splice(idx, 0, new_alt);
23272352
vehicle.positions_ts.splice(idx, 0, new_ts);
23282353
vehicle.positions_ids.splice(idx, 0, position.position_id);
23292354
vehicle.num_positions++;

0 commit comments

Comments
 (0)