Navigation: rerouting off the route (30 m for 3 fixes, HDOP ≤ 5, every 15 s at most) from the position to the points not reached yet; lanes as SVG arrows, the ones to take bold; dashboard background see-through with a slider; the GNSS simulation starts at a random place in the offline countries, the map jumping there
This commit is contained in:
1 parent
fbccb2acc9
commit
5f4c7c6ee8
9 files changed
+176
-35
No files matched your search
+70
-15
@@ -1,5 +1,5 @@
|
||||
import { isLoggedIn } from "/js/auth.js";
|
||||
import { onRouteChanged } from "/js/panels.js";
|
||||
import { onRouteChanged, requestReroute } from "/js/panels.js";
|
||||
|
||||
// The car's dashboard over the map, off the CAN signals (GET /api/can,
|
||||
// names from data/can.json): one inclinometer — compass ring around a rear
|
||||
@@ -9,7 +9,7 @@ import { onRouteChanged } from "/js/panels.js";
|
||||
// Heading: driving, the GNSS course (from consecutive fixes); standing
|
||||
// still, the compass (imu.heading, once the node sends it) — else the
|
||||
// last course stays, dimmed.
|
||||
const SIGNALS = { pitch: "imu.pitch", roll: "imu.roll", heading: "imu.heading", speed: "gnss.speed", lat: "gnss.lat", lon: "gnss.lon" };
|
||||
const SIGNALS = { pitch: "imu.pitch", roll: "imu.roll", heading: "imu.heading", speed: "gnss.speed", lat: "gnss.lat", lon: "gnss.lon", hdop: "gnss.hdop" };
|
||||
const MOVING_MS = 1.5; // m/s: above this the GNSS course counts
|
||||
const COURSE_MIN_M = 5; // fixes closer than this: too noisy for a course
|
||||
const POLL_MS = 500;
|
||||
@@ -20,11 +20,19 @@ const STORE_KEY = "maptool.dashboard";
|
||||
// while on it (its speed_limits / signals / maneuvers from /api/route),
|
||||
// else the limit of the road being driven (POST /api/road, map-matched).
|
||||
const OVER_KMH = 3; // over the limit by more than this: warning
|
||||
const ON_ROUTE_M = 50; // farther from the route line: off it
|
||||
// Off the route → rerouting, after Ferrostar's split: deviation detected
|
||||
// (farther than ON_ROUTE_M for OFF_ROUTE_FIXES fixes in a row, fixes with an
|
||||
// HDOP over MAX_HDOP don't count), then a new route from the position to
|
||||
// the points not reached yet — at most every REROUTE_EVERY_MS, only moving.
|
||||
const ON_ROUTE_M = 30; // farther from the route line: off it
|
||||
const OFF_ROUTE_FIXES = 3;
|
||||
const MAX_HDOP = 5; // ~ over 10 m off at the NEO-6M's few m UERE
|
||||
const REROUTE_EVERY_MS = 15000;
|
||||
const ROAD_EVERY_MS = 4000; // /api/road at most this often, only while moving
|
||||
const TRACE_STEP_M = 10; // fixes kept for /api/road at least this far apart
|
||||
const LANES_AHEAD_M = 1000; // the next maneuver's lanes from this close
|
||||
const DEFAULT_WIDGETS = { inclinometer: true, speed: true, navigation: true };
|
||||
const DEFAULT_OPACITY = 0.45; // the card's background, the map shows through
|
||||
|
||||
const load = () => {
|
||||
try { const saved = JSON.parse(localStorage.getItem(STORE_KEY)) || {}; return { ...saved, widgets: { ...DEFAULT_WIDGETS, ...saved.widgets } }; }
|
||||
@@ -35,9 +43,23 @@ const RAD = Math.PI / 180;
|
||||
// meters between two [lon, lat] (equirectangular: fine at these distances)
|
||||
const meters = ([lon1, lat1], [lon2, lat2]) => Math.hypot((lon2 - lon1) * Math.cos(((lat1 + lat2) / 2) * RAD), lat2 - lat1) * 111320;
|
||||
const distance = (m) => (m >= 1000 ? `${(m / 1000).toFixed(1)} km` : `${Math.round(m / 10) * 10} m`);
|
||||
// Valhalla's lane direction bits → arrows
|
||||
const LANE_ARROWS = [[2, "↑"], [16, "↖"], [8, "←"], [4, "↙"], [32, "↗"], [64, "→"], [128, "↘"], [256, "↶"], [512, "⇤"], [1024, "⇥"]];
|
||||
const laneArrows = (bits) => LANE_ARROWS.filter(([bit]) => bits & bit).map(([, arrow]) => arrow).join("") || "↑";
|
||||
// Valhalla's lane direction bits → the arrow's angle (° from straight on)
|
||||
const LANE_ANGLES = { 2: 0, 16: -45, 8: -90, 4: -135, 32: 45, 64: 90, 128: 135, 512: -25, 1024: 25 };
|
||||
const LANE_REVERSE = 256;
|
||||
// One lane as an SVG: an arrow per direction it allows; `active` (the
|
||||
// directions to take, a bitmask too) drawn bold.
|
||||
function laneSvg({ directions, active = 0 }) {
|
||||
const arrow = (bit) => {
|
||||
const bold = active & bit ? " dash-lane__take" : "";
|
||||
if (bit === LANE_REVERSE) return `<path class="dash-lane__arrow${bold}" d="M14 30 V14 A5 5 0 0 0 4 14 V20 M1 17 L4 21 L7 17"/>`;
|
||||
const a = (LANE_ANGLES[bit] * Math.PI) / 180;
|
||||
const [x, y] = [12 + 9 * Math.sin(a), 15 - 9 * Math.cos(a)];
|
||||
const head = (side) => `L${(x - 5 * Math.sin(a + side)).toFixed(1)} ${(y + 5 * Math.cos(a + side)).toFixed(1)}`;
|
||||
return `<path class="dash-lane__arrow${bold}" d="M12 30 V15 L${x.toFixed(1)} ${y.toFixed(1)} M${(x - 5 * Math.sin(a + 0.6)).toFixed(1)} ${(y + 5 * Math.cos(a + 0.6)).toFixed(1)} L${x.toFixed(1)} ${y.toFixed(1)} ${head(-0.6)}"/>`;
|
||||
};
|
||||
const bits = [2, 16, 8, 4, 32, 64, 128, 512, 1024, LANE_REVERSE].filter((bit) => directions & bit);
|
||||
return `<svg viewBox="0 0 24 32" class="dash-lane${active ? " dash-lane--go" : ""}">${(bits.length ? bits : [2]).map(arrow).join("")}</svg>`;
|
||||
}
|
||||
|
||||
// A route prepared for "where am I on it": km at every vertex, maneuvers' start km.
|
||||
function prepareRoute(route) {
|
||||
@@ -47,7 +69,9 @@ function prepareRoute(route) {
|
||||
for (let i = 1; i < coords.length; i++) km.push(km[i - 1] + meters(coords[i - 1], coords[i]) / 1000);
|
||||
let start = 0;
|
||||
const maneuvers = route.properties.maneuvers.map((m) => { const at = start; start += m.km; return { ...m, at }; });
|
||||
return { coords, km, limits: route.properties.speed_limits, signals: route.properties.signals || [], maneuvers };
|
||||
let leg = 0;
|
||||
const viaKm = (route.properties.legs_km || []).slice(0, -1).map((length) => (leg += length)); // km at each via point
|
||||
return { coords, km, limits: route.properties.speed_limits, signals: route.properties.signals || [], maneuvers, viaKm };
|
||||
}
|
||||
|
||||
// Nearest point on the route: [km along it, meters off it].
|
||||
@@ -105,10 +129,17 @@ export class DashboardControl {
|
||||
this._values = {};
|
||||
this._fix = null; // last fix a course was measured from
|
||||
this._course = null;
|
||||
this._route = prepareRoute(onRouteChanged((route) => (this._route = prepareRoute(route))));
|
||||
this._route = prepareRoute(onRouteChanged((route) => {
|
||||
this._route = prepareRoute(route);
|
||||
this._lastAt = 0;
|
||||
this._offFixes = 0;
|
||||
}));
|
||||
this._trace = []; // [lon, lat] fixes TRACE_STEP_M apart, for /api/road
|
||||
this._road = null; // its last answer
|
||||
this._roadAt = 0;
|
||||
this._lastAt = 0; // km along the route at the last fix on it
|
||||
this._offFixes = 0;
|
||||
this._rerouteAt = 0;
|
||||
}
|
||||
|
||||
// The limit and what's ahead: from the route while on it, else the road.
|
||||
@@ -119,12 +150,17 @@ export class DashboardControl {
|
||||
if (!this._trace.length || meters(this._trace.at(-1), position) >= TRACE_STEP_M) this._trace = [...this._trace.slice(-7), position];
|
||||
|
||||
const [at, off] = this._route ? locate(this._route, position) : [0, Infinity];
|
||||
if (off <= ON_ROUTE_M) {
|
||||
const fresh = position.join() !== this._lastFix; // polled faster than the fixes come
|
||||
this._lastFix = position.join();
|
||||
if (fresh && this._route && off > ON_ROUTE_M && (this._values.hdop ?? 0) <= MAX_HDOP) this._offFixes++;
|
||||
else if (off <= ON_ROUTE_M) this._offFixes = 0;
|
||||
if (this._route && this._offFixes < OFF_ROUTE_FIXES) {
|
||||
const { limits, signals, maneuvers } = this._route;
|
||||
const current = limits.findLast(([km]) => km <= at);
|
||||
const next = limits.find(([km, limit]) => km > at && limit !== current?.[1]);
|
||||
const signal = signals.find(([, , km]) => km > at);
|
||||
const maneuver = maneuvers.find((m) => m.at > at + 0.005);
|
||||
if (off <= ON_ROUTE_M) this._lastAt = at;
|
||||
this._nav = {
|
||||
limit: current?.[1] ?? null,
|
||||
next: next && { limit: next[1], m: (next[0] - at) * 1000 },
|
||||
@@ -133,7 +169,11 @@ export class DashboardControl {
|
||||
};
|
||||
return;
|
||||
}
|
||||
this._nav = { limit: this._road?.speed_limit ?? null };
|
||||
this._nav = { limit: this._road?.speed_limit ?? null, offRoute: !!this._route };
|
||||
if (this._route && (speed ?? 0) >= MOVING_MS && Date.now() - this._rerouteAt >= REROUTE_EVERY_MS) {
|
||||
this._rerouteAt = Date.now();
|
||||
requestReroute(position, this._route.viaKm.filter((km) => km <= this._lastAt).length);
|
||||
}
|
||||
if ((speed ?? 0) < MOVING_MS || this._trace.length < 2 || Date.now() - this._roadAt < ROAD_EVERY_MS) return;
|
||||
this._roadAt = Date.now();
|
||||
fetch("/api/road", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ points: this._trace }) })
|
||||
@@ -171,7 +211,8 @@ export class DashboardControl {
|
||||
<label class="live-tracker__row"><input type="checkbox" data-widget="inclinometer"> Inclinometer & compass</label>
|
||||
<label class="live-tracker__row"><input type="checkbox" data-widget="speed"> Speed</label>
|
||||
<label class="live-tracker__row"><input type="checkbox" data-widget="navigation"> Speed limit & ahead (route)</label>
|
||||
<button type="button" class="dash-sim">Simulate driving from the map center</button>
|
||||
<label class="live-tracker__row">Background <input type="range" class="dash-opacity" min="0" max="1" step="0.05"></label>
|
||||
<button type="button" class="dash-sim">Simulate driving from a random place</button>
|
||||
<p class="live-panel__status dash-sim-status" hidden></p>
|
||||
</div>`;
|
||||
this._toggle = li.querySelector(".overlays-panel__toggle");
|
||||
@@ -196,6 +237,13 @@ export class DashboardControl {
|
||||
});
|
||||
}
|
||||
this._toggle.addEventListener("change", () => (this._toggle.checked ? this._enable() : this._disable()));
|
||||
const opacity = li.querySelector(".dash-opacity");
|
||||
opacity.value = this._settings.opacity ?? DEFAULT_OPACITY;
|
||||
opacity.addEventListener("input", () => {
|
||||
this._settings.opacity = +opacity.value;
|
||||
this._save();
|
||||
this._card?.style.setProperty("--dash-alpha", opacity.value);
|
||||
});
|
||||
this._simButton = li.querySelector(".dash-sim");
|
||||
this._simStatus = li.querySelector(".dash-sim-status");
|
||||
this._simButton.addEventListener("click", () => this._simulate(!this._simRunning));
|
||||
@@ -203,11 +251,12 @@ export class DashboardControl {
|
||||
}
|
||||
|
||||
// GNSS simulation on the server (POST/DELETE /api/sim/gnss): random
|
||||
// driving from the map center; on = true / false, null = just read the state.
|
||||
// driving from a random place in the offline countries, the map jumping
|
||||
// there with the first fix; on = true / false, null = just read the state.
|
||||
async _simulate(on) {
|
||||
const { lng, lat } = this._map.getCenter();
|
||||
const options = on === true ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ start: [lng, lat] }) }
|
||||
const options = on === true ? { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" }
|
||||
: on === false ? { method: "DELETE" } : {};
|
||||
this._jumpToFix = on === true;
|
||||
try {
|
||||
const response = await fetch("/api/sim/gnss", options);
|
||||
const state = await response.json();
|
||||
@@ -229,6 +278,7 @@ export class DashboardControl {
|
||||
_enable() {
|
||||
this._card = document.createElement("div");
|
||||
this._card.className = "dash-card";
|
||||
this._card.style.setProperty("--dash-alpha", this._settings.opacity ?? DEFAULT_OPACITY);
|
||||
this._card.innerHTML = `<div class="dash-nav"><div class="dash-limit"></div><div class="dash-ahead"></div></div>${INCLINOMETER}<div class="dash-speed"><span></span> km/h</div>`;
|
||||
this._map.getContainer().appendChild(this._card);
|
||||
this._status.hidden = false;
|
||||
@@ -255,6 +305,10 @@ export class DashboardControl {
|
||||
// Date has 1 s resolution: +1 so a fresh value isn't "stale"
|
||||
return [key, s && now - s.at < STALE_S + 1 ? s.value : null];
|
||||
}));
|
||||
if (this._jumpToFix && this._simRunning && this._values.lat != null) {
|
||||
this._jumpToFix = false;
|
||||
this._map.jumpTo({ center: [this._values.lon, this._values.lat], zoom: Math.max(this._map.getZoom(), 15) });
|
||||
}
|
||||
this._updateCourse(this._values);
|
||||
this._navigate();
|
||||
this._status.textContent = this._simRunning ? "Simulated GNSS" : data.available ? "" : "CAN is off on this server";
|
||||
@@ -310,13 +364,14 @@ export class DashboardControl {
|
||||
sign.className = `dash-limit${limit === "unlimited" ? " dash-limit--none" : ""}`;
|
||||
sign.textContent = typeof limit === "number" ? limit : "";
|
||||
const rows = [];
|
||||
if (n?.offRoute) rows.push(`<div class="dash-offroute">Off the route — rerouting</div>`);
|
||||
if (n?.next) rows.push(`<div><span class="dash-limit dash-limit--small${n.next.limit === "unlimited" ? " dash-limit--none" : ""}">${typeof n.next.limit === "number" ? n.next.limit : n.next.limit ? "" : "?"}</span> in ${distance(n.next.m)}</div>`);
|
||||
if (n?.signal != null) rows.push(`<div><span class="dash-signal"></span> ${distance(n.signal)}</div>`);
|
||||
if (n?.maneuver) {
|
||||
const { text, m, lanes } = n.maneuver;
|
||||
rows.push(`<div class="dash-maneuver">${distance(m)} · ${text.replace(/[&<>]/g, (c) => ({ "&": "&", "<": "<", ">": ">" })[c])}</div>`);
|
||||
if (lanes && m <= LANES_AHEAD_M) {
|
||||
rows.push(`<div class="dash-lanes">${lanes.map((lane) => `<span class="${lane.active || lane.valid ? "dash-lane--go" : ""}">${laneArrows(lane.directions)}</span>`).join("")}</div>`);
|
||||
rows.push(`<div class="dash-lanes">${lanes.map(laneSvg).join("")}</div>`);
|
||||
}
|
||||
}
|
||||
nav.querySelector(".dash-ahead").innerHTML = rows.join("");
|
||||
|
||||
Reference in new issue
Block a user