From 8680cdbee7883b8a9824856827fa1594b0430127 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Mon, 30 Nov 2026 18:45:32 +0100 Subject: [PATCH] GNSS course worked out server-side (App.gnss_update, for the CAN node and the simulation, which send none): following in 3D turns heading-up behind the position again --- app.rb | 34 ++++++++++++++++++++++++++++------ docs/deployment.adoc | 8 ++++++++ lib/maptool/gnss_sim.rb | 9 +++++++++ test/test_app.rb | 17 +++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/app.rb b/app.rb index 75a6da2..9920415 100644 --- a/app.rb +++ b/app.rb @@ -384,6 +384,31 @@ module Maptool GNSS_LATEST = {} GNSS_LATEST_MUTEX = Mutex.new + COURSE_MIN_M = 5 # fixes closer than this: too noisy for a course + COURSE_MIN_SPEED = 1.5 # m/s: slower, the last course stays + + # Where every GNSS fix lands (the node over CAN, the simulation): into + # GNSS_LATEST, with the course over ground (track, °) worked out from + # the fix COURSE_MIN_M back when the receiver sends none — following + # heading-up from behind (followPosition, gnss.js) turns by it. + def self.gnss_update(gnss, at: Time.now.to_f) + GNSS_LATEST_MUTEX.synchronize do + GNSS_LATEST.merge!(gnss) + next unless gnss.key?(:lat) || gnss.key?(:lon) + + GNSS_LATEST[:received_at] = at + next if gnss.key?(:track) + + position = GNSS_LATEST.values_at(:lon, :lat) + from = GNSS_LATEST[:course_from] + if from.nil? || (GNSS_LATEST[:speed] || 0) < COURSE_MIN_SPEED + GNSS_LATEST[:course_from] = position + elsif GnssSim.meters(from, position) >= COURSE_MIN_M + GNSS_LATEST[:track] = GnssSim.bearing(from, position).round(1) + GNSS_LATEST[:course_from] = position + end + end + end # Native CAN (lib/maptool/can.rb), on when data/can.json exists: one # reader thread per interface. Signals named gnss. (lat, lon, @@ -410,10 +435,7 @@ module Maptool gnss = values.filter_map { |name, value| [name.delete_prefix("gnss.").to_sym, value] if name.start_with?("gnss.") }.to_h next if gnss.empty? || GNSS_SIM.running? - GNSS_LATEST_MUTEX.synchronize do - GNSS_LATEST.merge!(gnss) - GNSS_LATEST[:received_at] = Time.now.to_f if gnss.key?(:lat) || gnss.key?(:lon) - end + App.gnss_update(gnss) end end end @@ -1251,7 +1273,7 @@ module Maptool CAN_STORE.override_prefix = "gnss." GNSS_SIM.start(lon, lat) do |fix| CAN_STORE.put(fix.transform_keys { |key| "gnss.#{key}" }) - GNSS_LATEST_MUTEX.synchronize { GNSS_LATEST.merge!(fix, received_at: Time.now.to_f) } + App.gnss_update(fix) end json running: true rescue JSON::ParserError @@ -1281,7 +1303,7 @@ module Maptool GNSS_LATEST_MUTEX.synchronize do received_at = GNSS_LATEST[:received_at] fresh = received_at && (Time.now.to_f - received_at) < GNSS_STALE_AFTER - json(fresh ? GNSS_LATEST.merge(available: true) : { available: false }) + json(fresh ? GNSS_LATEST.except(:course_from).merge(available: true) : { available: false }) end end end diff --git a/docs/deployment.adoc b/docs/deployment.adoc index 56e485e..d28db92 100644 --- a/docs/deployment.adoc +++ b/docs/deployment.adoc @@ -258,6 +258,14 @@ adds 360 to the negatives — a filter on `heading:`). No tilt compensation or hard-iron calibration yet, so only roughly right standing level. The node goes operational by itself, so no `nmt_start`. +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 +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. +The simulation (Dashboard settings, `POST`/`DELETE /api/sim/gnss`) drives +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. + === Map data (one way) `bin/pull-maps.sh`, run by `maptool-pull-maps.timer` (10 min after boot, diff --git a/lib/maptool/gnss_sim.rb b/lib/maptool/gnss_sim.rb index 7021a92..e2bd650 100644 --- a/lib/maptool/gnss_sim.rb +++ b/lib/maptool/gnss_sim.rb @@ -93,6 +93,15 @@ module Maptool raise Routing::RouteError, "no route from #{position.map { |v| v.round(5) }.join(', ')}: #{last&.message}" end + # Initial bearing from one [lon, lat] to another, 0..360° from north. + def self.bearing((lon1, lat1), (lon2, lat2)) + rad = Math::PI / 180 + d = (lon2 - lon1) * rad + y = Math.sin(d) * Math.cos(lat2 * rad) + x = Math.cos(lat1 * rad) * Math.sin(lat2 * rad) - Math.sin(lat1 * rad) * Math.cos(lat2 * rad) * Math.cos(d) + (Math.atan2(y, x) / rad) % 360 + end + def self.meters((lon1, lat1), (lon2, lat2)) Math.hypot((lon2 - lon1) * Math.cos((lat1 + lat2) / 2 * Math::PI / 180), lat2 - lat1) * 111_320 end diff --git a/test/test_app.rb b/test/test_app.rb index d5f5b6e..f929ed2 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -662,6 +662,23 @@ class AppTest < Minitest::Test assert_equal 404, response.status end + def test_a_gnss_fix_without_a_course_gets_one_from_the_fixes_before + latest = Maptool::App::GNSS_LATEST + latest.clear + step = 10.0 / 111_320 # 10 m north each fix + Maptool::App.gnss_update({ lat: 48.0, lon: 17.0, speed: 10.0 }) + assert_nil latest[:track] + Maptool::App.gnss_update({ lat: 48.0 + step, lon: 17.0 }) + assert_in_delta 0.0, latest[:track], 0.1 + Maptool::App.gnss_update({ lat: 48.0 + step, lon: 17.0 + step / Math.cos(48 * Math::PI / 180) }) + assert_in_delta 90.0, latest[:track], 0.5 + Maptool::App.gnss_update({ speed: 0.0 }) + Maptool::App.gnss_update({ lat: 48.0, lon: 17.0 }) # standing still: the course stays + assert_in_delta 90.0, latest[:track], 0.5 + ensure + latest.clear + end + private def upload(filename, content, folder: "", content_type: "application/octet-stream")