Your first bus¶
EtherCAT refresher
Poster → ESM and SubDevice structure and ESC.
TL;DR
simulated_bus runs a master and an emulated slave in one process in five steps: parse
an ESI file, build the slave, connect the master, bring the bus to OP, and exchange process
data. The code below comes from the example that CI compiles.
Run it¶
After building from source, from the repository root:
./build/examples/master/simulated_bus/simulated_bus \
-f examples/slave/lan9252/freedom-k64f/nuttx/freedom-k64f.xml -t Board
Device: Board (product 0x00defede, revision 0x5a01)
Detected 1 slave(s)
SAFE_OP reached
OPERATIONAL reached
Process data: 12 input byte(s), 3 output byte(s)
What runs¶
flowchart LR
esi["ESI file<br/>freedom-k64f.xml"]
subgraph proc["One process"]
bus["Bus"] --> link["Link"] --> sock["LoopbackSocket"] --> esc["EmulatedESC"]
slave["slave::Slave"] --> esc
end
esi -->|EEPROM image| esc
esi -->|dictionary| slave
On hardware, a Socket on a network card replaces LoopbackSocket, and a Lan9252 or
XMC4800 replaces EmulatedESC. The rest of the code stays the same.
1. Parse the ESI¶
ESI::Parser parser;
ESI::DeviceFilter filter;
if (not device_type.empty())
{
filter.type = device_type;
}
ESI::Device dev;
try
{
dev = parser.loadDevice(esi_file, filter);
}
catch (std::exception const& e)
{
std::cerr << "Cannot load device: " << e.what() << std::endl;
return 1;
}
CoE::materializeStorage(dev.dictionary);
printf("Device: %s (product 0x%08x, revision 0x%x)\n", dev.type.c_str(), dev.product_code, dev.revision_no);
loadDevice()selects a device by its<Type>(-t), or uses the first one.- One device description provides both the EEPROM image and the CoE dictionary.
materializeStorage()allocates storage for dictionary entries that don't have any.
2. Build the emulated slave¶
EmulatedESC esc;
esc.loadEeprom(ESI::buildEepromImage(dev));
PDO pdo(&esc);
slave::Slave sl(&esc, &pdo);
std::unique_ptr<mailbox::response::Mailbox> mbx;
if (dev.mailbox and dev.mailbox->coe)
{
mbx = std::make_unique<mailbox::response::Mailbox>(&esc, 1024);
mbx->enableCoE(dev.dictionary); // dev owns the OD; injected into both
sl.setMailbox(mbx.get());
sl.setDictionary(&dev.dictionary);
}
constexpr uint32_t PDO_SIZE = 4096; // a full frame: never overflow on large PDOs
std::vector<uint8_t> inputs(PDO_SIZE, 0);
std::vector<uint8_t> outputs(PDO_SIZE, 0);
pdo.setInput(inputs.data(), PDO_SIZE);
pdo.setOutput(outputs.data(), PDO_SIZE);
sl.start(); // DL_STATUS is derived from the topology by the LoopbackSocket's network
EmulatedESCstands in for the ESC chip and loads the EEPROM image.PDOandslave::Slaveare the classes used by slave firmware.- The mailbox serves CoE requests from the dictionary. The application owns the dictionary and the process data buffers.
3. Connect the master¶
bool op_phase = false;
auto tick = [&]()
{
sl.routine();
if (op_phase and sl.state() == State::SAFE_OP)
{
sl.validateOutputData();
}
};
auto loopback = std::make_shared<LoopbackSocket>(std::vector<EmulatedESC*>{&esc}, tick);
auto link = std::make_shared<Link>(loopback, std::make_shared<SocketNull>(), [](){});
Bus bus(link);
LoopbackSocketpasses each frame to the emulated ESC, then callstickto run oneroutine()of the slave stack.Linktakes a nominal socket and a redundancy socket.SocketNullmeans there is no redundancy.validateOutputData()lets the slave enter OP. In real firmware, the application decides when its outputs are valid.
4. Bring the bus to OP¶
auto noop = [](DatagramState const&) {};
auto cyclic = [&]()
{
bus.processDataRead(noop);
bus.processDataWrite(noop);
};
std::vector<uint8_t> iomap(4096); // master-side process image
bus.init(100ms);
printf("Detected %zu slave(s)\n", bus.slaves().size());
bus.createMapping(iomap.data(), iomap.size());
init()finds the slaves, assigns them addresses, reads their EEPROMs, and brings them to PRE-OP.createMapping()lays out the process image iniomap, which the application owns.
bus.requestState(State::SAFE_OP);
bus.waitForState(State::SAFE_OP, 500ms);
printf("SAFE_OP reached\n");
for (auto& s : bus.slaves())
{
for (int32_t i = 0; i < s.output.bsize; ++i)
{
s.output.data[i] = 0x55;
}
}
cyclic();
op_phase = true;
bus.requestState(State::OPERATIONAL);
bus.waitForState(State::OPERATIONAL, 500ms, cyclic);
printf("OPERATIONAL reached\n");
- A slave with outputs enters OP only after receiving valid outputs, so the example writes them before requesting OP.
waitForState()keeps callingcyclicwhile it waits, so process data continues to flow.- With
--dc, the example also sets up distributed clocks before SAFE-OP.
5. Exchange process data¶
for (int cycle = 0; cycle < 3; ++cycle)
{
cyclic();
}
auto const& s0 = bus.slaves().at(0);
printf("Process data: %d input byte(s), %d output byte(s)\n", s0.input.bsize, s0.output.bsize);
processDataRead() and processDataWrite() each send one datagram and wait for a reply. A
real-time loop batches datagrams instead; see the cyclic loop.
Deep dive: one cycle through both stacks
sequenceDiagram
participant App as Application
participant Bus
participant Link
participant Sock as LoopbackSocket
participant ESC as EmulatedESC
participant Sl as slave::Slave
App->>Bus: processDataRead(), processDataWrite()
Bus->>Link: datagrams
Link->>Sock: write(frame)
Sock->>ESC: process each datagram
Sock->>Sl: routine()
Sock-->>Link: read(frame)
Link-->>Bus: callbacks
Recap¶
| Step | KickCAT types |
|---|---|
| Device description | ESI::Parser, ESI::buildEepromImage() |
| Slave | EmulatedESC, PDO, slave::Slave, mailbox::response::Mailbox |
| Transport | LoopbackSocket, Link, SocketNull |
| Master | Bus |
Next¶
- Emulate another device: pass any vendor ESI file with
-f, such as one of Beckhoff's ESI files. - Run several slaves in a separate process: see Simulation.
- Run on real boards: see Hardware.