Skip to content

Commit

Permalink
Use consequently 'mock' instead of 'fake'. (#1026)
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
destogl authored and mergify[bot] committed Jun 5, 2023
1 parent 297159d commit b478fe5
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 31 deletions.
9 changes: 7 additions & 2 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,13 @@ 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);

<<<<<<< HEAD
bool use_fake_gpio_command_interfaces_;
bool use_fake_sensor_command_interfaces_;
=======
bool use_mock_gpio_command_interfaces_;
bool use_mock_sensor_command_interfaces_;
>>>>>>> 7174a1d (Use consequently 'mock' instead of 'fake'. (#1026))

double position_state_following_offset_;
std::string custom_interface_with_following_offset_;
Expand Down
69 changes: 52 additions & 17 deletions hardware_interface/src/mock_components/generic_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,48 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
}
else
{
<<<<<<< HEAD
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;
}
>>>>>>> 7174a1d (Use consequently 'mock' instead of 'fake'. (#1026))
}

// 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 +192,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 +218,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 +230,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 +313,27 @@ std::vector<hardware_interface::CommandInterface> GenericSystem::export_command_
}
}

<<<<<<< HEAD
// Fake sensor command interfaces
if (use_fake_sensor_command_interfaces_)
=======
// Mock sensor command interfaces
if (use_mock_sensor_command_interfaces_)
>>>>>>> 7174a1d (Use consequently 'mock' instead of 'fake'. (#1026))
{
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 @@ -377,13 +412,13 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur

if (use_fake_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
126 changes: 114 additions & 12 deletions hardware_interface/test/mock_components/test_generic_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ const auto PERIOD = rclcpp::Duration::from_seconds(0.01);

class TestGenericSystem : public ::testing::Test
{
<<<<<<< HEAD
=======
public:
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);

>>>>>>> 7174a1d (Use consequently 'mock' instead of 'fake'. (#1026))
protected:
void SetUp() override
{
Expand Down Expand Up @@ -181,7 +189,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 @@ -210,7 +218,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 @@ -377,7 +385,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 @@ -453,18 +495,54 @@ 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_;
};

<<<<<<< HEAD
=======
// Forward declaration
namespace hardware_interface
{
class ResourceStorage;
}

class TestableResourceManager : public hardware_interface::ResourceManager
{
public:
friend TestGenericSystem;

FRIEND_TEST(TestGenericSystem, generic_fake_system_2dof_symetric_interfaces);
FRIEND_TEST(TestGenericSystem, generic_system_2dof_symetric_interfaces);
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_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_mock_command);
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True);

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

TestableResourceManager(
const std::string & urdf, bool validate_interfaces = true, bool activate_all = false)
: hardware_interface::ResourceManager(urdf, validate_interfaces, activate_all)
{
}
};

>>>>>>> 7174a1d (Use consequently 'mock' instead of 'fake'. (#1026))
void set_components_state(
hardware_interface::ResourceManager & rm, const std::vector<std::string> & components,
const uint8_t state_id, const std::string & state_name)
Expand Down Expand Up @@ -1034,18 +1112,18 @@ void test_generic_system_with_fake_sensor_commands(std::string urdf)
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_fake_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_fake_sensor_commands(urdf);
Expand Down Expand Up @@ -1360,12 +1438,18 @@ TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_)
generic_system_functional_test(urdf);
}

<<<<<<< HEAD
TEST_F(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_fake_command_)
{
auto urdf = ros2_control_test_assets::urdf_head +
valid_urdf_ros2_control_system_robot_with_gpio_fake_command_ +
ros2_control_test_assets::urdf_tail;
hardware_interface::ResourceManager rm(urdf);
=======
void TestGenericSystem::test_generic_system_with_mock_gpio_commands(std::string & urdf)
{
TestableResourceManager rm(urdf);
>>>>>>> 7174a1d (Use consequently 'mock' instead of 'fake'. (#1026))

// check is hardware is started
auto status_map = rm.get_components_status();
Expand Down Expand Up @@ -1471,7 +1555,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 @@ -1499,7 +1601,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 b478fe5

Please sign in to comment.