Dead reckoning in a tunnel: no fix for 3 s (or mode < 2) on the trip's route, the position carried along it (estimated: true, a grey arrow) by the car's CAN speed (vehicle.speed) when there is one, the average speed of the last ~20 s otherwise, up to 10 min (App.gnss_position, TripStore.ahead)

This commit is contained in:
randy committed 2026-12-02 18:24:47 +01:00
1 parent 7fdc0114e6
commit 7204355510
8 files changed
+143 -18

No files matched your search

+53 -5
View File
@@ -390,6 +390,21 @@ module Maptool
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
SPEED_AVG_S = 20.0
VEHICLE_SPEED_FRESH_S = 2 # older: no CAN speed, the average held
# The car's speed off CANopen (signal vehicle.speed, m/s): the meters
# driven since the last fix (dr_m), for dead reckoning in a tunnel.
def self.vehicle_speed_update(speed, at: Time.now.to_f)
GNSS_LATEST_MUTEX.synchronize do
last, last_at = GNSS_LATEST.values_at(:vehicle_speed, :vehicle_speed_at)
if last && GNSS_LATEST[:dr_m] && at - last_at < VEHICLE_SPEED_FRESH_S
GNSS_LATEST[:dr_m] += last * (at - last_at)
end
GNSS_LATEST[:vehicle_speed] = speed.to_f
GNSS_LATEST[:vehicle_speed_at] = at
end
end
# Where every GNSS fix lands (the node over CAN, the simulation): into
# GNSS_LATEST, with the course over ground (track, °) worked out from
@@ -399,7 +414,17 @@ module Maptool
fix = GNSS_LATEST_MUTEX.synchronize do
GNSS_LATEST.merge!(gnss)
next unless gnss.key?(:lat) || gnss.key?(:lon)
# a receiver that lost its fix (a tunnel) and repeats the last one
next if GNSS_LATEST[:mode] && GNSS_LATEST[:mode].to_i < 2
# the average speed of the last ~SPEED_AVG_S (the speed a tunnel was
# entered at), and the car's own odometer since this fix reset
last_at = GNSS_LATEST[:received_at]
if gnss[:speed] && last_at && at > last_at
k = 1 - Math.exp(-(at - last_at) / SPEED_AVG_S)
GNSS_LATEST[:speed_avg] = (GNSS_LATEST[:speed_avg] || gnss[:speed]) * (1 - k) + gnss[:speed] * k
end
GNSS_LATEST[:dr_m] = 0.0
GNSS_LATEST[:received_at] = at
unless gnss.key?(:track)
position = GNSS_LATEST.values_at(:lon, :lat)
@@ -452,6 +477,7 @@ module Maptool
CAN_CONFIG.fetch("interfaces", []).each do |interface|
Thread.new do
Can.listen(interface, decoder: decoder, store: CAN_STORE, start_nodes: CAN_CONFIG["nmt_start"]) do |values|
App.vehicle_speed_update(values["vehicle.speed"]) if values["vehicle.speed"]
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?
@@ -1432,13 +1458,35 @@ module Maptool
json LIVE_STORE.update_settings(params.slice("name", "icon", "color", "logging", "trail_hours"))
end
# No fix for DR_AFTER_S while driving the trip's route (a tunnel): the
# position carried along the route, `estimated: true`, for up to
# DR_MAX_S — by the car's speed off CAN when there is one (dr_m), the
# average speed the tunnel was entered at otherwise. ponytail: along
# the route only; off it the gyro's yaw rate would be needed (TODO).
DR_AFTER_S = 3
DR_MAX_S = 600
def self.gnss_position(now: Time.now.to_f)
latest = GNSS_LATEST_MUTEX.synchronize { GNSS_LATEST.dup }
dr_m, vehicle_at = latest.values_at(:dr_m, :vehicle_speed_at)
fix = latest.except(:course_from, :dr_m, :vehicle_speed_at)
age = fix[:received_at] && now - fix[:received_at]
return { available: false } unless age
speed = fix[:speed_avg] || fix[:speed]
if age >= DR_AFTER_S && age < DR_MAX_S && speed.to_f >= TripStore::MIN_SPEED &&
(coords = TRIP_STORE.get.dig(:route, :geometry, :coordinates))
can = vehicle_at && now - vehicle_at < VEHICLE_SPEED_FRESH_S
meters = can ? dr_m + fix[:vehicle_speed] * (now - vehicle_at) : speed * age
lngLat, track = TripStore.ahead(coords, [fix[:lon], fix[:lat]], meters)
return fix.merge(lon: lngLat[0], lat: lngLat[1], track: track, speed: can ? fix[:vehicle_speed] : speed, available: true, estimated: true) if lngLat
end
age < GNSS_STALE_AFTER ? fix.merge(available: true) : { available: false }
end
get "/api/gnss/position" do
require_login!
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.except(:course_from).merge(available: true) : { available: false })
end
json App.gnss_position
end
end
end