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
This commit is contained in:
1 parent
8680cdbee7
commit
fbccb2acc9
3 files changed
+22
-6
No files matched your search
@@ -3,12 +3,12 @@ require_relative "routing"
|
|||||||
module Maptool
|
module Maptool
|
||||||
# Simulated driving, to try the dashboard and navigation without the car:
|
# 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
|
# 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
|
# 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
|
# (app.rb: the CAN store's gnss.* signals and GNSS_LATEST), the node's own
|
||||||
# are ignored meanwhile.
|
# are ignored meanwhile.
|
||||||
class GnssSim
|
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)
|
DEST_KM = (2.0..8.0)
|
||||||
NO_LIMIT_KMH = 50 # an untagged road
|
NO_LIMIT_KMH = 50 # an untagged road
|
||||||
UNLIMITED_KMH = 130
|
UNLIMITED_KMH = 130
|
||||||
|
|||||||
+19
-3
@@ -14,16 +14,32 @@ const POLL_INTERVAL_MS = 1500;
|
|||||||
// geolocation). In the 3D (pitched) view it turns heading-up like a car
|
// 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
|
// 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).
|
// 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 pitched = map.getPitch() > 0;
|
||||||
const moving = Number.isFinite(heading) && (speed ?? 0) >= 1;
|
const moving = Number.isFinite(heading) && (speed ?? 0) >= 1;
|
||||||
map.easeTo({
|
map.easeTo({
|
||||||
center: lngLat,
|
center: lngLat,
|
||||||
padding: { top: pitched ? map.getContainer().clientHeight * 0.4 : 0 },
|
padding: { top: pitched ? map.getContainer().clientHeight * 0.4 : 0 },
|
||||||
...(pitched && moving && { bearing: heading }),
|
...(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 {
|
export class GnssControl {
|
||||||
constructor(map) {
|
constructor(map) {
|
||||||
this._map = map;
|
this._map = map;
|
||||||
@@ -122,8 +138,8 @@ export class GnssControl {
|
|||||||
this._marker = new Marker({ element: el }).setLngLat(lngLat).addTo(this._map);
|
this._marker = new Marker({ element: el }).setLngLat(lngLat).addTo(this._map);
|
||||||
this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) });
|
this._map.flyTo({ center: lngLat, zoom: Math.max(this._map.getZoom(), 15) });
|
||||||
} else {
|
} else {
|
||||||
this._marker.setLngLat(lngLat);
|
glideMarker(this._marker, lngLat, POLL_INTERVAL_MS);
|
||||||
followPosition(this._map, lngLat, data.track, data.speed);
|
followPosition(this._map, lngLat, data.track, data.speed, POLL_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ class GnssSimTest < Minitest::Test
|
|||||||
assert_in_delta 48.0 + 1000.0 / 111_320, fixes.last[:lat], 1e-9
|
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_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 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
|
||||||
end
|
end
|
||||||
Reference in new issue
Block a user