Debug pages switch between each other: one tab bar on top of /status.html, /can.html and /sim.html (views/_debug_nav.erb). CAN analyzer: a trace of every frame as it came (the server keeps the last 1000, GET /api/can?since=<seq> gives the new ones), each byte colored by what it is and decoded by its CANopen role — SDO command, index:subindex, expedited payload, segments, abort codes, byte 0 in bits; NMT, heartbeat, EMCY, SYNC, PDOs; who sent it, Δ ms, pause, clear, the frame filter
This commit is contained in:
1 parent
7901d75ff1
commit
7fdc0114e6
7 files changed
+153
-9
No files matched your search
+123
-4
@@ -4,7 +4,7 @@
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<title>CANopen diagnostics</title>
|
||||
<title>CAN analyzer</title>
|
||||
<link rel="icon" href="/icons/icon.svg" type="image/svg+xml">
|
||||
<style>
|
||||
:root { color-scheme: light dark; --muted: GrayText; --line: color-mix(in srgb, CanvasText 15%, transparent); --ok: #2e7d32; --warn: #ef6c00; --bad: #c62828; }
|
||||
@@ -32,12 +32,22 @@
|
||||
/* a byte that just changed, fading out over CHANGED_FOR */
|
||||
/* a fixed width: the column doesn't jump when a node goes "silent" */
|
||||
#nodes th:nth-child(2), #nodes td:nth-child(2) { width: 9em; min-width: 9em; }
|
||||
/* the trace: bytes colored by what they are (SDO: command, index, subindex, payload) */
|
||||
#trace-box { max-height: 70vh; overflow: auto; }
|
||||
#trace td { padding-top: 3px; padding-bottom: 3px; }
|
||||
#trace thead th { position: sticky; top: 0; background: Canvas; }
|
||||
.b-cmd { color: #e53935; } .b-idx { color: #1e88e5; } .b-sub { color: #64b5f6; } .b-pay { color: #00a37a; } .b-pad { color: color-mix(in srgb, CanvasText 30%, transparent); }
|
||||
.bits-cs { color: #a020f0; } .bits-x { color: color-mix(in srgb, CanvasText 30%, transparent); } .bits-n { color: #5a3fc0; font-weight: 600; } .bits-es { color: #9b8fd0; }
|
||||
.legend { color: var(--muted); font-size: 0.8rem; font-weight: 400; text-transform: none; letter-spacing: 0; }
|
||||
.legend span { margin-right: 8px; }
|
||||
button { padding: 6px 10px; font-size: 0.9rem; }
|
||||
.chg { background: color-mix(in srgb, #ffb300 calc(var(--fresh) * 70%), transparent); border-radius: 3px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<%= erb :_debug_nav, layout: false %>
|
||||
<header>
|
||||
<h1>CANopen diagnostics</h1>
|
||||
<h1>CAN analyzer</h1>
|
||||
<span id="status">loading…</span>
|
||||
</header>
|
||||
|
||||
@@ -56,6 +66,11 @@
|
||||
<div class="scroll"><table id="frames"><thead><tr><th>Interface</th><th>COB-ID</th><th>Role</th><th>Node</th><th class="num">Count</th><th class="num">Rate</th><th>Data</th><th>Last</th></tr></thead><tbody></tbody></table></div>
|
||||
</div></div>
|
||||
|
||||
<h2>Trace <button id="pause" type="button">Pause</button> <button id="clear" type="button">Clear</button>
|
||||
<span class="legend"><span class="b-cmd">command</span><span class="b-idx">OD index</span><span class="b-sub">subindex</span><span class="b-pay">payload</span><span class="b-pad">unused</span>
|
||||
· byte 0: <span class="bits-cs">ccs/scs</span> <span class="bits-x">t</span> <span class="bits-n">n</span> <span class="bits-es">e s</span> · the frame filter applies</span></h2>
|
||||
<div id="trace-box" class="scroll"><table id="trace"><thead><tr><th class="num">#</th><th class="num">Time</th><th class="num">Δ ms</th><th>Interface</th><th>ID</th><th>Data</th><th>From</th><th>Byte 0</th><th>Meaning</th></tr></thead><tbody></tbody></table></div>
|
||||
|
||||
<script>
|
||||
// Polls GET /api/can (lib/maptool/can.rb's Store) once a second.
|
||||
const STALE_AFTER = 10; // s without a frame: greyed out
|
||||
@@ -125,15 +140,117 @@
|
||||
filter ? "No frame matches." : "No frame yet.", 8);
|
||||
}
|
||||
|
||||
// The trace: every frame as it came (the server keeps the last 1000),
|
||||
// each decoded by its CANopen role.
|
||||
const TRACE_MAX = 1000;
|
||||
let trace = [], logSince = 0, paused = false, t0 = null;
|
||||
|
||||
function canopenRole(id, ext) { // lib/maptool/can.rb's Can.canopen_role
|
||||
if (ext) return [null, null];
|
||||
const node = id & 0x7f, fn = id >> 7;
|
||||
if (fn === 0) return id ? [null, null] : ["nmt", null];
|
||||
if (fn === 1) return node ? ["emcy", node] : ["sync", null];
|
||||
if (fn === 2) return ["time", null];
|
||||
if (fn >= 3 && fn <= 10) return [`${fn % 2 ? "tpdo" : "rpdo"}${Math.floor((fn - 1) / 2)}`, node];
|
||||
return [{ 0xb: "sdo_tx", 0xc: "sdo_rx", 0xe: "heartbeat" }[fn] || null, node];
|
||||
}
|
||||
const le = (b) => b.reduceRight((v, x) => v * 256 + x, 0);
|
||||
const NMT = { 0x01: "start", 0x02: "stop", 0x80: "enter pre-operational", 0x81: "reset node", 0x82: "reset communication" };
|
||||
const HB = { 0x00: "boot-up", 0x04: "stopped", 0x05: "operational", 0x7f: "pre-operational" };
|
||||
const ABORT = { 0x05040000: "timeout", 0x05040001: "bad command", 0x06010000: "unsupported access", 0x06010001: "write-only", 0x06010002: "read-only",
|
||||
0x06020000: "no such object", 0x06070010: "length mismatch", 0x06090011: "no such subindex", 0x06090030: "value out of range",
|
||||
0x08000000: "general error", 0x08000020: "can't store", 0x08000022: "device state" };
|
||||
const SDO_CLIENT = ["download segment", "download init request", "upload init request", "upload segment request", "abort", "block upload", "block download"];
|
||||
const SDO_SERVER = ["upload segment", "download segment response", "upload init response", "download init response", "abort", "block download", "block upload"];
|
||||
|
||||
// → { cls: a class per byte, from, bits (byte 0, SDO), note }
|
||||
function decode(f) {
|
||||
const b = (f.data.match(/../g) || []).map((x) => parseInt(x, 16));
|
||||
const [role, node] = canopenRole(f.id, f.extended);
|
||||
const cls = b.map(() => "b-pay");
|
||||
const pad = (from) => { for (let i = from; i < b.length; i++) cls[i] = "b-pad"; };
|
||||
const value = (from, to) => { const v = le(b.slice(from, to)); return `${hex(v, 2 * (to - from))} = ${v}`; };
|
||||
let from = node != null ? `node ${node}` : "", bits = "", note = role || (f.extended ? "extended" : "");
|
||||
if (role === "nmt") {
|
||||
from = "master"; cls[0] = "b-cmd";
|
||||
note = `NMT ${NMT[b[0]] || hex(b[0] ?? 0, 2)} → ${b[1] ? `node ${b[1]}` : "all nodes"}`;
|
||||
} else if (role === "heartbeat") {
|
||||
note = `heartbeat: ${HB[b[0] & 0x7f] || "unknown"}`;
|
||||
} else if (role === "emcy" && b.length >= 3) {
|
||||
cls[0] = cls[1] = "b-cmd"; cls[2] = "b-idx";
|
||||
const code = b[0] | (b[1] << 8);
|
||||
note = code ? `EMCY ${hex(code, 4)}, error register ${hex(b[2], 2)}` : "EMCY: errors reset";
|
||||
} else if (role === "sync") {
|
||||
from = "master"; note = b.length ? `SYNC, counter ${b[0]}` : "SYNC";
|
||||
} else if (role?.startsWith("rpdo")) {
|
||||
from = `master → ${node}`; note = role.toUpperCase();
|
||||
} else if (role?.startsWith("tpdo")) {
|
||||
note = role.toUpperCase();
|
||||
} else if (role === "sdo_rx" || role === "sdo_tx") {
|
||||
const client = role === "sdo_rx", cs = b[0] >> 5;
|
||||
from = client ? `client → ${node}` : `server ${node}`;
|
||||
cls.fill("b-pad"); cls[0] = "b-cmd";
|
||||
const bin = b[0].toString(2).padStart(8, "0");
|
||||
const segment = cs === 0; // a data segment, either way
|
||||
const withIndex = client ? [1, 2, 4].includes(cs) : [2, 3, 4].includes(cs);
|
||||
bits = segment
|
||||
? `<span class="bits-cs">${bin.slice(0, 3)}</span><span class="bits-x">${bin[3]}</span><span class="bits-n">${bin.slice(4, 7)}</span><span class="bits-es">${bin[7]}</span>`
|
||||
: `<span class="bits-cs">${bin.slice(0, 3)}</span><span class="bits-x">${bin[3]}</span><span class="bits-n">${bin.slice(4, 6)}</span><span class="bits-es">${bin.slice(6)}</span>`;
|
||||
note = (client ? SDO_CLIENT : SDO_SERVER)[cs] || `command ${cs}`;
|
||||
if (withIndex && b.length >= 4) {
|
||||
cls[1] = cls[2] = "b-idx"; cls[3] = "b-sub";
|
||||
note += ` ${hex(b[1] | (b[2] << 8), 4).slice(2)}:${b[3].toString(16).toUpperCase().padStart(2, "0")}`;
|
||||
}
|
||||
if (cs === 4 && b.length === 8) {
|
||||
for (let i = 4; i < 8; i++) cls[i] = "b-pay";
|
||||
const code = le(b.slice(4)); note += ` ${hex(code, 8)}${ABORT[code] ? ` (${ABORT[code]})` : ""}`;
|
||||
} else if ((client && cs === 1) || (!client && cs === 2)) { // init with data or size
|
||||
const n = (b[0] >> 2) & 3, e = (b[0] >> 1) & 1, s = b[0] & 1;
|
||||
if (e) { const end = s ? 8 - n : 8; for (let i = 4; i < end; i++) cls[i] = "b-pay"; note += `, expedited ${end - 4} B: ${value(4, end)}`; }
|
||||
else if (s) { for (let i = 4; i < 8; i++) cls[i] = "b-pay"; note += `, segmented, ${le(b.slice(4, 8))} B`; }
|
||||
} else if (segment) {
|
||||
const n = (b[0] >> 1) & 7, end = 8 - n;
|
||||
for (let i = 1; i < end; i++) cls[i] = "b-pay";
|
||||
note += `, toggle ${(b[0] >> 4) & 1}, ${end - 1} B${b[0] & 1 ? ", last" : ""}`;
|
||||
}
|
||||
}
|
||||
return { b, cls, from, bits, note };
|
||||
}
|
||||
|
||||
function renderTrace() {
|
||||
const box = document.getElementById("trace-box");
|
||||
const atBottom = box.scrollHeight - box.scrollTop - box.clientHeight < 24;
|
||||
const filter = document.getElementById("filter").value.trim().toLowerCase();
|
||||
let prev = null;
|
||||
const rows = [];
|
||||
for (const f of trace) {
|
||||
const d = f.decoded ||= decode(f);
|
||||
const cob = f.extended ? hex(f.id, 8) : hex(f.id, 3);
|
||||
const dt = prev ? ((f.at - prev.at) * 1000).toFixed(1) : "";
|
||||
prev = f;
|
||||
if (filter && ![f.interface, cob, d.from, d.note].join(" ").toLowerCase().includes(filter)) continue;
|
||||
const data = d.b.map((x, i) => `<span class="${d.cls[i]}">${x.toString(16).toUpperCase().padStart(2, "0")}</span>`).join(" ");
|
||||
rows.push(`<tr><td class="num">${f.seq}</td><td class="num">${(f.at - t0).toFixed(4)}</td><td class="num">${dt}</td><td>${esc(f.interface)}</td><td><code>${cob}</code></td><td><code>${data}</code></td><td>${esc(d.from)}</td><td><code>${d.bits}</code></td><td>${esc(d.note)}</td></tr>`);
|
||||
}
|
||||
fill("trace", rows, trace.length ? "No frame matches." : "No frame yet.", 9);
|
||||
if (atBottom) box.scrollTop = box.scrollHeight;
|
||||
}
|
||||
|
||||
async function poll() {
|
||||
const status = document.getElementById("status");
|
||||
try {
|
||||
const response = await fetch("/api/can", { cache: "no-store" });
|
||||
const response = await fetch(paused ? "/api/can" : `/api/can?since=${logSince}`, { cache: "no-store" });
|
||||
if (response.status === 401) { status.textContent = "Not logged in — log in on the map first."; return; }
|
||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||
const date = Date.parse(response.headers.get("Date"));
|
||||
if (date) clockOffset = Math.abs(date / 1000 - Date.now() / 1000) > 2 ? date / 1000 - Date.now() / 1000 : 0;
|
||||
render(last = await response.json());
|
||||
if (last.log?.length) {
|
||||
logSince = last.log.at(-1).seq;
|
||||
t0 ??= last.log[0].at;
|
||||
trace = trace.concat(last.log).slice(-TRACE_MAX);
|
||||
renderTrace();
|
||||
}
|
||||
const silent = Object.keys(last.nodes).filter((id) => last.nodes[id].silent);
|
||||
status.className = silent.length ? "bad" : "";
|
||||
status.textContent = (silent.length ? `⚠ node ${silent.join(", ")} silent · ` : "") + `updated ${new Date().toLocaleTimeString()}`;
|
||||
@@ -143,7 +260,9 @@
|
||||
setTimeout(poll, 1000);
|
||||
}
|
||||
}
|
||||
document.getElementById("filter").addEventListener("input", () => last && render(last));
|
||||
document.getElementById("filter").addEventListener("input", () => { if (last) render(last); renderTrace(); });
|
||||
document.getElementById("pause").addEventListener("click", (e) => { paused = !paused; e.target.textContent = paused ? "Resume" : "Pause"; });
|
||||
document.getElementById("clear").addEventListener("click", () => { trace = []; t0 = null; renderTrace(); });
|
||||
poll();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in new issue
Block a user