GNSS simulation on its own diagnostics page (/sim.html: drive the trip's route or randomly, stop, simulated time, the position and the trip); along the route with a detour now and then (300–800 m off every 3–8 km, routed by Valhalla); simulated time 1–20× with the fixes carrying the simulation's clock, so a recorded trip keeps real speeds (PUT /api/sim/gnss); /api/can says simulated
This commit is contained in:
1 parent
7d72e9eb63
commit
0c809b3e2b
9 files changed
+265
-46
No files matched your search
+88
-4
@@ -15,6 +15,16 @@ module Maptool
|
||||
LIMIT_SHARE = 0.9 # drives a bit under the limit
|
||||
|
||||
attr_reader :error
|
||||
# Time runs this many times faster (1, 2, 5, 10…): the car covers that
|
||||
# much more ground per real tick, while each fix's `time` is the
|
||||
# simulation's own clock — a recorded trip shows the real speeds, not
|
||||
# 250 km/h. Changeable while it runs.
|
||||
SPEEDUPS = [1, 2, 5, 10, 20].freeze
|
||||
attr_reader :speedup
|
||||
|
||||
def speedup=(value)
|
||||
@speedup = SPEEDUPS.include?(value.to_i) ? value.to_i : 1
|
||||
end
|
||||
|
||||
def initialize(valhalla_url, vehicle: { id: "car" }, random: Random.new, sleep: ->(s) { Kernel.sleep(s) },
|
||||
router: Routing.method(:route))
|
||||
@@ -25,6 +35,8 @@ module Maptool
|
||||
@sleep = sleep
|
||||
@thread = nil
|
||||
@mutex = Mutex.new
|
||||
@speedup = 1
|
||||
@clock = Time.now.to_f
|
||||
end
|
||||
|
||||
def running?
|
||||
@@ -41,13 +53,14 @@ module Maptool
|
||||
# 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:, time: the simulation's epoch s }.
|
||||
def start(lon = nil, lat = nil, bboxes: nil, route: nil, &on_fix)
|
||||
stop
|
||||
@error = nil
|
||||
@clock = Time.now.to_f
|
||||
@mutex.synchronize do
|
||||
@thread = Thread.new do
|
||||
position, route = if route then [route[:geometry][:coordinates].first, route]
|
||||
position, route = if route then [route[:geometry][:coordinates].first, with_detours(route)]
|
||||
elsif bboxes then random_start(bboxes)
|
||||
else [[Float(lon), Float(lat)], nil]
|
||||
end
|
||||
@@ -94,11 +107,12 @@ module Maptool
|
||||
i = 1
|
||||
while i < coords.size
|
||||
kmh = speed_kmh(limits.reverse.find { |start, _| start <= at }&.last)
|
||||
at += kmh / 3600 * TICK_S
|
||||
at += kmh / 3600 * TICK_S * @speedup
|
||||
@clock += TICK_S * @speedup
|
||||
i += 1 while i < coords.size - 1 && km[i] < at
|
||||
t = km[i] > km[i - 1] ? ((at - km[i - 1]) / (km[i] - km[i - 1])).clamp(0.0, 1.0) : 1.0
|
||||
lon, lat = coords[i - 1].zip(coords[i]).map { |a, b| a + (b - a) * t }
|
||||
on_fix&.call(lat: lat, lon: lon, speed: kmh / 3.6, satellites: 12, hdop: 0.8)
|
||||
on_fix&.call(lat: lat, lon: lon, speed: kmh / 3.6, satellites: 12, hdop: 0.8, time: @clock.round(1))
|
||||
break if i == coords.size - 1 && t >= 1
|
||||
|
||||
@sleep.call(TICK_S)
|
||||
@@ -110,6 +124,76 @@ module Maptool
|
||||
(limit == "unlimited" ? UNLIMITED_KMH : limit || NO_LIMIT_KMH) * LIMIT_SHARE
|
||||
end
|
||||
|
||||
DETOUR_EVERY_KM = (3.0..8.0) # along the route between two detours
|
||||
DETOUR_ALONG_KM = (0.6..1.5) # the stretch of route a detour replaces
|
||||
DETOUR_OFF_M = (300.0..800.0) # how far off it goes, either side
|
||||
|
||||
# The route with a detour now and then (to try off-route and the
|
||||
# rerouting): a stretch of it replaced by a way (our Valhalla) through a
|
||||
# point DETOUR_OFF_M to one side; where there's no way, the route as it
|
||||
# is. Speed limits carried along, the detours' own.
|
||||
def with_detours(route)
|
||||
coords = route[:geometry][:coordinates]
|
||||
km = [0.0]
|
||||
coords.each_cons(2) { |a, b| km << km.last + self.class.meters(a, b) / 1000 }
|
||||
limits = route[:properties][:speed_limits] || [[0.0, nil]]
|
||||
out = []
|
||||
out_limits = []
|
||||
add = lambda do |line, line_limits, from_km|
|
||||
offset = out.empty? ? 0.0 : self.class.length_km(out)
|
||||
line_limits.each { |start, limit| out_limits << [offset + start - from_km, limit] }
|
||||
out.concat(out.empty? ? line : line.drop(1))
|
||||
end
|
||||
original = ->(from, to) { add.(self.class.slice(coords, km, from, to), limits_in(limits, from, to), from) }
|
||||
at = 0.0
|
||||
loop do
|
||||
start = at + @random.rand(DETOUR_EVERY_KM)
|
||||
finish = start + @random.rand(DETOUR_ALONG_KM)
|
||||
break original.(at, km.last) if finish >= km.last
|
||||
|
||||
original.(at, start)
|
||||
a, b = self.class.point_at(coords, km, start), self.class.point_at(coords, km, finish)
|
||||
side = @random.rand < 0.5 ? -1 : 1
|
||||
bearing = (self.class.bearing(a, b) + 90 * side) * Math::PI / 180
|
||||
mid = self.class.point_at(coords, km, (start + finish) / 2)
|
||||
off = @random.rand(DETOUR_OFF_M)
|
||||
via = [mid[0] + off * Math.sin(bearing) / (111_320 * Math.cos(mid[1] * Math::PI / 180)), mid[1] + off * Math.cos(bearing) / 111_320]
|
||||
begin
|
||||
detour = @router.call(@valhalla_url, [a, via, b], @vehicle)
|
||||
add.(detour[:geometry][:coordinates], detour[:properties][:speed_limits] || [[0.0, nil]], 0.0)
|
||||
rescue Routing::RouteError
|
||||
original.(start, finish)
|
||||
end
|
||||
at = finish
|
||||
end
|
||||
{ geometry: { type: "LineString", coordinates: out }, properties: { speed_limits: out_limits.uniq } }
|
||||
end
|
||||
|
||||
# The limits that apply from `from` to `to` km, from `from` on.
|
||||
def limits_in(limits, from, to)
|
||||
first = limits.reverse.find { |start, _| start <= from } || [from, nil]
|
||||
[[from, first[1]]] + limits.select { |start, _| start > from && start < to }
|
||||
end
|
||||
|
||||
# [lon, lat] at km along the line (km: the cumulative km at each vertex).
|
||||
def self.point_at(coords, km, at)
|
||||
i = km.index { |k| k >= at } || (km.size - 1)
|
||||
return coords[i] if i.zero? || km[i] == km[i - 1]
|
||||
|
||||
t = (at - km[i - 1]) / (km[i] - km[i - 1])
|
||||
coords[i - 1].zip(coords[i]).map { |p, q| p + (q - p) * t }
|
||||
end
|
||||
|
||||
# The line from `from` to `to` km, its ends cut at those points.
|
||||
def self.slice(coords, km, from, to)
|
||||
inner = coords.each_index.select { |i| km[i] > from && km[i] < to }.map { |i| coords[i] }
|
||||
[point_at(coords, km, from), *inner, point_at(coords, km, to)]
|
||||
end
|
||||
|
||||
def self.length_km(line)
|
||||
line.each_cons(2).sum { |a, b| meters(a, b) } / 1000
|
||||
end
|
||||
|
||||
# A route to a random point DEST_KM away; a few tries (a lake, no road there).
|
||||
def random_route(position)
|
||||
last = nil
|
||||
|
||||
Reference in new issue
Block a user