Skip to content

Commit

Permalink
[MockHardware] Use consequently 'mock' instead of 'fake'. (backport #…
Browse files Browse the repository at this point in the history
…1026) – And deprecate parameters. (#1051)

* Use consequently 'mock' instead of 'fake'. (#1026)

(cherry picked from commit 7174a1d)

# Conflicts:
#	hardware_interface/include/mock_components/generic_system.hpp
#	hardware_interface/src/mock_components/generic_system.cpp
#	hardware_interface/test/mock_components/test_generic_system.cpp

---------

Co-authored-by: Dr. Denis <[email protected]>
  • Loading branch information
mergify[bot] and destogl authored Jun 7, 2023
1 parent 297159d commit 9d5d524
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 50 deletions.
8 changes: 4 additions & 4 deletions hardware_interface/include/mock_components/generic_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ class HARDWARE_INTERFACE_PUBLIC GenericSystem : public hardware_interface::Syste

std::vector<std::string> sensor_interfaces_;
/// The size of this vector is (sensor_interfaces_.size() x nr_joints)
std::vector<std::vector<double>> sensor_fake_commands_;
std::vector<std::vector<double>> sensor_mock_commands_;
std::vector<std::vector<double>> sensor_states_;

std::vector<std::string> gpio_interfaces_;
/// The size of this vector is (gpio_interfaces_.size() x nr_joints)
std::vector<std::vector<double>> gpio_fake_commands_;
std::vector<std::vector<double>> gpio_mock_commands_;
std::vector<std::vector<double>> gpio_commands_;
std::vector<std::vector<double>> gpio_states_;

Expand All @@ -108,8 +108,8 @@ class HARDWARE_INTERFACE_PUBLIC GenericSystem : public hardware_interface::Syste
std::vector<std::string> & interfaces, std::vector<std::vector<double>> & storage,
std::vector<InterfaceType> & target_interfaces, bool using_state_interfaces);

bool use_fake_gpio_command_interfaces_;
bool use_fake_sensor_command_interfaces_;
bool use_mock_gpio_command_interfaces_;
bool use_mock_sensor_command_interfaces_;

double position_state_following_offset_;
std::string custom_interface_with_following_offset_;
Expand Down
74 changes: 50 additions & 24 deletions hardware_interface/src/mock_components/generic_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,52 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
}
};

// check if to create fake command interface for sensor
auto it = info_.hardware_parameters.find("fake_sensor_commands");
// check if to create mock command interface for sensor
auto it = info_.hardware_parameters.find("mock_sensor_commands");
if (it != info_.hardware_parameters.end())
{
use_fake_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second);
use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second);
}
else
{
use_fake_sensor_command_interfaces_ = false;
// check if fake_sensor_commands was set instead and issue warning.
it = info_.hardware_parameters.find("fake_sensor_commands");
if (it != info_.hardware_parameters.end())
{
use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second);
RCUTILS_LOG_WARN_NAMED(
"mock_generic_system",
"Parameter 'fake_sensor_commands' has been deprecated from usage. Use"
"'mock_sensor_commands' instead.");
}
else
{
use_mock_sensor_command_interfaces_ = false;
}
}

// check if to create fake command interface for gpio
it = info_.hardware_parameters.find("fake_gpio_commands");
// check if to create mock command interface for gpio
it = info_.hardware_parameters.find("mock_gpio_commands");
if (it != info_.hardware_parameters.end())
{
use_fake_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second);
use_mock_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second);
}
else
{
use_fake_gpio_command_interfaces_ = false;
// check if fake_gpio_commands was set instead and issue warning
it = info_.hardware_parameters.find("fake_gpio_commands");
if (it != info_.hardware_parameters.end())
{
use_mock_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second);
RCUTILS_LOG_WARN_NAMED(
"mock_generic_system",
"Parameter 'fake_gpio_commands' has been deprecated from usage. Use"
"'mock_gpio_commands' instead.");
}
else
{
use_mock_gpio_command_interfaces_ = false;
}
}

// process parameters about state following
Expand Down Expand Up @@ -162,14 +188,14 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
index_custom_interface_with_following_offset_ =
std::distance(other_interfaces_.begin(), if_it);
RCUTILS_LOG_INFO_NAMED(
"fake_generic_system", "Custom interface with following offset '%s' found at index: %zu.",
"mock_generic_system", "Custom interface with following offset '%s' found at index: %zu.",
custom_interface_with_following_offset_.c_str(),
index_custom_interface_with_following_offset_);
}
else
{
RCUTILS_LOG_WARN_NAMED(
"fake_generic_system",
"mock_generic_system",
"Custom interface with following offset '%s' does not exist. Offset will not be applied",
custom_interface_with_following_offset_.c_str());
}
Expand All @@ -188,7 +214,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
}
}
initialize_storage_vectors(
sensor_fake_commands_, sensor_states_, sensor_interfaces_, info_.sensors);
sensor_mock_commands_, sensor_states_, sensor_interfaces_, info_.sensors);

// search for gpio interfaces
for (const auto & gpio : info_.gpios)
Expand All @@ -200,10 +226,10 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
populate_non_standard_interfaces(gpio.state_interfaces, gpio_interfaces_);
}

// Fake gpio command interfaces
if (use_fake_gpio_command_interfaces_)
// Mock gpio command interfaces
if (use_mock_gpio_command_interfaces_)
{
initialize_storage_vectors(gpio_fake_commands_, gpio_states_, gpio_interfaces_, info_.gpios);
initialize_storage_vectors(gpio_mock_commands_, gpio_states_, gpio_interfaces_, info_.gpios);
}
// Real gpio command interfaces
else
Expand Down Expand Up @@ -283,22 +309,22 @@ std::vector<hardware_interface::CommandInterface> GenericSystem::export_command_
}
}

// Fake sensor command interfaces
if (use_fake_sensor_command_interfaces_)
// Mock sensor command interfaces
if (use_mock_sensor_command_interfaces_)
{
if (!populate_interfaces(
info_.sensors, sensor_interfaces_, sensor_fake_commands_, command_interfaces, true))
info_.sensors, sensor_interfaces_, sensor_mock_commands_, command_interfaces, true))
{
throw std::runtime_error(
"Interface is not found in the standard nor other list. This should never happen!");
}
}

// Fake gpio command interfaces (consider all state interfaces for command interfaces)
if (use_fake_gpio_command_interfaces_)
// Mock gpio command interfaces (consider all state interfaces for command interfaces)
if (use_mock_gpio_command_interfaces_)
{
if (!populate_interfaces(
info_.gpios, gpio_interfaces_, gpio_fake_commands_, command_interfaces, true))
info_.gpios, gpio_interfaces_, gpio_mock_commands_, command_interfaces, true))
{
throw std::runtime_error(
"Interface is not found in the gpio list. This should never happen!");
Expand Down Expand Up @@ -375,15 +401,15 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
}
}

if (use_fake_sensor_command_interfaces_)
if (use_mock_sensor_command_interfaces_)
{
mirror_command_to_state(sensor_states_, sensor_fake_commands_);
mirror_command_to_state(sensor_states_, sensor_mock_commands_);
}

// do loopback on all gpio interfaces
if (use_fake_gpio_command_interfaces_)
if (use_mock_gpio_command_interfaces_)
{
mirror_command_to_state(gpio_states_, gpio_fake_commands_);
mirror_command_to_state(gpio_states_, gpio_mock_commands_);
}
else
{
Expand Down
Loading

0 comments on commit 9d5d524

Please sign in to comment.