Skip to content

Commit d49f7d0

Browse files
authored
Fix rate calculations (projecthorus#37)
1 parent bbff536 commit d49f7d0

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

js/tracker.js

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,42 +2582,19 @@ function addPosition(position) {
25822582
}
25832583

25842584
if(dt >= 0) {
2585-
if(vehicle.num_positions > 0) {
2586-
2587-
//average over 5s if available
2588-
var old_ts = vehicle.positions_ts[vehicle.positions_ts.length - 5];
2589-
var dtt = (new_ts - old_ts) / 1000; // convert to seconds
2590-
2591-
if (vehicle.positions_ts.length < 5) {
2592-
dtt = 1000;
2593-
}
2594-
2585+
if(vehicle.num_positions > 0 && dt > 0) {
25952586
// calculate vertical rate
2596-
if (dtt > 10) {
2597-
var rate = (position.gps_alt - vehicle.curr_position.gps_alt) / dt;
2598-
if (!isNaN(rate) && isFinite(rate) && dt != 0) {
2599-
vehicle.ascent_rate = 0.7 * rate + 0.3 * vehicle.ascent_rate;
2600-
}
2601-
} else {
2602-
var rate = (position.gps_alt - vehicle.positions_alts[vehicle.positions_alts.length - 5]) / dtt;
2603-
if (!isNaN(rate) && isFinite(rate)) {
2604-
vehicle.ascent_rate = 0.7 * rate + 0.3 * vehicle.ascent_rate;
2605-
}
2587+
var rate = (position.gps_alt - vehicle.curr_position.gps_alt) / dt;
2588+
if (!isNaN(rate) && isFinite(rate)) {
2589+
vehicle.ascent_rate = 0.7 * rate + 0.3 * vehicle.ascent_rate;
26062590
}
26072591

26082592
// calculate horizontal rate
2609-
if (dtt > 10) {
2610-
horizontal_rate_temp = new_latlng.distanceTo(new L.LatLng(vehicle.curr_position.gps_lat, vehicle.curr_position.gps_lon)) / dt;
2611-
if (!isNaN(horizontal_rate_temp) && isFinite(horizontal_rate_temp) && dt != 0) {
2612-
vehicle.horizontal_rate = horizontal_rate_temp;
2613-
}
2614-
} else {
2615-
horizontal_rate_temp = new_latlng.distanceTo(vehicle.positions[vehicle.positions.length - 5]) / dtt;
2616-
if (!isNaN(horizontal_rate_temp)) {
2617-
vehicle.horizontal_rate = horizontal_rate_temp;
2618-
}
2593+
horizontal_rate_temp = new_latlng.distanceTo(new L.LatLng(vehicle.curr_position.gps_lat, vehicle.curr_position.gps_lon)) / dt;
2594+
if (!isNaN(horizontal_rate_temp) && isFinite(horizontal_rate_temp)) {
2595+
vehicle.horizontal_rate = horizontal_rate_temp;
26192596
}
2620-
}
2597+
}
26212598

26222599
// add the new position
26232600
if(wvar.mode == "Position") {

0 commit comments

Comments
 (0)