140 lines
5.4 KiB
Ruby
140 lines
5.4 KiB
Ruby
require_relative "routing"
|
||
|
||
module Maptool
|
||
# Simulated driving, to try the dashboard and navigation without the car:
|
||
# from a start point it routes (our Valhalla) to a random point 2–8 km
|
||
# away and drives there at the road's speed limit, a fix every TICK_S, then
|
||
# on to the next random point. Its fixes go where the GNSS node's do
|
||
# (app.rb: the CAN store's gnss.* signals and GNSS_LATEST), the node's own
|
||
# are ignored meanwhile.
|
||
class GnssSim
|
||
TICK_S = 0.5 # finer than the clients poll (1.5 s), so each poll sees a fresh fix
|
||
DEST_KM = (2.0..8.0)
|
||
NO_LIMIT_KMH = 50 # an untagged road
|
||
UNLIMITED_KMH = 130
|
||
LIMIT_SHARE = 0.9 # drives a bit under the limit
|
||
|
||
attr_reader :error
|
||
|
||
def initialize(valhalla_url, vehicle: { id: "car" }, random: Random.new, sleep: ->(s) { Kernel.sleep(s) },
|
||
router: Routing.method(:route))
|
||
@valhalla_url = valhalla_url
|
||
@router = router
|
||
@vehicle = vehicle
|
||
@random = random
|
||
@sleep = sleep
|
||
@thread = nil
|
||
@mutex = Mutex.new
|
||
end
|
||
|
||
def running?
|
||
@mutex.synchronize { @thread&.alive? || false }
|
||
end
|
||
|
||
RANDOM_START_TRIES = 20 # a random point at sea / off the routing tiles: another one
|
||
# Shorter: a road island the start snapped to (a lake's middle → a 0 km
|
||
# route), it'd drive the same few meters again and again — another try.
|
||
MIN_TRIP_KM = 1.0
|
||
|
||
# Drives from [lon, lat] until stopped — or, with bboxes
|
||
# ([[west, south, east, north], ...], the offline countries) instead,
|
||
# from a random place in one of them; each fix to the block:
|
||
# { lat:, lon:, speed: m/s, satellites:, hdop: }.
|
||
def start(lon = nil, lat = nil, bboxes: nil, &on_fix)
|
||
stop
|
||
@error = nil
|
||
@mutex.synchronize do
|
||
@thread = Thread.new do
|
||
position, route = bboxes ? random_start(bboxes) : [[Float(lon), Float(lat)], nil]
|
||
loop do
|
||
position = drive(position, route, &on_fix)
|
||
route = nil
|
||
rescue Routing::RouteError
|
||
raise unless bboxes
|
||
|
||
position, route = random_start(bboxes) # no way on from here: somewhere else
|
||
end
|
||
rescue StandardError => e
|
||
@error = e.message
|
||
warn "gnss sim: #{e.message}"
|
||
end
|
||
end
|
||
end
|
||
|
||
def stop
|
||
thread = @mutex.synchronize { @thread.tap { @thread = nil } }
|
||
thread&.kill&.join
|
||
end
|
||
|
||
# A random place in one of the bboxes with a route out of it: [position, route].
|
||
def random_start(bboxes)
|
||
RANDOM_START_TRIES.times do
|
||
west, south, east, north = bboxes.sample(random: @random)
|
||
position = [west + @random.rand * (east - west), south + @random.rand * (north - south)]
|
||
return [position, random_route(position)]
|
||
rescue Routing::RouteError
|
||
next
|
||
end
|
||
raise Routing::RouteError, "no road found at #{RANDOM_START_TRIES} random places in the offline countries"
|
||
end
|
||
|
||
# One random trip from position (route: already planned); returns where it ended.
|
||
def drive(position, route = nil, &on_fix)
|
||
route ||= random_route(position)
|
||
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]]
|
||
at = 0.0
|
||
i = 1
|
||
while i < coords.size
|
||
kmh = speed_kmh(limits.reverse.find { |start, _| start <= at }&.last)
|
||
at += kmh / 3600 * TICK_S
|
||
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)
|
||
break if i == coords.size - 1 && t >= 1
|
||
|
||
@sleep.call(TICK_S)
|
||
end
|
||
coords.last
|
||
end
|
||
|
||
def speed_kmh(limit)
|
||
(limit == "unlimited" ? UNLIMITED_KMH : limit || NO_LIMIT_KMH) * LIMIT_SHARE
|
||
end
|
||
|
||
# A route to a random point DEST_KM away; a few tries (a lake, no road there).
|
||
def random_route(position)
|
||
last = nil
|
||
5.times do
|
||
bearing = @random.rand * 2 * Math::PI
|
||
distance = @random.rand(DEST_KM) * 1000
|
||
lon, lat = position
|
||
dest = [lon + distance * Math.sin(bearing) / (111_320 * Math.cos(lat * Math::PI / 180)), lat + distance * Math.cos(bearing) / 111_320]
|
||
route = @router.call(@valhalla_url, [position, dest], @vehicle)
|
||
return route if route[:geometry][:coordinates].size >= 2 && route[:properties][:distance_km].to_f >= MIN_TRIP_KM
|
||
|
||
last = Routing::RouteError.new("only #{route[:properties][:distance_km]} km")
|
||
rescue Routing::RouteError => e
|
||
last = e
|
||
end
|
||
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
|
||
end
|
||
end
|