Skip to content

Commit 0b08994

Browse files
authored
More efficient rate calculation (#40)
1 parent ad93a3b commit 0b08994

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

js/tracker.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,24 +2585,29 @@ function addPosition(position) {
25852585
if(vehicle.num_positions > 0 && dt > 0) {
25862586
var search_ts = new_ts - 10000
25872587

2588-
function searchPositions(time) {
2589-
return time <= search_ts
2588+
function searchPositions(times) {
2589+
for (i = times.length; i >= 0; i--) {
2590+
if (times[i] <= search_ts) {
2591+
return times[i]
2592+
}
2593+
}
2594+
return null
25902595
}
25912596

2592-
var search_matches = vehicle.positions_ts.filter(searchPositions)
2597+
var search_match = searchPositions(vehicle.positions_ts)
2598+
var search_index = vehicle.positions_ts.indexOf(search_match)
25932599

2594-
if (search_matches.length > 0 && search_matches[search_matches.length-1] >= search_ts - 5000) {
2595-
var search_match = search_matches[search_matches.length-1]
2600+
if (search_match != null && search_match >= search_ts - 5000) {
25962601
var dtt = (curr_ts - search_match) / 1000;
25972602

25982603
// calculate vertical rate
2599-
var rate = (position.gps_alt - vehicle.positions_alts[search_matches.length-1]) / dtt;
2604+
var rate = (position.gps_alt - vehicle.positions_alts[search_index]) / dtt;
26002605
if (!isNaN(rate) && isFinite(rate)) {
26012606
vehicle.ascent_rate = 0.2 * rate + 0.8 * vehicle.ascent_rate;
26022607
}
26032608

26042609
// calculate horizontal rate
2605-
horizontal_rate_temp = new_latlng.distanceTo(vehicle.positions[search_matches.length-1]) / dtt;
2610+
horizontal_rate_temp = new_latlng.distanceTo(vehicle.positions[search_index]) / dtt;
26062611
if (!isNaN(horizontal_rate_temp) && isFinite(horizontal_rate_temp)) {
26072612
vehicle.horizontal_rate = horizontal_rate_temp;
26082613
}

0 commit comments

Comments
 (0)