Nodes describe themselves: an SDO client (Can::Sdo) reads each esphome-canopen node's TPDOs and entities (type, name, unit, range) on its first heartbeat, cached by the TPDO layout hash, decoded with no hand-written signals — can.json keeps aliases (with unit conversion) and foreign devices; design doc (NPS planning, cockpit instruments, Unigrid band) with color tokens, the dashboard in cockpit colors; starting the simulation turns the GNSS follow on, the GNSS position an arrow by its course
CI / check (push) Successful in 23s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-30 19:51:03 +01:00
1 parent ce290f2162
commit e92f01db12
12 files changed
+713 -76

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
require_relative "test_helper"
require "tmpdir"
require "maptool/can_discovery"
class CanDiscoveryTest < Minitest::Test
Frame = Maptool::Can::Frame
# A node's SDO server over its OD ({[index, sub] => bytes}), expedited and
# segmented uploads, expedited downloads; `noise` answers first, as if for
# another SDO client.
class FakeNode
attr_reader :od
def initialize(node, od, noise: nil)
@node = node
@od = od
@queue = []
@noise = noise
end
def send(frame)
return unless frame.id == 0x600 + @node
@queue << Frame.new(0x580 + @node, @noise, false) if @noise
@noise = nil
cs = frame.data.getbyte(0)
index, sub = frame.data.byteslice(1, 3).unpack("S<C")
reply = case cs & 0xE0
when 0x40 then initiate(index, sub)
when 0x60 then segment(cs)
when 0x20 then (@od[[index, sub]] = frame.data.byteslice(4, 4 - ((cs >> 2) & 3)); [0x60, index, sub, 0].pack("CS<CL<"))
end
@queue << Frame.new(0x580 + @node, reply, false)
end
def receive(_timeout)
@queue.shift
end
private
def initiate(index, sub)
bytes = @od[[index, sub]] or return [0x80, index, sub, 0x06020000].pack("CS<CL<")
if bytes.bytesize <= 4
[0x43 | ((4 - bytes.bytesize) << 2), index, sub].pack("CS<C") + bytes.ljust(4, "\0")
else
@pending = bytes.dup
@toggle = 0
[0x41, index, sub, bytes.bytesize].pack("CS<CL<")
end
end
def segment(cs)
chunk = @pending.slice!(0, 7)
last = @pending.empty?
[((cs >> 4) & 1) << 4 | ((7 - chunk.bytesize) << 1) | (last ? 1 : 0)].pack("C") + chunk.ljust(7, "\0")
end
end
u32 = ->(v) { [v].pack("L<") }
f32 = ->(v) { [v].pack("e") }
# The GNSS node: TPDO2 0x3A1 lat/lon (entities 6, 7: sensor, REAL32),
# TPDO3 0x4A1 altitude (entity 8: sensor16, -500..9000 m) + satellites
# (entity 11: sensor8, 0..255).
GNSS_OD = {
[0x3002, 1] => u32.(0xCAFE), [0x1008, 0] => "XIAO STM32C5 2",
[0x1802, 1] => u32.(0x3A1), [0x1A02, 0] => "\x02".b, [0x1A02, 1] => u32.(0x20610120), [0x1A02, 2] => u32.(0x20710120),
[0x1803, 1] => u32.(0x4A1), [0x1A03, 0] => "\x02".b, [0x1A03, 1] => u32.(0x20810110), [0x1A03, 2] => u32.(0x20B10108),
[0x1804, 1] => u32.(0x80000000 | 0x221), # disabled
[0x2001, 6] => u32.(1), [0x2001, 7] => u32.(1), [0x2001, 8] => u32.(7), [0x2001, 11] => u32.(6),
[0x2060, 1] => "Latitude", [0x2070, 1] => "Longitude",
[0x2080, 1] => "Altitude", [0x2080, 3] => "m", [0x2080, 7] => f32.(-500.0), [0x2080, 8] => f32.(9000.0),
[0x20B0, 1] => "Satellites", [0x20B0, 7] => f32.(0.0), [0x20B0, 8] => f32.(255.0),
}.transform_values(&:b).freeze
def test_sdo_reads_expedited_and_segmented_and_skips_another_clients_answer
noise = [0x43, 0x3100, 2, 99].pack("CS<CL<") # can2mqtt's log poll answered
sdo = Maptool::Can::Sdo.new(FakeNode.new(0x21, GNSS_OD.dup, noise: noise))
assert_equal 0xCAFE, sdo.upload(0x21, 0x3002, 1).unpack1("L<")
assert_equal "XIAO STM32C5 2", sdo.upload(0x21, 0x1008, 0) # 14 bytes: 2 segments
error = assert_raises(Maptool::Can::Sdo::Abort) { sdo.upload(0x21, 0x9999, 0) }
assert_equal 0x06020000, error.code
node = FakeNode.new(0x21, {})
Maptool::Can::Sdo.new(node).download(0x21, 0x20E2, 1, "\x01")
assert_equal "\x01", node.od[[0x20E2, 1]]
end
def test_a_node_described_decodes_its_own_tpdos
sdo = Maptool::Can::Sdo.new(FakeNode.new(0x21, GNSS_OD.dup))
description = Maptool::Can::Discovery.describe(sdo, 0x21, aliases: { "XIAO STM32C5 2.Latitude" => "gnss.lat" })
assert_equal [0xCAFE, "XIAO STM32C5 2"], description.values_at(:hash, :name)
names = description[:signals].map { |s| s["name"] }
assert_equal ["gnss.lat", "XIAO STM32C5 2.Longitude", "XIAO STM32C5 2.Altitude", "XIAO STM32C5 2.Satellites"], names
decoder = Maptool::Can::Decoder.new([])
decoder.set_node(0x21, description[:signals])
values = decoder.decode(Frame.new(0x3A1, [48.05, 17.36].pack("ee"), false))
assert_in_delta 48.05, values["gnss.lat"], 1e-5
raw = ((144.5 + 500) / 9500 * 65_534).round # the node's encoding
values = decoder.decode(Frame.new(0x4A1, [raw, 7].pack("S<C"), false))
assert_in_delta 144.5, values["XIAO STM32C5 2.Altitude"], 0.1
assert_in_delta 7.03, values["XIAO STM32C5 2.Satellites"], 0.01 # 255/254 per step, as the node encodes
assert_equal "m", decoder.units["XIAO STM32C5 2.Altitude"]
assert_empty decoder.decode(Frame.new(0x4A1, [65_535, 255].pack("S<C"), false)) # unknown
end
# A store that has heard the node, the description cached, read again only
# when the layout hash changes; a node without 0x3002 left alone.
def test_the_discoverer_caches_by_layout_hash
Dir.mktmpdir do |dir|
store = Maptool::Can::Store.new
store.record("vcan0", Frame.new(0x721, "\x05".b, false), {})
store.record("vcan0", Frame.new(0x705, "\x05".b, false), {}) # a foreign node
od = GNSS_OD.dup
node = FakeNode.new(0x21, od)
foreign = FakeNode.new(0x05, {})
both = Object.new
both.define_singleton_method(:send) { |f| node.send(f) || foreign.send(f) }
both.define_singleton_method(:receive) { |t| node.receive(t) || foreign.receive(t) }
decoder = Maptool::Can::Decoder.new([])
discoverer = Maptool::Can::Discoverer.new(store: store, decoder: decoder, cache_dir: dir, sdo: Maptool::Can::Sdo.new(both))
_, err = capture_io { discoverer.run_once(now: 0) }
assert_match(/node 33 \(XIAO STM32C5 2\): 4 signals/, err)
assert File.exist?(File.join(dir, "21.json"))
# a restart: from the cache, nothing read until it's checked again
decoder = Maptool::Can::Decoder.new([])
Maptool::Can::Discoverer.new(store: store, decoder: decoder, cache_dir: dir,
aliases: { "XIAO STM32C5 2.Altitude" => { "name" => "gnss.alt_ft", "scale" => 3.28084, "unit" => "ft" } })
raw = ((144.5 + 500) / 9500 * 65_534).round
assert_in_delta 474.1, decoder.decode(Frame.new(0x4A1, [raw, 7].pack("S<C"), false))["gnss.alt_ft"], 0.5
assert_equal "ft", decoder.units["gnss.alt_ft"]
end
end
end