Skip to content

Commit

Permalink
Use consequently 'mock' instead of 'fake'. (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
destogl authored May 19, 2023
1 parent 8d47dcc commit 7174a1d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 43 deletions.
6 changes: 3 additions & 3 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,7 +108,7 @@ 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_mock_gpio_command_interfaces_;
bool use_mock_sensor_command_interfaces_;

double position_state_following_offset_;
Expand Down
51 changes: 32 additions & 19 deletions hardware_interface/src/mock_components/generic_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
{
use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second);
RCUTILS_LOG_WARN_NAMED(
"fake_generic_system",
"mock_generic_system",
"Parameter 'fake_sensor_commands' has been deprecated from usage. Use"
"'mock_sensor_commands' instead.");
}
Expand All @@ -95,15 +95,28 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
}
}

// 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 @@ -188,14 +201,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 @@ -214,7 +227,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 @@ -226,10 +239,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 @@ -309,22 +322,22 @@ std::vector<hardware_interface::CommandInterface> GenericSystem::export_command_
}
}

// 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 @@ -403,13 +416,13 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur

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
93 changes: 72 additions & 21 deletions hardware_interface/test/mock_components/test_generic_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ const auto PERIOD = rclcpp::Duration::from_seconds(0.01);
class TestGenericSystem : public ::testing::Test
{
public:
void test_generic_system_with_mock_sensor_commands(std::string & urdf);
void test_generic_system_with_mimic_joint(std::string & urdf);
void test_generic_system_with_mock_sensor_commands(std::string & urdf);
void test_generic_system_with_mock_gpio_commands(std::string & urdf);

protected:
void SetUp() override
Expand Down Expand Up @@ -185,7 +186,7 @@ class TestGenericSystem : public ::testing::Test
</ros2_control>
)";

hardware_system_2dof_with_sensor_fake_command_ =
hardware_system_2dof_with_sensor_mock_command_ =
R"(
<ros2_control name="GenericSystem2dof" type="system">
<hardware>
Expand Down Expand Up @@ -214,7 +215,7 @@ class TestGenericSystem : public ::testing::Test
</ros2_control>
)";

hardware_system_2dof_with_sensor_fake_command_True_ =
hardware_system_2dof_with_sensor_mock_command_True_ =
R"(
<ros2_control name="GenericSystem2dof" type="system">
<hardware>
Expand Down Expand Up @@ -381,7 +382,41 @@ class TestGenericSystem : public ::testing::Test
</ros2_control>
)";

valid_urdf_ros2_control_system_robot_with_gpio_fake_command_ =
valid_urdf_ros2_control_system_robot_with_gpio_mock_command_ =
R"(
<ros2_control name="GenericSystem2dof" type="system">
<hardware>
<plugin>mock_components/GenericSystem</plugin>
<param name="mock_gpio_commands">true</param>
</hardware>
<joint name="joint1">
<command_interface name="position"/>
<command_interface name="velocity"/>
<state_interface name="position"/>
<state_interface name="velocity"/>
<param name="initial_position">3.45</param>
</joint>
<joint name="joint2">
<command_interface name="position"/>
<command_interface name="velocity"/>
<state_interface name="position"/>
<state_interface name="velocity"/>
<param name="initial_position">2.78</param>
</joint>
<gpio name="flange_analog_IOs">
<command_interface name="analog_output1" data_type="double"/>
<state_interface name="analog_output1"/>
<state_interface name="analog_input1"/>
<state_interface name="analog_input2"/>
</gpio>
<gpio name="flange_vacuum">
<command_interface name="vacuum"/>
<state_interface name="vacuum" data_type="double"/>
</gpio>
</ros2_control>
)";

valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True_ =
R"(
<ros2_control name="GenericSystem2dof" type="system">
<hardware>
Expand Down Expand Up @@ -457,14 +492,15 @@ class TestGenericSystem : public ::testing::Test
std::string hardware_system_2dof_standard_interfaces_;
std::string hardware_system_2dof_with_other_interface_;
std::string hardware_system_2dof_with_sensor_;
std::string hardware_system_2dof_with_sensor_fake_command_;
std::string hardware_system_2dof_with_sensor_fake_command_True_;
std::string hardware_system_2dof_with_sensor_mock_command_;
std::string hardware_system_2dof_with_sensor_mock_command_True_;
std::string hardware_system_2dof_with_mimic_joint_;
std::string hardware_system_2dof_standard_interfaces_with_offset_;
std::string hardware_system_2dof_standard_interfaces_with_custom_interface_for_offset_;
std::string hardware_system_2dof_standard_interfaces_with_custom_interface_for_offset_missing_;
std::string valid_urdf_ros2_control_system_robot_with_gpio_;
std::string valid_urdf_ros2_control_system_robot_with_gpio_fake_command_;
std::string valid_urdf_ros2_control_system_robot_with_gpio_mock_command_;
std::string valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True_;
std::string sensor_with_initial_value_;
std::string gpio_with_initial_value_;
};
Expand All @@ -485,12 +521,12 @@ class TestableResourceManager : public hardware_interface::ResourceManager
FRIEND_TEST(TestGenericSystem, generic_system_2dof_asymetric_interfaces);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_other_interfaces);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_fake_command);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_fake_command_True);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_mock_command);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_mock_command_True);
FRIEND_TEST(TestGenericSystem, hardware_system_2dof_with_mimic_joint);
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio);
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command);
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command);
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command);
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True);

TestableResourceManager() : hardware_interface::ResourceManager() {}

Expand Down Expand Up @@ -1070,18 +1106,18 @@ void TestGenericSystem::test_generic_system_with_mock_sensor_commands(std::strin
ASSERT_EQ(4.44, sty_c.get_value());
}

TEST_F(TestGenericSystem, generic_system_2dof_sensor_fake_command)
TEST_F(TestGenericSystem, generic_system_2dof_sensor_mock_command)
{
auto urdf = ros2_control_test_assets::urdf_head + hardware_system_2dof_with_sensor_fake_command_ +
auto urdf = ros2_control_test_assets::urdf_head + hardware_system_2dof_with_sensor_mock_command_ +
ros2_control_test_assets::urdf_tail;

test_generic_system_with_mock_sensor_commands(urdf);
}

TEST_F(TestGenericSystem, generic_system_2dof_sensor_fake_command_True)
TEST_F(TestGenericSystem, generic_system_2dof_sensor_mock_command_True)
{
auto urdf = ros2_control_test_assets::urdf_head +
hardware_system_2dof_with_sensor_fake_command_True_ +
hardware_system_2dof_with_sensor_mock_command_True_ +
ros2_control_test_assets::urdf_tail;

test_generic_system_with_mock_sensor_commands(urdf);
Expand Down Expand Up @@ -1396,11 +1432,8 @@ TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio)
generic_system_functional_test(urdf);
}

TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command)
void TestGenericSystem::test_generic_system_with_mock_gpio_commands(std::string & urdf)
{
auto urdf = ros2_control_test_assets::urdf_head +
valid_urdf_ros2_control_system_robot_with_gpio_fake_command_ +
ros2_control_test_assets::urdf_tail;
TestableResourceManager rm(urdf);

// check is hardware is started
Expand Down Expand Up @@ -1507,7 +1540,25 @@ TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_co
ASSERT_EQ(2.22, gpio2_vac_c.get_value());
}

TEST_F(TestGenericSystem, sensor_with_initial_value_)
TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command)
{
auto urdf = ros2_control_test_assets::urdf_head +
valid_urdf_ros2_control_system_robot_with_gpio_mock_command_ +
ros2_control_test_assets::urdf_tail;

test_generic_system_with_mock_gpio_commands(urdf);
}

TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True)
{
auto urdf = ros2_control_test_assets::urdf_head +
valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True_ +
ros2_control_test_assets::urdf_tail;

test_generic_system_with_mock_gpio_commands(urdf);
}

TEST_F(TestGenericSystem, sensor_with_initial_value)
{
auto urdf = ros2_control_test_assets::urdf_head + sensor_with_initial_value_ +
ros2_control_test_assets::urdf_tail;
Expand Down Expand Up @@ -1535,7 +1586,7 @@ TEST_F(TestGenericSystem, sensor_with_initial_value_)
ASSERT_EQ(0.0, force_z_s.get_value());
}

TEST_F(TestGenericSystem, gpio_with_initial_value_)
TEST_F(TestGenericSystem, gpio_with_initial_value)
{
auto urdf = ros2_control_test_assets::urdf_head + gpio_with_initial_value_ +
ros2_control_test_assets::urdf_tail;
Expand Down

0 comments on commit 7174a1d

Please sign in to comment.