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
CI / check (push) Successful in 23s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-30 18:45:32 +01:00
1 parent f65eb98d18
commit 8680cdbee7
4 files changed
+62 -6

No files matched your search

+28 -6
View File
@@ -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.<field> (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
+8
View File
@@ -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,
+9
View File
@@ -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
+17
View File
@@ -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")