Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
soem_fieldbus_driver.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <memory>
5#include <set>
6#include <span>
7#include <string>
8#include <vector>
9
11
12struct ecx_context;
13
14namespace mm::comm::soem {
15
24 std::string ifname;
31 bool mailboxStatusFmmu = false;
32};
33
50 public:
54
56 ~SoemFieldbusDriver() override;
57
60
67 std::expected<void, std::string> init() override;
68
76 std::expected<int, std::string> scan() override;
77
80 SlaveInfo slaveInfo(uint16_t position) const override;
81
83 uint16_t slaveState(uint16_t position) const override;
84
86 std::expected<void, std::string> configureProcessData() override;
87
90
92 std::vector<SlaveConfig> busConfig() const override;
93
102 int exchangeProcessData(std::span<const uint8_t> outputs, std::span<uint8_t> inputs) override;
103
105 void stop() override;
106
108 std::expected<std::vector<FieldbusDriver::SlaveStateRaw>, std::string> readStates(
109 const std::vector<uint16_t>& positions) override;
110
112 std::expected<std::vector<SlaveDiagnostics>, std::string> readDiagnostics(
113 const std::vector<uint16_t>& positions) override;
114
116 std::expected<std::vector<DcSyncDiagnostics>, std::string> readDcSync(
117 const std::vector<uint16_t>& positions) override;
118
120 std::expected<std::vector<uint8_t>, std::string> readSdo(uint16_t slavePosition, uint16_t index,
121 uint8_t subindex) override;
122
124 std::expected<std::vector<uint8_t>, std::string> readSdoComplete(uint16_t slavePosition,
125 uint16_t index) override;
126
128 std::expected<void, std::string> writeSdo(uint16_t slavePosition, uint16_t index,
129 uint8_t subindex,
130 std::span<const uint8_t> data) override;
131
133 std::expected<std::vector<OdEntry>, std::string> readObjectDictionary(
134 uint16_t slavePosition) override;
135
137 std::expected<std::vector<uint8_t>, std::string> readSii(uint16_t slavePosition) override;
138
140 std::expected<void, std::string> writeSii(uint16_t slavePosition,
141 std::span<const uint8_t> data) override;
142
144 std::expected<std::vector<uint8_t>, std::string> readFile(uint16_t slavePosition,
145 const std::string& filename) override;
146
148 std::expected<void, std::string> writeFile(uint16_t slavePosition, const std::string& filename,
149 std::span<const uint8_t> data) override;
150
156 std::expected<void, std::string> readRegister(uint16_t slavePosition, uint16_t address,
157 std::span<uint8_t> data) override;
158
164 std::expected<void, std::string> writeRegister(uint16_t slavePosition, uint16_t address,
165 std::span<const uint8_t> data) override;
166
168 std::expected<ProcessDataWatchdogConfig, std::string> processDataWatchdog(
169 uint16_t slavePosition) override;
170
172 std::expected<ProcessDataWatchdogConfig, std::string> setProcessDataWatchdog(
173 uint16_t slavePosition, std::chrono::nanoseconds timeout) override;
174
176 int slaveCount() const;
177
180 const std::vector<uint16_t>& positions, std::optional<EtherCatState> requiredState,
181 EtherCatState targetState, std::chrono::steady_clock::duration timeout,
182 std::chrono::steady_clock::duration resendInterval = std::chrono::seconds(2),
183 std::function<void()> tick = nullptr, std::function<bool()> shouldAbort = nullptr) override;
184
185 private:
195 std::expected<void, std::string> deactivateMailboxStatusFmmus();
196
212 std::expected<void, std::string> blockLrwOnPruIcssSlaves();
213
218 void closeContext();
219
220 std::string ifname_;
221 // Keep SOEM 2.0's mailbox-status FMMU active (see the constructor). Default false: the FMMU is
222 // deactivated after every map by deactivateMailboxStatusFmmus().
223 bool mailboxStatusFmmu_ = false;
224 // ecx_contextt is several hundred KB (EC_MAXSLAVE slave entries) — heap-
225 // allocated and null until init() succeeds.
226 std::unique_ptr<ecx_context> ctx_;
227 // EtherCAT IOmap: ecx_config_map_group lays the whole bus's process data out here as
228 // [all outputs | all inputs]. Sized to kMaxProcessImageBytes so it matches the cap
229 // configureProcessData() enforces and the ProcessBuffer scratch layered on top — the
230 // three must agree, or a bus the rest of the stack accepts would overflow or be rejected
231 // here. At ~100 bytes per axis across both directions this holds ~320 axes — far beyond the
232 // ~50 the stack targets.
233 uint8_t map_[kMaxProcessImageBytes]{};
234 // 1-based positions whose context currently holds BOOT-sized mailbox sync
235 // managers (set when we drive a slave into BOOT). ecx_config_init programs the
236 // correct PRE-OP mailbox SMs for every slave during scan(), so a fresh-scan
237 // INIT→PRE-OP needs no reprogramming; only a slave returning from BOOT (e.g.
238 // after a firmware download) carries stale BOOT SMs that must be reset.
239 // Guarded by controlPlaneMutex_. Cleared by scan() (which re-runs ecx_config_init).
240 std::set<uint16_t> bootMailboxSlaves_;
241};
242
243} // namespace mm::comm::soem
Abstract interface for an EtherCAT fieldbus driver.
Definition fieldbus_driver.h:343
SOEM-backed EtherCAT fieldbus driver.
Definition soem_fieldbus_driver.h:49
std::expected< void, std::string > writeFile(uint16_t slavePosition, const std::string &filename, std::span< const uint8_t > data) override
Writes a file to the slave via File over EtherCAT (FoE).
Definition soem_fieldbus_driver.cc:965
std::expected< void, std::string > writeSdo(uint16_t slavePosition, uint16_t index, uint8_t subindex, std::span< const uint8_t > data) override
Writes an object dictionary entry to a slave (CoE SDO download).
Definition soem_fieldbus_driver.cc:721
void transitionToState(const std::vector< uint16_t > &positions, std::optional< EtherCatState > requiredState, EtherCatState targetState, std::chrono::steady_clock::duration timeout, std::chrono::steady_clock::duration resendInterval=std::chrono::seconds(2), std::function< void()> tick=nullptr, std::function< bool()> shouldAbort=nullptr) override
Commands a set of devices to targetState and blocks until all arrive or timeout elapses.
Definition soem_fieldbus_driver.cc:1231
std::expected< std::vector< OdEntry >, std::string > readObjectDictionary(uint16_t slavePosition) override
Enumerates the entire CoE object dictionary of a slave via SDO Info.
Definition soem_fieldbus_driver.cc:782
int exchangeProcessData(std::span< const uint8_t > outputs, std::span< uint8_t > inputs) override
Copies outputs into the IOmap, sends and receives, copies inputs back out.
Definition soem_fieldbus_driver.cc:433
std::expected< void, std::string > writeRegister(uint16_t slavePosition, uint16_t address, std::span< const uint8_t > data) override
Writes bytes to an ESC register via FPWR.
Definition soem_fieldbus_driver.cc:1006
std::expected< std::vector< SlaveDiagnostics >, std::string > readDiagnostics(const std::vector< uint16_t > &positions) override
Reads live link-quality and watchdog diagnostics for each slave in positions.
Definition soem_fieldbus_driver.cc:513
std::expected< void, std::string > init() override
Opens the NIC and initialises the SOEM master context.
Definition soem_fieldbus_driver.cc:38
std::expected< void, std::string > writeSii(uint16_t slavePosition, std::span< const uint8_t > data) override
Writes a raw SII (EEPROM) image to a slave.
Definition soem_fieldbus_driver.cc:897
std::expected< std::vector< DcSyncDiagnostics >, std::string > readDcSync(const std::vector< uint16_t > &positions) override
Reads live distributed-clock synchronisation status for each slave in positions.
Definition soem_fieldbus_driver.cc:573
SlaveInfo slaveInfo(uint16_t position) const override
Returns the immutable identity fields for the slave at position.
Definition soem_fieldbus_driver.cc:470
std::expected< std::vector< uint8_t >, std::string > readFile(uint16_t slavePosition, const std::string &filename) override
Reads a file from the slave via File over EtherCAT (FoE).
Definition soem_fieldbus_driver.cc:932
void stop() override
Closes the NIC and releases all driver resources.
Definition soem_fieldbus_driver.cc:452
std::expected< ProcessDataWatchdogConfig, std::string > processDataWatchdog(uint16_t slavePosition) override
Reads the process-data (sync-manager) watchdog configuration of one slave.
Definition soem_fieldbus_driver.cc:1049
SoemFieldbusDriver(const SoemFieldbusDriver &)=delete
~SoemFieldbusDriver() override
Closes the NIC if init() succeeded.
Definition soem_fieldbus_driver.cc:36
std::expected< ProcessDataWatchdogConfig, std::string > setProcessDataWatchdog(uint16_t slavePosition, std::chrono::nanoseconds timeout) override
Sets the process-data (sync-manager) watchdog timeout of one slave.
Definition soem_fieldbus_driver.cc:1079
std::expected< std::vector< FieldbusDriver::SlaveStateRaw >, std::string > readStates(const std::vector< uint16_t > &positions) override
Reads the current AL Status for each slave in positions.
Definition soem_fieldbus_driver.cc:498
std::expected< std::vector< uint8_t >, std::string > readSdoComplete(uint16_t slavePosition, uint16_t index) override
Reads an entire object in one CoE SDO upload using Complete Access.
Definition soem_fieldbus_driver.cc:680
std::expected< int, std::string > scan() override
Scans the bus for slaves and configures their sync managers and FMMUs.
Definition soem_fieldbus_driver.cc:59
uint16_t slaveState(uint16_t position) const override
Returns the last-known AL status for a slave without any bus I/O.
Definition soem_fieldbus_driver.cc:490
std::expected< void, std::string > readRegister(uint16_t slavePosition, uint16_t address, std::span< uint8_t > data) override
Reads bytes from an ESC register via FPRD.
Definition soem_fieldbus_driver.cc:987
int slaveCount() const
Returns the number of discovered slaves, or 0 before discovery.
Definition soem_fieldbus_driver.cc:485
std::expected< std::vector< uint8_t >, std::string > readSdo(uint16_t slavePosition, uint16_t index, uint8_t subindex) override
Reads an object dictionary entry via CoE SDO upload.
Definition soem_fieldbus_driver.cc:631
SoemFieldbusDriver & operator=(const SoemFieldbusDriver &)=delete
std::vector< SlaveConfig > busConfig() const override
Returns the static ESC configuration of every slave, as last programmed by the master.
Definition soem_fieldbus_driver.cc:363
PdoLayout processDataLayout() override
Returns the current process-data layout established by configureProcessData.
Definition soem_fieldbus_driver.cc:331
std::expected< void, std::string > configureProcessData() override
Maps the process data and lays out the IOmap (one-time, control-plane).
Definition soem_fieldbus_driver.cc:176
std::expected< std::vector< uint8_t >, std::string > readSii(uint16_t slavePosition) override
Reads the raw Slave Information Interface (SII / EEPROM) image of a slave.
Definition soem_fieldbus_driver.cc:865
Definition soem_fieldbus_driver.cc:31
EtherCatState
EtherCAT Application Layer state values.
Definition fieldbus_driver.h:34
constexpr std::size_t kMaxProcessImageBytes
Maximum size in bytes of the combined process-data image (all outputs + all inputs).
Definition fieldbus_driver.h:28
Layout of the process-data images, produced by configureProcessData.
Definition fieldbus_driver.h:155
Immutable identity fields read from a slave's EEPROM during configuration.
Definition fieldbus_driver.h:96
Construction-time configuration for SoemFieldbusDriver.
Definition soem_fieldbus_driver.h:22
std::string ifname
Resolved OS network interface name (e.g. "eth0", "enp3s0").
Definition soem_fieldbus_driver.h:24
bool mailboxStatusFmmu
Definition soem_fieldbus_driver.h:31