Skip to content

Commit

Permalink
fixed broken mock master
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbed authored and yguel committed Dec 20, 2023
1 parent c9bebed commit 33c45fc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MockMaster : public ethercat_interface::EcMaster

bool init(std::string master_interface = "0");

bool add_slave(ethercat_interface::EcSlave * slave);
bool add_slave(std::shared_ptr<ethercat_interface::EcSlave> slave);

bool configure_slaves();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>
#include <unordered_map>
#include <string>
#include <memory>

#include "ethercat_interface/ec_sdo_manager.hpp"
#include "ethercat_interface/ec_slave.hpp"
Expand All @@ -31,33 +32,30 @@ namespace ethercat_master
class MockSlave
{
public:
explicit MockSlave(ethercat_interface::EcSlave * slave);
explicit MockSlave(std::shared_ptr<ethercat_interface::EcSlave> slave);
~MockSlave();
/** read or write data to the domain */
int process_data(size_t index, uint8_t * domain_address);
int process_data(size_t pdo_mapping_index, size_t pdo_channel_index, uint8_t * domain_address);
bool initialized();
void set_state_is_operational(bool value);
/** Assign activate DC synchronization. return activate word*/
int dc_sync();

bool setupSlave(
std::unordered_map<std::string, std::string> slave_paramters,
std::vector<double> * state_interface,
std::vector<double> * command_interface);
uint32_t get_vendor_id();
uint32_t get_product_id();
int get_bus_position();

uint32_t vendor_id;
uint32_t product_id;
int bus_position;
int bus_alias;
ethercat_interface::pdo_config_t get_pdo_config();
ethercat_interface::sm_config_t get_sm_config();
ethercat_interface::sdo_config_t get_sdo_config();

std::vector<ethercat_interface::SdoConfigEntry> sdo_config;

protected:
ethercat_interface::EcSlave * slave_;
std::vector<double> * state_interface_ptr_;
std::vector<double> * command_interface_ptr_;
std::unordered_map<std::string, std::string> paramters_;
bool is_operational_ = false;
std::shared_ptr<ethercat_interface::EcSlave> slave_;
int bus_position_;

bool setup_slave();
};
} // namespace ethercat_master
#endif // ETHERCAT_MASTER__EC_SLAVE_MOCK_HPP_
6 changes: 3 additions & 3 deletions ethercat_master/ethercat_master_mock/src/ec_master_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ bool MockMaster::init(std::string master_interface)
return true;
}

bool MockMaster::add_slave(ethercat_interface::EcSlave * slave)
bool MockMaster::add_slave(std::shared_ptr<ethercat_interface::EcSlave> slave)
{
// configure slave in master
auto mock_slave_ptr = std::make_shared<MockSlave>(slave);
slave_list_.push_back(mock_slave_ptr);
slave_list_.emplace_back();
slave_list_.back() = std::make_shared<MockSlave>(slave);
return true;
}

Expand Down
29 changes: 13 additions & 16 deletions ethercat_master/ethercat_master_mock/src/ec_slave_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@

namespace ethercat_master
{
MockSlave::MockSlave(ethercat_interface::EcSlave * slave)
MockSlave::MockSlave(std::shared_ptr<ethercat_interface::EcSlave> slave)
{
slave_ = slave;
vendor_id = slave->vendor_id;
product_id = slave->product_id;
sdo_config = slave->sdo_config;
setup_slave();
}

MockSlave::~MockSlave()
{
}

int MockSlave::process_data(size_t index, uint8_t * domain_address)
int MockSlave::process_data(
size_t pdo_mapping_index, size_t pdo_channel_index, uint8_t * domain_address)
{
return slave_->process_data(index, domain_address);
slave_->process_data(pdo_mapping_index, pdo_channel_index, domain_address);
return 0;
}

bool MockSlave::initialized()
Expand All @@ -42,24 +42,21 @@ bool MockSlave::initialized()

void MockSlave::set_state_is_operational(bool value)
{
is_operational_ = value;
slave_->set_state_is_operational(value);
}

int MockSlave::dc_sync()
{
return slave_->dc_sync();
}

bool MockSlave::setupSlave(
std::unordered_map<std::string, std::string> slave_paramters,
std::vector<double> * state_interface,
std::vector<double> * command_interface)
bool MockSlave::setup_slave()
{
state_interface_ptr_ = state_interface;
command_interface_ptr_ = command_interface;
paramters_ = slave_paramters;
bus_position = std::stoi(slave_paramters["position"]);
bus_alias = std::stoi(slave_paramters["alias"]);
if (slave_->get_slave_parameters().find("position") != slave_->get_slave_parameters().end()) {
bus_position_ = std::stoi(slave_->get_slave_parameters()["position"]);
} else {
bus_position_ = 0;
}
return true;
}
} // namespace ethercat_master

0 comments on commit 33c45fc

Please sign in to comment.