Simulation along the trip's route ("Simulate driving the route" when there is one, POST /api/sim/gnss {"trip": true}), random trips after it
CI / check (push) Successful in 21s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-12-01 01:45:38 +01:00
1 parent 07a4d363ec
commit 7fcac61c99
5 files changed
+38 -9

No files matched your search

+8 -3
View File
@@ -1341,8 +1341,13 @@ module Maptool
post "/api/sim/gnss" do post "/api/sim/gnss" do
require_login! require_login!
lon, lat = JSON.parse(request.body.read)["start"] body = JSON.parse(request.body.read)
if lon.nil? lon, lat = body["start"]
# {"trip": true}: along the shared trip's route (to try the navigation)
route = TRIP_STORE.get[:route] if body["trip"]
halt 422, json(error: "the trip has no route") if body["trip"] && !route
if route then nil
elsif lon.nil?
codes = (PmtilesBuilder.offline_meta(DATA_DIR) || {}).fetch("sources", []).map { |source| source["code"] } codes = (PmtilesBuilder.offline_meta(DATA_DIR) || {}).fetch("sources", []).map { |source| source["code"] }
bboxes = codes.filter_map { |code| PmtilesBuilder.country_bbox(code) } bboxes = codes.filter_map { |code| PmtilesBuilder.country_bbox(code) }
halt 422, json(error: "no offline countries to start in") if bboxes.empty? halt 422, json(error: "no offline countries to start in") if bboxes.empty?
@@ -1351,7 +1356,7 @@ module Maptool
end end
CAN_STORE.override_prefix = "gnss." CAN_STORE.override_prefix = "gnss."
GNSS_SIM.start(lon, lat, bboxes: bboxes) do |fix| GNSS_SIM.start(lon, lat, bboxes: bboxes, route: route) do |fix|
CAN_STORE.put(fix.transform_keys { |key| "gnss.#{key}" }) CAN_STORE.put(fix.transform_keys { |key| "gnss.#{key}" })
App.gnss_update(fix) App.gnss_update(fix)
end end
+1 -1
View File
@@ -326,7 +326,7 @@ The node sends no course: `App.gnss_update` (where every fix lands, the
node's and the GNSS simulation's) works it out from the fix ≥ 5 m back node's and the GNSS simulation's) works it out from the fix ≥ 5 m back
while moving ≥ 1.5 m/s and keeps the last one standing still, so while moving ≥ 1.5 m/s and keeps the last one standing still, so
`/api/gnss/position` has `track` and following in 3D turns heading-up. `/api/gnss/position` has `track` and following in 3D turns heading-up.
The simulation (Dashboard settings, `POST`/`DELETE /api/sim/gnss`) drives The simulation (Dashboard settings, `POST`/`DELETE /api/sim/gnss`; `{"trip": true}` drives the shared trip's route first, the dashboard's button does when there is one) drives
random trips over the local Valhalla and stands in for the node's `gnss.*` random trips over the local Valhalla and stands in for the node's `gnss.*`
meanwhile — on the car's server only for trying things out. While it meanwhile — on the car's server only for trying things out. While it
runs, the dashboard takes the altitude from the terrain (no `gnss.alt` runs, the dashboard takes the altitude from the terrain (no `gnss.alt`
+8 -3
View File
@@ -38,14 +38,19 @@ module Maptool
# Drives from [lon, lat] until stopped — or, with bboxes # Drives from [lon, lat] until stopped — or, with bboxes
# ([[west, south, east, north], ...], the offline countries) instead, # ([[west, south, east, north], ...], the offline countries) instead,
# from a random place in one of them; each fix to the block: # from a random place in one of them; with a route (the trip's /api/route
# Feature), that route from its start first, random trips after it;
# each fix to the block:
# { lat:, lon:, speed: m/s, satellites:, hdop: }. # { lat:, lon:, speed: m/s, satellites:, hdop: }.
def start(lon = nil, lat = nil, bboxes: nil, &on_fix) def start(lon = nil, lat = nil, bboxes: nil, route: nil, &on_fix)
stop stop
@error = nil @error = nil
@mutex.synchronize do @mutex.synchronize do
@thread = Thread.new do @thread = Thread.new do
position, route = bboxes ? random_start(bboxes) : [[Float(lon), Float(lat)], nil] position, route = if route then [route[:geometry][:coordinates].first, route]
elsif bboxes then random_start(bboxes)
else [[Float(lon), Float(lat)], nil]
end
loop do loop do
position = drive(position, route, &on_fix) position = drive(position, route, &on_fix)
route = nil route = nil
+8 -2
View File
@@ -176,6 +176,7 @@ export class DashboardControl {
this._lastAt = 0; this._lastAt = 0;
this._offFixes = 0; this._offFixes = 0;
this._sentKm = undefined; // the new route drawn by the next fix this._sentKm = undefined; // the new route drawn by the next fix
this._simLabel();
})); }));
this._trace = []; // [lon, lat] fixes TRACE_STEP_M apart, for /api/road this._trace = []; // [lon, lat] fixes TRACE_STEP_M apart, for /api/road
this._road = null; // its last answer this._road = null; // its last answer
@@ -340,7 +341,8 @@ export class DashboardControl {
// driving from a random place in the offline countries, the GNSS control // driving from a random place in the offline countries, the GNSS control
// turned on to follow it; on = true / false, null = just read the state. // turned on to follow it; on = true / false, null = just read the state.
async _simulate(on) { async _simulate(on) {
const options = on === true ? { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" } // along the route when there is one (the shared trip's), else from a random place
const options = on === true ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(this._route ? { trip: true } : {}) }
: on === false ? { method: "DELETE" } : {}; : on === false ? { method: "DELETE" } : {};
try { try {
const response = await fetch("/api/sim/gnss", options); const response = await fetch("/api/sim/gnss", options);
@@ -354,7 +356,11 @@ export class DashboardControl {
this._simStatus.hidden = false; this._simStatus.hidden = false;
this._simStatus.textContent = `Simulation: ${e.message}`; this._simStatus.textContent = `Simulation: ${e.message}`;
} }
this._simButton.textContent = this._simRunning ? "Stop the simulation" : "Simulate driving from the map center"; this._simLabel();
}
_simLabel() {
if (this._simButton) this._simButton.textContent = this._simRunning ? "Stop the simulation" : this._route ? "Simulate driving the route" : "Simulate driving from a random place";
} }
_save() { _save() {
+13
View File
@@ -22,6 +22,19 @@ class GnssSimTest < Minitest::Test
assert_in_delta (1000 / (36 / 3.6) / 2 + 500 / (45 / 3.6)) / Maptool::GnssSim::TICK_S, fixes.size, 3 # a fix every TICK_S 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
# Started with the trip's route: from its start along it, before any random trip.
def test_starts_along_a_given_route
fixes = Queue.new
sim = Maptool::GnssSim.new("http://valhalla", sleep: ->(_s) {}, router: ->(*) { raise Maptool::Routing::RouteError, "done" })
route = { geometry: { coordinates: [[18.0, 49.0], [18.0, 49.001]] }, properties: { speed_limits: [[0.0, 50]] } }
sim.start(route: route) { |fix| fixes << fix }
first = fixes.pop
assert_in_delta 49.0, first[:lat], 0.0001
assert_in_delta 18.0, first[:lon], 1e-9
ensure
sim&.stop
end
# No road at the first random place: another one, inside the bbox. # No road at the first random place: another one, inside the bbox.
class FirstPlaceAtSea < Maptool::GnssSim class FirstPlaceAtSea < Maptool::GnssSim
attr_reader :tried attr_reader :tried