CANopen diagnostics and map data over HTTPS: /can.html (interfaces, nodes with heartbeat state and last EMCY, decoded signals, every COB-ID with role, count, rate and last data); signals can mark an invalid raw value (esphome-canopen's unknown) and NaN gives no value; the CAN reader never dies and a broken can.json only turns CAN off; the GNSS node mcu-stm32-xiao-c5-2 (0x21) and host networking for the car's server in docs; bin/pull-maps.sh pulls map data from GET /api/sync/maps(/file) with SYNC_TOKEN instead of rsync over SSH, resuming cut-off downloads
This commit is contained in:
1 parent
a3e5fefaf1
commit
6a0d6194fe
9 files changed
+365
-43
No files matched your search
+32
-7
@@ -19,7 +19,9 @@ module Maptool
|
||||
# "type": "u16", "scale": 0.125, "unit": "rpm" } ] }
|
||||
# `id` matches the frame's id under `mask` (default: all bits); an id
|
||||
# above 0x7FF is an extended (29-bit) one. Types are little-endian like
|
||||
# CANopen: u8 i8 u16 i16 u32 i32 f32, or bit. value = raw * scale + offset.
|
||||
# CANopen: u8 i8 u16 i16 u32 i32 f32, or bit. value = raw * scale + offset;
|
||||
# a raw `invalid` (e.g. 65535, esphome-canopen's "unknown") or a NaN float
|
||||
# gives no value.
|
||||
module Can
|
||||
EFF_FLAG = 0x80000000 # extended frame
|
||||
RTR_FLAG = 0x40000000
|
||||
@@ -86,7 +88,7 @@ module Maptool
|
||||
raise ArgumentError, "#{s['name']}: unknown type #{type}" unless type == "bit" || TYPES.key?(type)
|
||||
|
||||
{ name: s.fetch("name"), id: id, mask: Integer(s.fetch("mask", "0x1FFFFFFF")), extended: id > 0x7FF,
|
||||
byte: s.fetch("byte", 0), bit: s["bit"], type: type, scale: s.fetch("scale", 1), offset: s.fetch("offset", 0) }
|
||||
byte: s.fetch("byte", 0), bit: s["bit"], type: type, scale: s.fetch("scale", 1), offset: s.fetch("offset", 0), invalid: s["invalid"] }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -102,18 +104,25 @@ module Maptool
|
||||
chunk = frame.data.byteslice(s[:byte], size)
|
||||
chunk.unpack1(format) if chunk&.bytesize == size
|
||||
end
|
||||
values[s[:name]] = s[:type] == "bit" ? raw : raw * s[:scale] + s[:offset] unless raw.nil?
|
||||
next if raw.nil? || raw == s[:invalid] || (raw.is_a?(Float) && raw.nan?)
|
||||
|
||||
values[s[:name]] = s[:type] == "bit" ? raw : raw * s[:scale] + s[:offset]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# The latest of everything heard, for GET /api/can.
|
||||
# The latest of everything heard, for GET /api/can (and the /can.html
|
||||
# diagnostics): interfaces, CANopen nodes (heartbeat state, last EMCY),
|
||||
# every frame id seen (count, rate, last data) and the decoded signals.
|
||||
class Store
|
||||
MAX_FRAME_IDS = 2048 # ponytail: stops counting new ids past this, enough for a bus
|
||||
|
||||
def initialize
|
||||
@mutex = Mutex.new
|
||||
@signals = {}
|
||||
@nodes = {}
|
||||
@interfaces = {}
|
||||
@frames = {}
|
||||
end
|
||||
|
||||
def interface(name, **state)
|
||||
@@ -130,18 +139,34 @@ module Maptool
|
||||
n = (@nodes[node] ||= {})
|
||||
n[:seen_at] = at
|
||||
n[:state] = HEARTBEAT_STATES.fetch(frame.data.getbyte(0) & 0x7F, "unknown") if role == :heartbeat && frame.data.bytesize >= 1
|
||||
# EMCY: error code (0 = errors reset), error register, 5 bytes vendor-specific
|
||||
n[:emcy] = { code: frame.data.unpack1("S<"), register: frame.data.getbyte(2), at: at } if role == :emcy && frame.data.bytesize >= 3
|
||||
end
|
||||
key = "#{interface}:#{frame.extended ? format('%08X', frame.id) : format('%03X', frame.id)}"
|
||||
f = @frames[key]
|
||||
if f.nil? && @frames.size < MAX_FRAME_IDS
|
||||
f = @frames[key] = { interface: interface, id: frame.id, extended: frame.extended, role: role, node: node, count: 0 }
|
||||
end
|
||||
if f
|
||||
if f[:at] # smoothed time between frames, for the rate
|
||||
gap = at - f[:at]
|
||||
f[:interval] = f[:interval] ? f[:interval] * 0.8 + gap * 0.2 : gap
|
||||
end
|
||||
f.merge!(count: f[:count] + 1, data: frame.data.unpack1("H*"), at: at)
|
||||
end
|
||||
values.each { |name, value| @signals[name] = { value: value, at: at } }
|
||||
end
|
||||
end
|
||||
|
||||
def snapshot
|
||||
@mutex.synchronize { { interfaces: @interfaces.dup, nodes: @nodes.dup, signals: @signals.dup } }
|
||||
@mutex.synchronize { { interfaces: @interfaces.dup, nodes: @nodes.dup, signals: @signals.dup, frames: @frames.values.map(&:dup) } }
|
||||
end
|
||||
end
|
||||
|
||||
# Reads `interface` forever, reconnecting (warned once per outage, not
|
||||
# every retry): an adapter unplugged or not up yet isn't fatal.
|
||||
# every retry): an adapter unplugged, not up yet or the bus gone down
|
||||
# isn't fatal, nor is anything else going wrong in here — the thread
|
||||
# never dies, it waits and starts over.
|
||||
def self.listen(interface, decoder:, store:, start_nodes: false, retry_delay: 5)
|
||||
down = false
|
||||
loop do
|
||||
@@ -159,7 +184,7 @@ module Maptool
|
||||
store.record(interface, frame, values)
|
||||
yield(values) if block_given? && !values.empty?
|
||||
end
|
||||
rescue SocketError, SystemCallError, IOError => e
|
||||
rescue StandardError => e
|
||||
store.interface(interface, up: false, error: e.message)
|
||||
warn "can: #{interface}: #{e.message} — retrying every #{retry_delay}s" unless down
|
||||
down = true
|
||||
|
||||
Reference in new issue
Block a user