diff --git a/TODO.adoc b/TODO.adoc index 9b2dcce..7bc2bc3 100644 --- a/TODO.adoc +++ b/TODO.adoc @@ -324,7 +324,8 @@ already reaches the home MQTT broker (`mqtt_ext`) from ESPHome nodes in `~/espho instead of "the first maneuver ahead by km"; arrival at 30 m. Done: progress only forward — the position looked for near the last one first (`locate`, 0.1 km back to 3 km ahead), so a route along the same road twice or crossing itself doesn't jump; the step still advances 5 m - past the maneuver. Arrival still to do + past the maneuver. Arrival: 30 m from the end "Arrived" (banner, drive mode), the navigation + goes on — driven past it, the reroute turns back to it ** [x] The navigation banner across the top (Dashboard widget, on by default): the next maneuver's arrow (Valhalla's maneuver type), distance and street, "then …" when the next one follows within 300 m, its lanes from 1 km, the street being driven; `/api/route` maneuvers @@ -337,12 +338,20 @@ already reaches the home MQTT broker (`mqtt_ext`) from ESPHome nodes in `~/espho mode's Menu, off by default ** [x] Lanes as SVG arrows, one per direction bit, the lanes to take (Valhalla's `active`) blue with that direction bold -*** [ ] Lane guidance before motorway exits (lanes several maneuvers ahead), bigger on the - driving screen -** [ ] Traffic signals with no route: around the position (Valhalla `locate` or the offline - PBF), and only for the direction driven (`traffic_signals:direction`) -** [ ] The "Navigate" follow mode for the CAN GNSS position (today the MQTT tracker and the - browser's GNSS control), the dashboard as part of the driving-mode UI +*** [x] Lane guidance before motorway exits (lanes several maneuvers ahead), bigger on the + driving screen. Done: the next maneuver's lanes from 1 km or 45 s at the speed driven, in drive + mode a cell under the maneuver (`ahead` in the profile), shown only when there's something +** [x] Traffic signals with no route: around the position (Valhalla `locate` or the offline + PBF), and only for the direction driven (`traffic_signals:direction`). Done: `/api/road` also routes + 1.5 km straight on from the last fix (leaving in the direction driven) and gives that way's signals; + the nearest one in front shown (dashboard, drive mode's `ahead` cell up to 1 km). The + `:direction` tag not yet +** [x] The "Navigate" follow mode for the CAN GNSS position (today the MQTT tracker and the + browser's GNSS control), the dashboard as part of the driving-mode UI. Done: drive mode's Follow + follows the CAN GNSS; the LTE tracker (MQTT) is a tracker layer to switch on (a second car, the car + seen from home), Meshtastic the same with the nodes picked +*** [ ] Any point of a tracker layer can be the one the map follows — one at a time (picking + another one, or the car's own GNSS, takes it over); today only the MQTT tracker's card has "Navigate" ** [x] `locate()` scans the whole route each fix — a window around the last hit on long routes ** [ ] Shared trip — one itinerary per server for every screen (the cab's display, the tablet or display in the back, phones): the co-driver adds a stop, the driver's view reroutes on the diff --git a/lib/maptool/routing.rb b/lib/maptool/routing.rb index 8787ec1..7861209 100644 --- a/lib/maptool/routing.rb +++ b/lib/maptool/routing.rb @@ -328,11 +328,37 @@ module Maptool shape: points.map { |lon, lat| { lon: Float(lon), lat: Float(lat) } }, costing: "auto", shape_match: "map_snap", filters: { attributes: %w[edge.names edge.road_class edge.speed_limit edge.surface], action: "include" }) edge = data["edges"]&.last or raise RouteError, "no road here" - { name: edge["names"]&.first, road_class: edge["road_class"], speed_limit: speed_limit(edge["speed_limit"]), surface: edge["surface"] }.compact + { name: edge["names"]&.first, road_class: edge["road_class"], speed_limit: speed_limit(edge["speed_limit"]), surface: edge["surface"], + signals: signals_ahead(valhalla_url, points) }.compact rescue ArgumentError, TypeError raise RouteError, "invalid coordinates" end + AHEAD_KM = 1.5 + + # The traffic signals on the road ahead with no route: a route from the + # last fix, leaving in the direction driven, to AHEAD_KM straight on; + # [[lon, lat, km from the fix], ...], nil if that fails (it's extra). + # ponytail: "straight on" picks the way at a junction, a signal past a + # turn the driver then takes is missed until the next fix's lookup + def self.signals_ahead(valhalla_url, points) + (lon0, lat0), (lon, lat) = points.last(2).map { |p| p.map { |v| Float(v) } } + rad = Math::PI / 180 + dx = (lon - lon0) * Math.cos(lat * rad) + dy = lat - lat0 + return nil if dx.zero? && dy.zero? + + heading = (Math.atan2(dx, dy) / rad) % 360 + deg = AHEAD_KM / 111.32 + to = { lon: lon + deg * Math.sin(heading * rad) / Math.cos(lat * rad), lat: lat + deg * Math.cos(heading * rad) } + data = valhalla(valhalla_url, "/route", locations: [{ lon: lon, lat: lat, heading: heading.round, heading_tolerance: 45 }, to], + costing: "auto", directions_type: "none") + along_route(valhalla_url, data.dig("trip", "legs"), "auto", {})&.dig(:signals) + rescue RouteError, Unavailable, Net::ReadTimeout, NoMethodError, TypeError, ArgumentError + nil + end + private_class_method :signals_ahead + ISOCHRONE_MAX_MINUTES = 240 # Valhalla's own default limit # How far the vehicle gets from [lon, lat] in each of `minutes`: a diff --git a/public/css/style.css b/public/css/style.css index fc76e7b..faafab3 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -3614,6 +3614,14 @@ html:not(.driving) .drive-cell { display: none; } .drive-cell--limit { color: var(--sign-ring); } .drive-cell--maneuver, .drive-cell--compass { flex-direction: row; align-items: center; justify-content: flex-start; gap: 12px; } +/* under the maneuver, over the map: the lanes (bigger than the desktop's) and the next signal */ +.drive-cell--ahead { position: relative; z-index: 2; align-self: start; height: var(--cell, 80px); flex-direction: row; align-items: center; justify-content: center; gap: calc(16px * var(--s, 1)); } +.drive-cell--ahead[hidden] { display: none; } +.drive-ahead__lanes svg.dash-lane { width: calc(40px * var(--s, 1)); height: calc(54px * var(--s, 1)); } +.drive-ahead__signal { display: flex; align-items: center; gap: 8px; font-size: calc(24px * var(--s, 1)); font-weight: 700; font-variant-numeric: tabular-nums; } +.drive-ahead__signal .dash-signal { width: calc(14px * var(--s, 1)); height: calc(36px * var(--s, 1)); background: + radial-gradient(circle at 50% 17%, #e53935 28%, transparent 31%), radial-gradient(circle at 50% 50%, #fbc02d 28%, transparent 31%), + radial-gradient(circle at 50% 83%, #43a047 28%, transparent 31%), #222; } .drive-cell--maneuver .dash-banner__icon { width: calc(42px * var(--s, 1)); height: calc(56px * var(--s, 1)); } .drive-status__brand { font-size: calc(14px * var(--s, 1)); letter-spacing: 0.12em; } .drive-status--ok { color: var(--inst-ok); } diff --git a/public/drive/road.json b/public/drive/road.json index 05266a9..0fffed4 100644 --- a/public/drive/road.json +++ b/public/drive/road.json @@ -13,6 +13,7 @@ { "area": "1 / 11 / 2 / 13", "widget": "field", "value": "time" }, { "area": "1 / 13 / 2 / 15", "widget": "field", "value": "sun" }, { "area": "1 / 15 / 2 / 17", "widget": "status" }, + { "area": "2 / 1 / 3 / 5", "widget": "ahead" }, { "area": "2 / 14 / 3 / 17", "widget": "field", "value": "nextStop", "small": true }, { "area": "3 / 14 / 4 / 17", "widget": "field", "value": "toNextStop" }, { "area": "4 / 14 / 5 / 17", "widget": "field", "value": "etaNextStop" }, diff --git a/public/js/dashboard.js b/public/js/dashboard.js index 64ab4eb..2951d9c 100644 --- a/public/js/dashboard.js +++ b/public/js/dashboard.js @@ -36,7 +36,10 @@ 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 LANES_AHEAD_M = 1000; // the next maneuver's lanes from this close (or LANES_AHEAD_S at the speed driven) +const LANES_AHEAD_S = 45; +const ARRIVE_M = 30; // this close to the end: arrived (navigation goes on: past it, the route turns back) +const ARRIVE_TYPES = [4, 5, 6]; // Valhalla's destination maneuvers const DEFAULT_WIDGETS = { inclinometer: true, speed: true, altitude: true, navigation: true, banner: true }; const THEN_M = 300; // the maneuver after the next one shown with it when it follows this closely // Voice prompts (speechSynthesis, Valhalla's verbal instructions), on/off in @@ -86,7 +89,7 @@ const LANE_ANGLES = { 2: 0, 16: -45, 8: -90, 4: -135, 32: 45, 64: 90, 128: 135, 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 }) { +export function laneSvg({ directions, active = 0 }) { const arrow = (bit) => { const cls = `dash-lane__arrow${active & bit ? " dash-lane__take" : ""}`; return bit === LANE_REVERSE ? uturnPath(cls) : arrowPath(LANE_ANGLES[bit], cls); @@ -236,6 +239,21 @@ export class DashboardControl { notifyRouteProgress(km); } + // With no route: the nearest of /api/road's signals ahead that's still in + // front (within 90° of the heading), straight-line metres. + _signalAhead(position) { + const heading = this._heading()?.deg; + if (heading == null || !this._road?.signals?.length) return null; + const ahead = this._road.signals + .map(([lon, lat]) => { + const dx = (lon - position[0]) * Math.cos(position[1] * RAD), dy = lat - position[1]; + const off = Math.abs((((Math.atan2(dx, dy) / RAD - heading) % 360) + 540) % 360 - 180); + return off < 90 ? meters(position, [lon, lat]) : null; + }) + .filter((m) => m != null); + return ahead.length ? Math.min(...ahead) : null; + } + _speak(index, maneuver, m, speed) { let voiceOn = false; try { voiceOn = localStorage.getItem(VOICE_KEY) === "on"; } catch { /* private window */ } @@ -283,12 +301,14 @@ export class DashboardControl { maneuver: maneuver && { ...maneuver, m: (maneuver.at - at) * 1000 }, then: maneuver && then && then.at - maneuver.at <= THEN_M / 1000 ? then : null, street: onStreet, + arrived: ARRIVE_TYPES.includes(maneuvers.at(-1)?.type) && (!maneuver || (index === maneuvers.length - 1 && (maneuver.at - at) * 1000 <= ARRIVE_M)), + lanesM: Math.max(LANES_AHEAD_M, (speed ?? 0) * LANES_AHEAD_S), }; if (maneuver && off <= ON_ROUTE_M) this._speak(index, maneuver, (maneuver.at - at) * 1000, speed ?? 0); return; } this._progress(null); - this._nav = { limit: this._road?.speed_limit ?? null, offRoute: !!this._route, street: this._road?.name }; + this._nav = { limit: this._road?.speed_limit ?? null, offRoute: !!this._route, street: this._road?.name, signal: this._signalAhead(position) }; 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); @@ -502,7 +522,7 @@ export class DashboardControl { if (n?.maneuver && !this._settings.widgets.banner) { const { text, m, lanes } = n.maneuver; rows.push(`