fix: smooth GPS-derived replay speed to kill jitter spikes
route_points (Pi) and get_trip_route (server) derived GPS speed from a single 5s segment, so GPS position jitter could read 150+ km/h when the car wasn't going that fast. Both now average over a trailing ~30s window (same as the max-speed calc), so spikes are damped.
This commit is contained in:
+15
-8
@@ -625,6 +625,7 @@ def get_trip_route(device_id, trip_id):
|
||||
result_points = []
|
||||
cumulative = 0.0
|
||||
prev = None
|
||||
gps_window = [] # trailing (lat, lon, datetime) points for GPS speed smoothing
|
||||
for p in points:
|
||||
lat = float(p[1])
|
||||
lon = float(p[2])
|
||||
@@ -632,18 +633,24 @@ def get_trip_route(device_id, trip_id):
|
||||
raw_odo = p[4]
|
||||
speed_source = "obd" if speed > 0 else "gps"
|
||||
# Mirror the Pi's route_points GPS-speed fallback: when OBD speed is
|
||||
# missing (adapter dropout), derive speed from the previous point's
|
||||
# movement so the replay doesn't flatline at 0 km/h while the car moves.
|
||||
if speed <= 0 and prev is not None:
|
||||
seconds = (p[0] - prev[2]).total_seconds()
|
||||
# missing (adapter dropout), derive speed from a trailing ~30s window
|
||||
# of movement (not the previous point) so the replay doesn't flatline
|
||||
# at 0 km/h while the car moves — and a single jittery 5s segment can't
|
||||
# spike the readout.
|
||||
if speed <= 0 and gps_window:
|
||||
start = gps_window[0]
|
||||
seconds = (p[0] - start[2]).total_seconds()
|
||||
if seconds > 0:
|
||||
dist_km = hav(prev[0], prev[1], lat, lon)
|
||||
seg_speed = dist_km / seconds * 3600
|
||||
if seg_speed <= 220 and dist_km * 1000 >= 8:
|
||||
speed = seg_speed
|
||||
dist_km = hav(start[0], start[1], lat, lon)
|
||||
w_speed = dist_km / seconds * 3600
|
||||
if w_speed <= 220 and dist_km * 1000 >= 8:
|
||||
speed = w_speed
|
||||
if prev is not None:
|
||||
cumulative += hav(prev[0], prev[1], lat, lon)
|
||||
prev = (lat, lon, p[0])
|
||||
gps_window.append((lat, lon, p[0]))
|
||||
if len(gps_window) > 6:
|
||||
gps_window.pop(0)
|
||||
if raw_odo is not None and float(raw_odo) > 0:
|
||||
odometer = float(raw_odo)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user