273bool setSlaveState(ecx_contextt *context, uint16_t slave,
int targetState);
315void logIoMap(uint8_t *ioMap,
size_t totalBytes);
374 const std::string &iface);
392 explicit Slave(ecx_contextt &context, uint16_t position);
445 int getState(
bool forceRefresh =
true)
const;
466 bool setState(
int targetState,
int timeoutMs = 0,
int intervalMs = 100);
555 std::unordered_map<common::ParameterKey, common::Parameter> &
599 template <
typename T = std::vector<u
int8_t>>
602 std::lock_guard<std::mutex> lock(mailboxMutex_);
606 if (state == EC_STATE_INIT || state == EC_STATE_BOOT) {
607 throw std::runtime_error(
608 "To upload object dictionary entries, the device must be in the (2) "
609 "PRE-OPERATIONAL, (4) SAFE-OPERATIONAL, or (8) OPERATIONAL state. "
610 "The current state is " +
619 const std::unique_ptr<std::uint8_t[]> data =
620 std::make_unique<std::uint8_t[]>(parameter.byteLength);
621 auto wkc = ecx_SDOread(&context_, position_, parameter.index,
622 parameter.subindex,
false, ¶meter.byteLength,
623 data.get(), EC_TIMEOUTRXM * 3);
625 LOG_F(ERROR,
"Device %d: SDO read failed for %#04x:%02x! WKC: %d",
626 position_, parameter.index, parameter.subindex, wkc);
628 throw std::runtime_error(
"Device " + std::to_string(position_) +
629 ": SDO read failed for " +
id +
630 "! WKC: " + std::to_string(wkc));
632 std::vector<uint8_t> receivedData(data.get(),
633 data.get() + parameter.byteLength);
634 if (!parameter.trySetValue(receivedData)) {
636 "Device %d: Failed to set the value of parameter %#04x:%02x!",
637 position_, parameter.index, parameter.subindex);
643 if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
644 return parameter.data;
647 auto value = parameter.getValue();
648 if (
auto ptr = std::get_if<T>(&
value)) {
651 throw std::bad_variant_access();
680 template <
typename T = std::vector<u
int8_t>>
682 std::lock_guard<std::mutex> lock(mailboxMutex_);
686 if (state == EC_STATE_INIT || state == EC_STATE_BOOT) {
687 throw std::runtime_error(
688 "To download object dictionary entries, the device must be in the "
689 "(2) PRE-OPERATIONAL, (4) SAFE-OPERATIONAL, or (8) OPERATIONAL "
690 "state. The current state is " +
697 std::vector<uint8_t> data;
698 if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
702 if (!parameter.trySetValue(
value)) {
704 "Device %d: Failed to set the value of parameter %#04x:%02x!",
705 position_, parameter.index, parameter.subindex);
708 data = parameter.data;
712 if (data.size() != parameter.byteLength) {
713 LOG_F(ERROR,
"Data size mismatch: expected %d, got %zu",
714 parameter.byteLength, data.size());
715 throw std::runtime_error(
"Data size does not match the parameter size!");
719 auto wkc = ecx_SDOwrite(&context_, position_, parameter.index,
720 parameter.subindex,
false, parameter.byteLength,
721 data.data(), EC_TIMEOUTRXM * 3);
725 LOG_F(ERROR,
"Device %d: SDO write failed for %#04x:%02x! WKC: %d",
726 position_, parameter.index, parameter.subindex, wkc);
821 ecx_contextt &context_;
822 const uint16_t position_;
825 std::mutex mailboxMutex_;
826 std::unordered_map<common::ParameterKey, common::Parameter>
852 explicit Master(
const std::string &iface);
905 void init(
const std::string &iface);
1033 const std::vector<std::unique_ptr<soempp::Slave>> &
getSlaves()
const;
1045 return fieldbus_->context.grouplist[fieldbus_->group].outputsWKC * 2 +
1046 fieldbus_->context.grouplist[fieldbus_->group].inputsWKC;
1116 std::unique_ptr<Fieldbus> fieldbus_;
1129 std::vector<std::unique_ptr<Slave>> slaves_;
Represents a device parameter identified by index and subindex.
Definition: common.h:472
Fieldbus & getFieldbus() const
Returns a reference to the Fieldbus instance.
Definition: soempp.cc:1095
Master()
Default constructor for the Master class.
Definition: soempp.cc:1063
void init(const std::string &iface)
Initializes the fieldbus using the specified network interface.
Definition: soempp.cc:1070
size_t getIoMapSize() const
Returns the total size of the I/O map for the current group.
Definition: soempp.cc:1099
int initSlaves()
Initializes the slave devices on the EtherCAT network.
Definition: soempp.cc:1104
void exchangeProcessDataAndUpdateParameters()
Exchanges process data with slaves and updates parameters based on the response.
Definition: soempp.cc:1136
bool start()
Initializes and starts the fieldbus master.
Definition: soempp.cc:1159
std::string getInterfaceName() const
Retrieves the interface name used by the fieldbus.
Definition: soempp.cc:1097
const std::vector< std::unique_ptr< soempp::Slave > > & getSlaves() const
Returns a reference to the list of slaves.
Definition: soempp.cc:1127
int roundtrip()
Performs a complete send/receive process data cycle on the fieldbus.
Definition: soempp.cc:1131
void deinit()
Deinitializes the fieldbus and releases associated resources.
Definition: soempp.cc:1088
~Master()
Destructor for the Master class.
Definition: soempp.cc:1093
int expectedWkc() const
Calculates the expected working counter (WKC) for the current fieldbus group.
Definition: soempp.h:1044
Represents a slave in the EtherCAT network.
Definition: soempp.h:380
void updatePdoMappings()
Updates the PDO mapping entries for the slave.
Definition: soempp.cc:980
void logTxPdoMappedParameters()
Logs all mapped TxPDO parameters with their current values.
Definition: soempp.cc:942
std::unordered_map< common::ParameterKey, common::Parameter > & getParametersMap()
Returns a reference to the internal parameters map.
Definition: soempp.cc:958
uint32_t getRevisionNumber() const
Gets the Revision Number of the slave device.
Definition: soempp.cc:732
common::PdoMappings readPdoMappings() const
Reads the PDO mappings from the slave device.
Definition: soempp.cc:973
uint32_t getProductCode() const
Gets the Product Code of the slave device.
Definition: soempp.cc:728
void updateParametersFromInputs()
Updates parameters from the received input data.
Definition: soempp.cc:1013
void logPdoMappings() const
Logs the PDO (Process Data Object) mappings for the device.
Definition: soempp.cc:988
T upload(uint16_t index, uint8_t subindex, bool refresh=true)
Uploads a parameter value from the device's object dictionary.
Definition: soempp.h:600
int getState(bool forceRefresh=true) const
Retrieves the current state of the slave.
Definition: soempp.cc:736
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: soempp.cc:740
bool download(uint16_t index, uint8_t subindex, const T &value)
Downloads a value to the specified object dictionary entry via SDO.
Definition: soempp.h:681
uint32_t getVendorId() const
Gets the Vendor ID of the slave device.
Definition: soempp.cc:724
void logRxPdoMappedParameters()
Logs all mapped RxPDO parameters with their current values.
Definition: soempp.cc:929
void clearParameters()
Clears all loaded object dictionary parameters.
Definition: soempp.cc:955
common::Parameter & findParameter(uint16_t index, uint8_t subindex)
Finds and returns a reference to a Parameter in the parameters map.
Definition: soempp.cc:962
void logParameters() const
Logs all loaded object dictionary parameters.
Definition: soempp.cc:923
common::PdoMappings & getPdoMappings()
Returns a reference to the cached PDO mappings.
Definition: soempp.cc:994
void updateOutputs()
Updates the outputs for the slave device.
Definition: soempp.cc:996
uint16_t getPosition() const
Gets the position of the slave.
Definition: soempp.cc:722
void loadParameters(bool readValues=false)
Loads the parameters (object dictionary entries) from the EtherCAT slave device.
Definition: soempp.cc:764
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: common.h:1134
void checkPdoMapping(ecx_contextt *context, uint16 slave)
Diagnoses and prints the PDO mapping for a given slave.
Definition: soempp.cc:224
std::string convertSlaveStateToString(int state)
Converts a numeric EtherCAT state value into a human-readable string.
Definition: soempp.cc:327
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: soempp.cc:493
common::PdoMappings readPdoMappings(ecx_contextt *context, uint16_t slave)
Read and parse all mapped PDO entries for a given slave.
Definition: soempp.cc:625
void initFieldbus(std::unique_ptr< Fieldbus > &fieldbus, const std::string &iface)
Initializes a Fieldbus instance for EtherCAT communication.
Definition: soempp.cc:1030
bool setSlaveState(ecx_contextt *context, uint16_t slave, int targetState)
Sets the state of a specified EtherCAT slave.
Definition: soempp.cc:523
std::map< std::string, std::string > mapMacAddressesToInterfaces()
Maps MAC addresses to their corresponding network interface names.
Definition: soempp.cc:27
bool transitionToHigherSlaveState(ecx_contextt *context, uint16_t slave, int targetState)
Transitions a slave to a higher EtherCAT state.
Definition: soempp.cc:356
std::string uiConfigWithDefaultPdoMapping
Default UI configuration used for PDO mapping.
Definition: soempp.h:48
void logPdoMappingEntries(const std::vector< common::PdoMappingEntry > &entries)
Logs PDO mapping entries grouped by PDO index.
Definition: soempp.cc:690
void logIoMap(uint8_t *ioMap, size_t totalBytes)
Logs the I/O map as a formatted hexadecimal string.
Definition: soempp.cc:709
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: soempp.cc:418
uint16_t getSlaveState(ecx_contextt *context, uint16_t slave, bool forceRefresh)
Retrieves the current state of a specific EtherCAT slave.
Definition: soempp.cc:306
int setPdoMappingFromUiConfig(ecx_contextt *context, uint16 slave)
Sets PDO mapping for a specified EtherCAT slave using UI configuration.
Definition: soempp.cc:129
Represents the mapped PDO entries for a slave device.
Definition: common.h:180
Structure representing the Fieldbus configuration and context for EtherCAT communication.
Definition: soempp.h:331
int64 DCtime
Definition: soempp.h:348
ec_slavet slavelist[EC_MAXSLAVE]
Definition: soempp.h:340
boolean ecaterror
Definition: soempp.h:347
ec_PDOdesct PDOdesc[EC_MAX_MAPT]
Definition: soempp.h:351
ec_idxstackT idxstack
Definition: soempp.h:346
ec_groupt grouplist[EC_MAXGROUP]
Definition: soempp.h:342
ec_PDOassignt PDOassign[EC_MAX_MAPT]
Definition: soempp.h:350
ec_eepromFMMUt eepFMMU
Definition: soempp.h:353
ec_eepromSMt eepSM
Definition: soempp.h:352
ecx_portt port
Definition: soempp.h:339
uint8 map[4096]
Definition: soempp.h:338
int slavecount
Definition: soempp.h:341
ecx_contextt context
Definition: soempp.h:332
uint8 esibuf[EC_MAXEEPBUF]
Definition: soempp.h:343
int roundtripTime
Definition: soempp.h:335
uint32 esimap[EC_MAXEEPBITMAP]
Definition: soempp.h:344
uint8 group
Definition: soempp.h:334
ec_eringt elist
Definition: soempp.h:345
std::string iface
Definition: soempp.h:333
ec_SMcommtypet SMcommtype[EC_MAX_MAPT]
Definition: soempp.h:349