Skip to content

Commit

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

* Use consequently 'mock' instead of 'fake'. (ros-controls#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
2 people authored and flochre committed Jul 5, 2023
1 parent a20ebe5 commit 5a59847
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 38 deletions.
8 changes: 6 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,7 +108,11 @@ 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_mock_gpio_command_interfaces_;
>>>>>>> [MockHardware] Use consequently 'mock' instead of 'fake'. (backport #1026) – And deprecate parameters. (#1051)
bool use_mock_sensor_command_interfaces_;

double position_state_following_offset_;
Expand Down
49 changes: 31 additions & 18 deletions hardware_interface/src/mock_components/generic_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i
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
{
// 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_ = it->second == "true" || it->second == "True";
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"
Expand All @@ -82,15 +82,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 @@ -201,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 @@ -213,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 @@ -296,22 +309,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 @@ -390,13 +403,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
127 changes: 109 additions & 18 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,11 @@ const auto PERIOD = rclcpp::Duration::from_seconds(0.01);

class TestGenericSystem : public ::testing::Test
{
public:
void test_generic_system_with_mimic_joint(const std::string & urdf);
void test_generic_system_with_mock_sensor_commands(const std::string & urdf);
void test_generic_system_with_mock_gpio_commands(const std::string & urdf);

protected:
void SetUp() override
{
Expand Down Expand Up @@ -181,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 @@ -210,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 @@ -377,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 @@ -453,18 +492,51 @@ 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_;
};

// 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)
{
}
};

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 @@ -912,7 +984,11 @@ TEST_F(TestGenericSystem, generic_system_2dof_sensor)
ASSERT_EQ(0.33, j2p_c.get_value());
}

<<<<<<< HEAD
void test_generic_system_with_mock_sensor_commands(std::string urdf)
=======
void TestGenericSystem::test_generic_system_with_mock_sensor_commands(const std::string & urdf)
>>>>>>> [MockHardware] Use consequently 'mock' instead of 'fake'. (backport #1026) – And deprecate parameters. (#1051)
{
hardware_interface::ResourceManager rm(urdf);
// Activate components to get all interfaces available
Expand Down Expand Up @@ -1034,24 +1110,24 @@ void test_generic_system_with_mock_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_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);
}

void test_generic_system_with_mimic_joint(std::string urdf)
void TestGenericSystem::test_generic_system_with_mimic_joint(const std::string & urdf)
{
hardware_interface::ResourceManager rm(urdf);
// Activate components to get all interfaces available
Expand Down Expand Up @@ -1360,12 +1436,9 @@ 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(const 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;
hardware_interface::ResourceManager rm(urdf);
TestableResourceManager rm(urdf);

// check is hardware is started
auto status_map = rm.get_components_status();
Expand Down Expand Up @@ -1471,7 +1544,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 +1590,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 5a59847

Please sign in to comment.