Skip to content

Commit 9a9c8a9

Browse files
Cleanup GenericSystem component code 🧹 (#2706)
1 parent 8ad5ebb commit 9a9c8a9

File tree

3 files changed

+42
-92
lines changed

3 files changed

+42
-92
lines changed

hardware_interface/include/mock_components/generic_system.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ class GenericSystem : public hardware_interface::SystemInterface
8282
hardware_interface::HW_IF_ACCELERATION, hardware_interface::HW_IF_EFFORT};
8383
std::vector<std::string> skip_interfaces_;
8484

85-
std::vector<std::string> other_interfaces_;
86-
std::vector<std::string> sensor_interfaces_;
87-
std::vector<std::string> gpio_mock_interfaces_;
88-
8985
private:
9086
bool populate_interfaces(
9187
const std::vector<hardware_interface::ComponentInfo> & components,

hardware_interface/src/mock_components/generic_system.cpp

Lines changed: 40 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,6 @@ CallbackReturn GenericSystem::on_init(
3939
return CallbackReturn::ERROR;
4040
}
4141

42-
auto populate_non_standard_interfaces =
43-
[this](auto interface_list, auto & non_standard_interfaces)
44-
{
45-
for (const auto & interface : interface_list)
46-
{
47-
// add to list if non-standard interface
48-
if (
49-
std::find(standard_interfaces_.begin(), standard_interfaces_.end(), interface.name) ==
50-
standard_interfaces_.end())
51-
{
52-
if (
53-
std::find(
54-
non_standard_interfaces.begin(), non_standard_interfaces.end(), interface.name) ==
55-
non_standard_interfaces.end())
56-
{
57-
non_standard_interfaces.emplace_back(interface.name);
58-
}
59-
}
60-
}
61-
};
62-
6342
// check if to create mock command interface for sensor
6443
auto it = get_hardware_info().hardware_parameters.find("mock_sensor_commands");
6544
if (it != get_hardware_info().hardware_parameters.end())
@@ -128,21 +107,44 @@ CallbackReturn GenericSystem::on_init(
128107
}
129108

130109
// search for non-standard joint interfaces
110+
std::vector<std::string> other_interfaces;
111+
auto populate_non_standard_interfaces =
112+
[this](auto interface_list, auto & non_standard_interfaces)
113+
{
114+
for (const auto & interface : interface_list)
115+
{
116+
// add to list if non-standard interface
117+
if (
118+
std::find(standard_interfaces_.begin(), standard_interfaces_.end(), interface.name) ==
119+
standard_interfaces_.end())
120+
{
121+
// and does not exist yet
122+
if (
123+
std::find(
124+
non_standard_interfaces.begin(), non_standard_interfaces.end(), interface.name) ==
125+
non_standard_interfaces.end())
126+
{
127+
non_standard_interfaces.emplace_back(interface.name);
128+
}
129+
}
130+
}
131+
};
131132
for (const auto & joint : get_hardware_info().joints)
132133
{
133-
// populate non-standard command interfaces to other_interfaces_
134-
populate_non_standard_interfaces(joint.command_interfaces, other_interfaces_);
134+
// populate non-standard command interfaces to other_interfaces
135+
populate_non_standard_interfaces(joint.command_interfaces, other_interfaces);
135136

136-
// populate non-standard state interfaces to other_interfaces_
137-
populate_non_standard_interfaces(joint.state_interfaces, other_interfaces_);
137+
// populate non-standard state interfaces to other_interfaces
138+
populate_non_standard_interfaces(joint.state_interfaces, other_interfaces);
138139
}
139140

140141
// when following offset is used on custom interface then find its index
141142
if (!custom_interface_with_following_offset_.empty())
142143
{
143-
auto if_it = std::find(
144-
other_interfaces_.begin(), other_interfaces_.end(), custom_interface_with_following_offset_);
145-
if (if_it != other_interfaces_.end())
144+
if (
145+
std::find(
146+
other_interfaces.begin(), other_interfaces.end(),
147+
custom_interface_with_following_offset_) != other_interfaces.end())
146148
{
147149
RCLCPP_INFO(
148150
get_logger(), "Custom interface with following offset '%s' found.",
@@ -157,19 +159,6 @@ CallbackReturn GenericSystem::on_init(
157159
}
158160
}
159161

160-
for (const auto & sensor : get_hardware_info().sensors)
161-
{
162-
for (const auto & interface : sensor.state_interfaces)
163-
{
164-
if (
165-
std::find(sensor_interfaces_.begin(), sensor_interfaces_.end(), interface.name) ==
166-
sensor_interfaces_.end())
167-
{
168-
sensor_interfaces_.emplace_back(interface.name);
169-
}
170-
}
171-
}
172-
173162
return CallbackReturn::SUCCESS;
174163
}
175164

@@ -231,26 +220,10 @@ return_type GenericSystem::prepare_command_mode_switch(
231220
}
232221
if (key == info.joints[joint_index].name + "/" + hardware_interface::HW_IF_VELOCITY)
233222
{
234-
if (!calculate_dynamics_)
235-
{
236-
RCLCPP_WARN(
237-
get_logger(),
238-
"Requested velocity mode for joint '%s' without dynamics calculation enabled - this "
239-
"might lead to wrong feedback and unexpected behavior.",
240-
info.joints[joint_index].name.c_str());
241-
}
242223
joint_found_in_x_requests_[joint_index] += 1;
243224
}
244225
if (key == info.joints[joint_index].name + "/" + hardware_interface::HW_IF_ACCELERATION)
245226
{
246-
if (!calculate_dynamics_)
247-
{
248-
RCLCPP_WARN(
249-
get_logger(),
250-
"Requested acceleration mode for joint '%s' without dynamics calculation enabled - "
251-
"this might lead to wrong feedback and unexpected behavior.",
252-
info.joints[joint_index].name.c_str());
253-
}
254227
joint_found_in_x_requests_[joint_index] += 1;
255228
}
256229
}
@@ -412,14 +385,13 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
412385
for (size_t i = 0; i < 3; ++i)
413386
{
414387
const auto full_name = joint_name + "/" + standard_interfaces_[i];
415-
if (joint_command_interfaces_.find(full_name) != joint_command_interfaces_.end())
388+
if (has_command(full_name))
416389
{
417-
joint_command_values_[i] =
418-
get_command(joint_command_interfaces_.at(full_name).get_name());
390+
joint_command_values_[i] = get_command(full_name);
419391
}
420-
if (joint_state_interfaces_.find(full_name) != joint_state_interfaces_.end())
392+
if (has_state(full_name))
421393
{
422-
joint_state_values_[i] = get_state(joint_state_interfaces_.at(full_name).get_name());
394+
joint_state_values_[i] = get_state(full_name);
423395
}
424396
}
425397

@@ -513,7 +485,7 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
513485
if (joint_command.get()->get_interface_name() == hardware_interface::HW_IF_POSITION)
514486
{
515487
const std::string & name = joint_command.get()->get_name();
516-
if (joint_state_interfaces_.find(name) != joint_state_interfaces_.end())
488+
if (has_state(name))
517489
{
518490
set_state(
519491
name, get_command(name) + (custom_interface_with_following_offset_.empty()
@@ -536,7 +508,7 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
536508
continue;
537509
}
538510
const std::string & full_interface_name = joint_state.get()->get_name();
539-
if (joint_command_interfaces_.find(full_interface_name) != joint_command_interfaces_.end())
511+
if (has_command(full_interface_name))
540512
{
541513
if (
542514
mirror_command_to_state(full_interface_name, joint_state.get()->get_data_type()) !=
@@ -561,29 +533,22 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
561533
{
562534
const auto & mimic_joint_name = joints.at(mimic_joint.joint_index).name;
563535
const auto & mimicked_joint_name = joints.at(mimic_joint.mimicked_joint_index).name;
564-
if (
565-
joint_state_interfaces_.find(mimic_joint_name + "/" + hardware_interface::HW_IF_POSITION) !=
566-
joint_state_interfaces_.end())
536+
if (has_state(mimic_joint_name + "/" + hardware_interface::HW_IF_POSITION))
567537
{
568538
set_state(
569539
mimic_joint_name + "/" + hardware_interface::HW_IF_POSITION,
570540
mimic_joint.offset +
571541
mimic_joint.multiplier *
572542
get_state(mimicked_joint_name + "/" + hardware_interface::HW_IF_POSITION));
573543
}
574-
if (
575-
joint_state_interfaces_.find(mimic_joint_name + "/" + hardware_interface::HW_IF_VELOCITY) !=
576-
joint_state_interfaces_.end())
544+
if (has_state(mimic_joint_name + "/" + hardware_interface::HW_IF_VELOCITY))
577545
{
578546
set_state(
579547
mimic_joint_name + "/" + hardware_interface::HW_IF_VELOCITY,
580548
mimic_joint.multiplier *
581549
get_state(mimicked_joint_name + "/" + hardware_interface::HW_IF_VELOCITY));
582550
}
583-
if (
584-
joint_state_interfaces_.find(
585-
mimic_joint_name + "/" + hardware_interface::HW_IF_ACCELERATION) !=
586-
joint_state_interfaces_.end())
551+
if (has_state(mimic_joint_name + "/" + hardware_interface::HW_IF_ACCELERATION))
587552
{
588553
set_state(
589554
mimic_joint_name + "/" + hardware_interface::HW_IF_ACCELERATION,
@@ -624,7 +589,7 @@ return_type GenericSystem::read(const rclcpp::Time & /*time*/, const rclcpp::Dur
624589
for (const auto & gpio_command : gpio_commands_)
625590
{
626591
const std::string & name = gpio_command.get()->get_name();
627-
if (gpio_state_interfaces_.find(name) != gpio_state_interfaces_.end())
592+
if (has_state(name))
628593
{
629594
if (mirror_command_to_state(name, gpio_command.get()->get_data_type()) != return_type::OK)
630595
{

hardware_interface/test/mock_components/test_generic_system.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ class TestGenericSystem : public ::testing::Test
808808
hardware_interface::DEFAULT_REGISTRY_KEY);
809809
}
810810

811+
void TearDown() override { node_.reset(); }
812+
811813
std::string hardware_system_2dof_;
812814
std::string hardware_system_2dof_asymetric_;
813815
std::string hardware_system_2dof_standard_interfaces_;
@@ -847,19 +849,6 @@ class ResourceStorage;
847849
class TestableResourceManager : public hardware_interface::ResourceManager
848850
{
849851
public:
850-
friend TestGenericSystem;
851-
852-
FRIEND_TEST(TestGenericSystem, generic_system_2dof_symetric_interfaces);
853-
FRIEND_TEST(TestGenericSystem, generic_system_2dof_asymetric_interfaces);
854-
FRIEND_TEST(TestGenericSystem, generic_system_2dof_other_interfaces);
855-
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor);
856-
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_mock_command);
857-
FRIEND_TEST(TestGenericSystem, generic_system_2dof_sensor_mock_command_True);
858-
FRIEND_TEST(TestGenericSystem, hardware_system_2dof_with_mimic_joint);
859-
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio);
860-
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command);
861-
FRIEND_TEST(TestGenericSystem, valid_urdf_ros2_control_system_robot_with_gpio_mock_command_True);
862-
863852
explicit TestableResourceManager(rclcpp::Node::SharedPtr node)
864853
: hardware_interface::ResourceManager(
865854
node->get_node_clock_interface(), node->get_node_logging_interface())

0 commit comments

Comments
 (0)