Virtual CAN bus with motor controllers
How a single shared CAN bus carries commands to, and feedback from, several motor controllers, how bus load grows with motor count and control rate, and why motors with higher identifiers get their feedback later.
What it demonstrates
How a single shared CAN bus carries commands to, and feedback from, several motor controllers, how bus load grows with motor count and control rate, and why motors with higher identifiers get their feedback later.
How it works
A discrete-event scheduler models the bus. At every control tick the robot computer queues one command frame per motor (illustrative IDs 0x101, 0x102, …). Whenever the bus is idle, the pending frame with the lowest identifier wins arbitration and occupies the bus for its full frame time, computed from the frame format with worst-case bit stuffing.
When a motor controller receives its command it queues a feedback frame (IDs 0x181, …) after a short processing delay. The round trip is measured from the control tick to the moment the feedback frame finishes. If the offered load exceeds the bus capacity, the queue grows until frames are dropped.
The animation runs in slow motion: by default 2 ms of bus time per real second.
Adjustable parameters
- Motor controllers (1–12)
- Control rate (50–1000 Hz)
- Nominal bit rate (125 kbit/s – 1 Mbit/s)
- Classical CAN or CAN FD (2 or 5 Mbit/s data phase, 8–64 byte payload)
- 11-bit or 29-bit identifiers
- Slow-motion factor
Known limitations
- Identifiers, payloads and the request–response pattern are illustrative, not Asimov's protocol.
- Frame times use worst-case bit stuffing; real frames are usually a little shorter.
- No error frames, retransmissions, bus-off handling or controller transmit-buffer effects.
- The CAN FD bit count treats the bit-rate switch as whole bits, a small approximation.
Equations
Core update rule
// When the bus goes idle, the lowest pending ID wins (lib/sim/ch05-can.ts)
const ready = pending.filter((f) => f.tQueued <= now);
const win = ready.reduce((a, b) => (b.id < a.id ? b : a));
win.tStart = now;
win.tEnd = now + frameTime(spec, bitrate, dataBitrate);
// on completion: a delivered command makes that motor queue its feedback
if (win.kind === "cmd") pending.push(feedbackFrame(win.motor, win.tEnd + replyDelay));Explained in Chapter 5: Communication & Firmware