10#include <unordered_map>
274bool setSlaveState(ecx_contextt* context, uint16_t slave,
int targetState);
305 const std::vector<mm::comm::base::PdoMappingEntry>& entries);
318void logIoMap(uint8_t* ioMap,
size_t totalBytes);
413 supportedProtocols, writeLength, writeOffset)
422struct SlaveInfoSyncManager {
446 uint16_t startAddress;
471struct SlaveInfoFmmu {
485 uint8_t logicalEndBit;
493 uint8_t logicalStartBit;
500 uint32_t logicalStartAddress;
507 uint16_t logicalLength;
515 uint8_t physicalStartBit;
522 uint16_t physicalStartAddress;
534 logicalStartBit, logicalStartAddress,
535 logicalLength, physicalStartBit,
536 physicalStartAddress,
type)
551 uint16_t alStatusCode;
559 uint16_t aliasAddress;
566 std::vector<SlaveInfoFmmu> fmmus;
583 uint32_t productCode;
599 uint32_t revisionNumber;
607 std::vector<SlaveInfoSyncManager> syncManagers;
629 revisionNumber, syncManagers,
name, vendorId)
681 const
std::
string& iface);
752 int getState(
bool forceRefresh =
true)
const;
773 bool setState(
int targetState,
int timeoutMs = 0,
int intervalMs = 100);
907 template <
typename T = std::vector<u
int8_t>>
910 std::lock_guard<std::mutex> lock(mailboxMutex_);
914 if (state == EC_STATE_INIT || state == EC_STATE_BOOT) {
915 throw std::runtime_error(
916 "To upload object dictionary entries, the device must be in the (2) "
917 "PRE-OPERATIONAL, (4) SAFE-OPERATIONAL, or (8) OPERATIONAL state. "
918 "The current state is " +
927 const std::unique_ptr<std::uint8_t[]> data =
928 std::make_unique<std::uint8_t[]>(parameter.byteLength);
929 auto wkc = ecx_SDOread(&context_, position_, parameter.index,
930 parameter.subindex,
false, ¶meter.byteLength,
931 data.get(), EC_TIMEOUTRXM * 3);
933 LOG_F(ERROR,
"Device %d: SDO read failed for %#04x:%02x! WKC: %d",
934 position_, parameter.index, parameter.subindex, wkc);
937 throw std::runtime_error(
"Device " + std::to_string(position_) +
938 ": SDO read failed for " +
id +
939 "! WKC: " + std::to_string(wkc));
941 std::vector<uint8_t> receivedData(data.get(),
942 data.get() + parameter.byteLength);
943 if (!parameter.trySetValue(receivedData)) {
945 "Device %d: Failed to set the value of parameter %#04x:%02x!",
946 position_, parameter.index, parameter.subindex);
952 if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
953 return parameter.data;
956 auto value = parameter.getValue();
957 if (
auto ptr = std::get_if<T>(&
value)) {
960 throw std::bad_variant_access();
989 template <
typename T = std::vector<u
int8_t>>
991 std::lock_guard<std::mutex> lock(mailboxMutex_);
995 if (state == EC_STATE_INIT || state == EC_STATE_BOOT) {
996 throw std::runtime_error(
997 "To download object dictionary entries, the device must be in the "
998 "(2) PRE-OPERATIONAL, (4) SAFE-OPERATIONAL, or (8) OPERATIONAL "
999 "state. The current state is " +
1006 std::vector<uint8_t> data;
1007 if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
1011 if (!parameter.trySetValue(
value)) {
1013 "Device %d: Failed to set the value of parameter %#04x:%02x!",
1014 position_, parameter.index, parameter.subindex);
1017 data = parameter.data;
1021 if (data.size() != parameter.byteLength) {
1022 LOG_F(ERROR,
"Data size mismatch: expected %d, got %zu",
1023 parameter.byteLength, data.size());
1024 throw std::runtime_error(
"Data size does not match the parameter size!");
1028 auto wkc = ecx_SDOwrite(&context_, position_, parameter.index,
1029 parameter.subindex,
false, parameter.byteLength,
1030 data.data(), EC_TIMEOUTRXM * 3);
1034 LOG_F(ERROR,
"Device %d: SDO write failed for %#04x:%02x! WKC: %d",
1035 position_, parameter.index, parameter.subindex, wkc);
1060 void updatePdoMappings();
1073 void logPdoMappings()
const;
1104 void updateOutputs();
1127 void updateParametersFromInputs();
1129 std::vector<uint8_t> readFile(
1130 const std::string& filename,
1131 const std::chrono::steady_clock::duration expiryTime);
1134 ecx_contextt& context_;
1135 const uint16_t position_;
1138 std::mutex mailboxMutex_;
1167 explicit Master(
const std::string& iface);
1220 void init(
const std::string& iface);
1348 const std::vector<std::unique_ptr<Slave>>&
getSlaves()
const;
1360 return fieldbus_->context.grouplist[fieldbus_->group].outputsWKC * 2 +
1361 fieldbus_->context.grouplist[fieldbus_->group].inputsWKC;
1431 std::unique_ptr<Fieldbus> fieldbus_;
1444 std::vector<std::unique_ptr<Slave>> slaves_;
Represents a device parameter identified by index and subindex.
Definition base.h:468
std::pair< uint16_t, uint8_t > Address
Alias for a pair of uint16_t and uint8_t representing a parameter address.
Definition base.h:478
int roundtrip()
Performs a complete send/receive process data cycle on the fieldbus.
Definition soem.cc:1303
int initSlaves()
Initializes the slave devices on the EtherCAT network.
Definition soem.cc:1276
Master()
Default constructor for the Master class.
Definition soem.cc:1224
void exchangeProcessDataAndUpdateParameters()
Exchanges process data with slaves and updates parameters based on the response.
Definition soem.cc:1308
size_t getIoMapSize() const
Returns the total size of the I/O map for the current group.
Definition soem.cc:1271
const std::vector< std::unique_ptr< Slave > > & getSlaves() const
Returns a reference to the list of slaves.
Definition soem.cc:1299
Fieldbus & getFieldbus() const
Returns a reference to the Fieldbus instance.
Definition soem.cc:1267
void init(const std::string &iface)
Initializes the fieldbus using the specified network interface.
Definition soem.cc:1231
std::string getInterfaceName() const
Retrieves the interface name used by the fieldbus.
Definition soem.cc:1269
void deinit()
Deinitializes the fieldbus and releases associated resources.
Definition soem.cc:1249
bool start()
Initializes and starts the fieldbus master.
Definition soem.cc:1331
~Master()
Destructor for the Master class.
Definition soem.cc:1265
int expectedWkc() const
Calculates the expected working counter (WKC) for the current fieldbus group.
Definition soem.h:1359
void loadParameters(bool readValues=false)
Loads the parameters (object dictionary entries) from the EtherCAT slave device.
Definition soem.cc:853
mm::comm::base::Parameter & findParameter(uint16_t index, uint8_t subindex)
Finds and returns a reference to a Parameter in the parameters map.
Definition soem.cc:1057
T upload(uint16_t index, uint8_t subindex, bool refresh=true)
Uploads a parameter value from the device's object dictionary.
Definition soem.h:908
int getState(bool forceRefresh=true) const
Retrieves the current state of the slave.
Definition soem.cc:825
uint32_t getProductCode() const
Gets the Product Code of the slave device.
Definition soem.cc:817
uint32_t getRevisionNumber() const
Gets the Revision Number of the slave device.
Definition soem.cc:821
std::unordered_map< mm::comm::base::Parameter::Address, mm::comm::base::Parameter > & getParametersMap()
Returns a reference to the internal parameters map.
Definition soem.cc:1053
void clearParameters()
Clears all loaded object dictionary parameters.
Definition soem.cc:1049
void logTxPdoMappedParameters()
Logs all mapped TxPDO parameters with their current values.
Definition soem.cc:1034
Slave(ecx_contextt &context, uint16_t position)
Constructs a Slave object with a given context and position.
Definition soem.cc:808
void logRxPdoMappedParameters()
Logs all mapped RxPDO parameters with their current values.
Definition soem.cc:1019
uint16_t getPosition() const
Gets the position of the slave.
Definition soem.cc:811
bool setState(int targetState, int timeoutMs=0, int intervalMs=100)
Attempts to set the slave to the specified EtherCAT state and optionally waits until it is reached.
Definition soem.cc:829
void logParameters() const
Logs all loaded object dictionary parameters.
Definition soem.cc:1013
uint32_t getVendorId() const
Gets the Vendor ID of the slave device.
Definition soem.cc:813
bool download(uint16_t index, uint8_t subindex, const T &value)
Downloads a value to the specified object dictionary entry via SDO.
Definition soem.h:990
uint8_t type
Definition co_dictionary.h:1
const char ** name
Definition co_dictionary.h:7
uint8_t * value
Definition co_dictionary.h:9
uint16_t index
Definition co_dictionary.h:0
std::string makeParameterId(int index, int subindex)
Formats the given index and subindex into a parameter identifier string.
Definition base.h:1579
mm::comm::base::PdoMappings readPdoMappings(ecx_contextt *context, uint16_t slave)
Read and parse all mapped PDO entries for a given slave.
Definition soem.cc:711
void logPdoMappingEntries(const std::vector< mm::comm::base::PdoMappingEntry > &entries)
Logs PDO mapping entries grouped by PDO index.
Definition soem.cc:777
std::string convertSlaveStateToString(int state)
Converts a numeric EtherCAT state value into a human-readable string.
Definition soem.cc:413
bool transitionToHigherSlaveState(ecx_contextt *context, uint16_t slave, int targetState)
Transitions a slave to a higher EtherCAT state.
Definition soem.cc:442
std::string uiConfigWithDefaultPdoMapping
Default UI configuration used for PDO mapping.
Definition soem.h:49
void initFieldbus(std::unique_ptr< Fieldbus > &fieldbus, const std::string &iface)
Initializes a Fieldbus instance for EtherCAT communication.
Definition soem.cc:1191
void updateMailboxSyncManagersOnNextState(ecx_contextt *context, uint16_t slave, int targetState)
Updates the mailbox and sync manager (SM) configurations for an EtherCAT slave when transitioning to ...
Definition soem.cc:504
void logIoMap(uint8_t *ioMap, size_t totalBytes)
Logs the I/O map as a formatted hexadecimal string.
Definition soem.cc:798
void checkPdoMapping(ecx_contextt *context, uint16 slave)
Diagnoses and prints the PDO mapping for a given slave.
Definition soem.cc:310
void configureDetectedSmmModule(ecx_contextt *context, uint16_t slave)
Reads the detected SMM module ID from the slave device and configures the module identification objec...
Definition soem.cc:579
std::map< std::string, std::string > mapMacAddressesToInterfaces()
Maps MAC addresses to their corresponding network interface names.
Definition soem.cc:36
int setPdoMappingFromUiConfig(ecx_contextt *context, uint16 slave)
Sets PDO mapping for a specified EtherCAT slave using UI configuration.
Definition soem.cc:215
uint16_t getSlaveState(ecx_contextt *context, uint16_t slave, bool forceRefresh)
Retrieves the current state of a specific EtherCAT slave.
Definition soem.cc:392
uint16 position
Definition soem.h:643
void logSlaveInfo(ecx_contextt *context, uint16 position)
Logs detailed information about a specific EtherCAT slave in JSON format.
Definition soem.cc:209
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SlaveInfoMailbox, readLength, readOffset, supportedProtocols, writeLength, writeOffset) struct SlaveInfoSyncManager
Information about a slave's Sync Manager (SM) configuration.
Definition soem.h:412
bool setSlaveState(ecx_contextt *context, uint16_t slave, int targetState)
Sets the state of a specified EtherCAT slave.
Definition soem.cc:609
SlaveInfo getSlaveInfo(ecx_contextt *context, uint16 position)
Definition soem.cc:157
Specialization of std::hash for std::pair<uint16_t, uint8_t>.
Definition util.h:609
Represents the mapped PDO entries for a slave device.
Definition base.h:150
Structure representing the Fieldbus configuration and context for EtherCAT communication.
Definition soem.h:334
boolean ecaterror
Definition soem.h:350
ec_idxstackT idxstack
Definition soem.h:349
ec_eepromFMMUt eepFMMU
Definition soem.h:356
ec_groupt grouplist[EC_MAXGROUP]
Definition soem.h:345
std::string iface
Definition soem.h:336
uint8 group
Definition soem.h:337
int slavecount
Definition soem.h:344
uint8 esibuf[EC_MAXEEPBUF]
Definition soem.h:346
uint32 esimap[EC_MAXEEPBITMAP]
Definition soem.h:347
int64 DCtime
Definition soem.h:351
ec_eepromSMt eepSM
Definition soem.h:355
ec_PDOassignt PDOassign[EC_MAX_MAPT]
Definition soem.h:353
uint8 map[4096]
Definition soem.h:341
ecx_contextt context
Definition soem.h:335
int roundtripTime
Definition soem.h:338
ec_PDOdesct PDOdesc[EC_MAX_MAPT]
Definition soem.h:354
ecx_portt port
Definition soem.h:342
ec_slavet slavelist[EC_MAXSLAVE]
Definition soem.h:343
ec_SMcommtypet SMcommtype[EC_MAX_MAPT]
Definition soem.h:352
ec_eringt elist
Definition soem.h:348
Information about a slave's mailbox configuration.
Definition soem.h:366
uint16_t writeOffset
Offset of the mailbox write area within the slave's memory.
Definition soem.h:409
uint16_t supportedProtocols
Supported mailbox protocols as a bitmask.
Definition soem.h:393
uint16_t readOffset
Offset of the mailbox read area within the slave's memory.
Definition soem.h:381
uint16_t readLength
Length of the mailbox read area (in bytes).
Definition soem.h:373
uint16_t writeLength
Length of the mailbox write area (in bytes).
Definition soem.h:401