From fbccb2acc9002cdb99971703d5fb4b6aeb10bab7 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 30 Nov 2026 18:48:20 +0100 Subject: [PATCH] GNSS follow glides the marker and the camera linearly to each fix over the poll interval instead of jumping; the simulation sends a fix every 0.5 s --- lib/maptool/gnss_sim.rb | 4 ++-- public/js/gnss.js | 22 +++++++++++++++++++--- test/test_gnss_sim.rb | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/maptool/gnss_sim.rb b/lib/maptool/gnss_sim.rb index e2bd650..fc53263 100644 --- a/lib/maptool/gnss_sim.rb +++ b/lib/maptool/gnss_sim.rb @@ -3,12 +3,12 @@ require_relative "routing" module Maptool # Simulated driving, to try the dashboard and navigation without the car: # from a start point it routes (our Valhalla) to a random point 2–8 km - # away and drives there at the road's speed limit, a fix a second, then + # away and drives there at the road's speed limit, a fix every TICK_S, then # on to the next random point. Its fixes go where the GNSS node's do # (app.rb: the CAN store's gnss.* signals and GNSS_LATEST), the node's own # are ignored meanwhile. class GnssSim - TICK_S = 1.0 + TICK_S = 0.5 # finer than the clients poll (1.5 s), so each poll sees a fresh fix DEST_KM = (2.0..8.0) NO_LIMIT_KMH = 50 # an untagged road UNLIMITED_KMH = 130 diff --git a/public/js/gnss.js b/public/js/gnss.js index 78a3727..4ac168d 100644 --- a/public/js/gnss.js +++ b/public/js/gnss.js @@ -14,16 +14,32 @@ const POLL_INTERVAL_MS = 1500; // geolocation). In the 3D (pitched) view it turns heading-up like a car // navigation, with the position in the lower part of the screen; the last // heading stays while standing still (speed in m/s, heading in degrees). -export function followPosition(map, lngLat, heading, speed) { +// duration (ms): a linear glide over that long — the time until the next +// fix, so a 1 Hz receiver moves the camera smoothly, not in 10 m jumps. +export function followPosition(map, lngLat, heading, speed, duration) { const pitched = map.getPitch() > 0; const moving = Number.isFinite(heading) && (speed ?? 0) >= 1; map.easeTo({ center: lngLat, padding: { top: pitched ? map.getContainer().clientHeight * 0.4 : 0 }, ...(pitched && moving && { bearing: heading }), + ...(duration != null && { duration, easing: (t) => t }), }); } +// The marker moved to lngLat the same way: a linear glide over duration ms. +function glideMarker(marker, lngLat, duration) { + cancelAnimationFrame(marker._glide); + const from = marker.getLngLat(); + const start = performance.now(); + const step = (now) => { + const t = Math.min(1, (now - start) / duration); + marker.setLngLat([from.lng + (lngLat[0] - from.lng) * t, from.lat + (lngLat[1] - from.lat) * t]); + if (t < 1) marker._glide = requestAnimationFrame(step); + }; + marker._glide = requestAnimationFrame(step); +} + export class GnssControl { constructor(map) { this._map = map; @@ -122,8 +138,8 @@ export class GnssControl { this._marker = new Marker({ element: el }).setLngLat(lngLat).addTo(this._map); this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) }); } else { - this._marker.setLngLat(lngLat); - followPosition(this._map, lngLat, data.track, data.speed); + glideMarker(this._marker, lngLat, POLL_INTERVAL_MS); + followPosition(this._map, lngLat, data.track, data.speed, POLL_INTERVAL_MS); } } diff --git a/test/test_gnss_sim.rb b/test/test_gnss_sim.rb index e804fb0..cec1946 100644 --- a/test/test_gnss_sim.rb +++ b/test/test_gnss_sim.rb @@ -19,6 +19,6 @@ class GnssSimTest < Minitest::Test assert_in_delta 48.0 + 1000.0 / 111_320, fixes.last[:lat], 1e-9 assert_equal [17.0, 48.0 + 1000.0 / 111_320], last assert fixes.each_cons(2).all? { |a, b| b[:lat] >= a[:lat] } # never backwards - assert_in_delta 1000 / (36 / 3.6) / 2 + 500 / (45 / 3.6), fixes.size, 3 # ~1 fix a second + assert_in_delta (1000 / (36 / 3.6) / 2 + 500 / (45 / 3.6)) / Maptool::GnssSim::TICK_S, fixes.size, 3 # a fix every TICK_S end end