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
This commit is contained in:
1 parent
ce290f2162
commit
e92f01db12
12 files changed
+713
-76
No files matched your search
@@ -0,0 +1,105 @@
|
||||
require_relative "can"
|
||||
|
||||
module Maptool
|
||||
module Can
|
||||
# A minimal SDO client (CiA 301): expedited and segmented uploads,
|
||||
# expedited downloads — enough to read a node's description and write a
|
||||
# command. Over a transport (#send a Frame, #receive(timeout) → Frame or
|
||||
# nil): SocketTransport on its own raw socket, next to the reader thread's.
|
||||
class Sdo
|
||||
TIMEOUT_S = 1.0
|
||||
|
||||
class Error < StandardError; end
|
||||
class Timeout < Error; end
|
||||
|
||||
class Abort < Error
|
||||
attr_reader :code
|
||||
|
||||
def initialize(code)
|
||||
@code = code
|
||||
super(format("SDO abort 0x%08X", code))
|
||||
end
|
||||
end
|
||||
|
||||
class SocketTransport
|
||||
def initialize(interface)
|
||||
@socket = Can.open(interface)
|
||||
end
|
||||
|
||||
def send(frame)
|
||||
@socket.write(frame.pack)
|
||||
end
|
||||
|
||||
def receive(timeout)
|
||||
return nil unless IO.select([@socket], nil, nil, timeout)
|
||||
|
||||
Frame.unpack(@socket.recv(16))
|
||||
end
|
||||
|
||||
def close
|
||||
@socket.close
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(transport, timeout: TIMEOUT_S)
|
||||
@transport = transport
|
||||
@timeout = timeout
|
||||
end
|
||||
|
||||
# The object's bytes (strings come back as bytes: force_encoding them).
|
||||
def upload(node, index, sub)
|
||||
response = request(node, [0x40, index, sub, 0].pack("CS<CL<"), index: index, sub: sub)
|
||||
cs = response.getbyte(0)
|
||||
raise Error, format("unexpected SDO reply 0x%02X", cs) unless cs & 0xE0 == 0x40
|
||||
|
||||
if cs & 0x02 != 0 # expedited: the data's in this frame
|
||||
size = cs & 0x01 != 0 ? 4 - ((cs >> 2) & 3) : 4
|
||||
return response.byteslice(4, size)
|
||||
end
|
||||
|
||||
data = "".b
|
||||
toggle = 0
|
||||
loop do
|
||||
segment = request(node, [0x60 | (toggle << 4)].pack("C") + ("\0" * 7))
|
||||
scs = segment.getbyte(0)
|
||||
raise Error, "SDO segment out of order" unless scs & 0xE0 == 0x00 && (scs >> 4) & 1 == toggle
|
||||
|
||||
data << segment.byteslice(1, 7 - ((scs >> 1) & 7))
|
||||
return data if scs & 0x01 != 0
|
||||
|
||||
toggle ^= 1
|
||||
end
|
||||
end
|
||||
|
||||
# An expedited download of up to 4 bytes.
|
||||
def download(node, index, sub, bytes)
|
||||
raise ArgumentError, "expedited: 1 to 4 bytes" unless (1..4).cover?(bytes.bytesize)
|
||||
|
||||
cs = 0x23 | ((4 - bytes.bytesize) << 2)
|
||||
response = request(node, [cs, index, sub].pack("CS<C") + bytes.b.ljust(4, "\0"), index: index, sub: sub)
|
||||
raise Error, format("unexpected SDO reply 0x%02X", response.getbyte(0)) unless response.getbyte(0) == 0x60
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Sends to 0x600+node, waits for 0x580+node. index/sub: an initiate's
|
||||
# answer must be for that object — another SDO client talking to the
|
||||
# same node (can2mqtt) gets its own answers too.
|
||||
def request(node, data, index: nil, sub: nil)
|
||||
@transport.send(Frame.new(0x600 + node, data, false))
|
||||
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout
|
||||
loop do
|
||||
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
raise Timeout, format("no SDO answer from node 0x%02X", node) if remaining <= 0
|
||||
|
||||
frame = @transport.receive(remaining)
|
||||
next unless frame && !frame.extended && frame.id == 0x580 + node && frame.data.bytesize == 8
|
||||
next if index && frame.data.byteslice(1, 3).unpack("S<C") != [index, sub]
|
||||
raise Abort, frame.data.byteslice(4, 4).unpack1("L<") if frame.data.getbyte(0) == 0x80
|
||||
|
||||
return frame.data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user