Skip to content

Commit

Permalink
make states and commands of component interfaces private
Browse files Browse the repository at this point in the history
  • Loading branch information
mamueluth committed Apr 10, 2024
1 parent 1dd3026 commit 8dec983
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod

/**
* Override this method to export custom StateInterfaces which are not defined in the URDF file.
* Those interfaces will be added to the unlisted_state_interfaces_ map.
*
* Note method name is going to be changed to export_state_interfaces() as soon as the deprecated
* version is removed.
*
* \return vector of shared pointers to the created and stored StateInterfaces
* \return vector of descriptions to the unlisted StateInterfaces
*/
virtual std::vector<std::shared_ptr<StateInterface>> export_state_interfaces_2()
virtual std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2()
{
// return empty vector by default.
return {};
Expand All @@ -181,8 +182,24 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
*/
virtual std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
{
std::vector<std::shared_ptr<StateInterface>> state_interfaces = export_state_interfaces_2();
state_interfaces.reserve(joint_state_interfaces_.size());
// import the unlisted interfaces
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
export_state_interfaces_2();

std::vector<std::shared_ptr<StateInterface>> state_interfaces;
state_interfaces.reserve(
unlisted_interface_descriptions.size() + joint_state_interfaces_.size());

// add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
// maps.
for (const auto & description : unlisted_interface_descriptions)
{
auto name = description.get_name();
unlisted_state_interfaces_.insert(std::make_pair(name, description));
auto state_interface = std::make_shared<StateInterface>(description);
actuator_states_.insert(std::make_pair(name, state_interface));
state_interfaces.push_back(state_interface);
}

for (const auto & [name, descr] : joint_state_interfaces_)
{
Expand Down Expand Up @@ -219,13 +236,14 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod

/**
* Override this method to export custom CommandInterfaces which are not defined in the URDF file.
* Those interfaces will be added to the unlisted_command_interfaces_ map.
*
* Note method name is going to be changed to export_command_interfaces() as soon as the
* deprecated version is removed.
*
* \return vector of shared pointers to the created and stored StateInterfaces
* \return vector of descriptions to the unlisted CommandInterfaces
*/
virtual std::vector<std::shared_ptr<CommandInterface>> export_command_interfaces_2()
virtual std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2()
{
// return empty vector by default.
return {};
Expand All @@ -240,9 +258,24 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
*/
virtual std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces()
{
std::vector<std::shared_ptr<CommandInterface>> command_interfaces =
// import the unlisted interfaces
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
export_command_interfaces_2();
command_interfaces.reserve(joint_command_interfaces_.size());

std::vector<std::shared_ptr<CommandInterface>> command_interfaces;
command_interfaces.reserve(
unlisted_interface_descriptions.size() + joint_command_interfaces_.size());

// add InterfaceDescriptions and create the CommandInterfaces from the descriptions and add to
// maps.
for (const auto & description : unlisted_interface_descriptions)
{
auto name = description.get_name();
unlisted_command_interfaces_.insert(std::make_pair(name, description));
auto command_interface = std::make_shared<CommandInterface>(description);
actuator_commands_.insert(std::make_pair(name, command_interface));
command_interfaces.push_back(command_interface);
}

for (const auto & [name, descr] : joint_command_interfaces_)
{
Expand Down Expand Up @@ -358,10 +391,14 @@ class ActuatorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNod
std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;

std::unordered_map<std::string, std::shared_ptr<StateInterface>> actuator_states_;
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> actuator_commands_;
std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;

rclcpp_lifecycle::State lifecycle_state_;

private:
std::unordered_map<std::string, std::shared_ptr<StateInterface>> actuator_states_;
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> actuator_commands_;
};

} // namespace hardware_interface
Expand Down
31 changes: 25 additions & 6 deletions hardware_interface/include/hardware_interface/sensor_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI

/**
* Override this method to export custom StateInterfaces which are not defined in the URDF file.
* Those interfaces will be added to the unlisted_state_interfaces_ map.
*
* Note method name is going to be changed to export_state_interfaces() as soon as the deprecated
* version is removed.
*
* \return vector of shared pointers to the created and stored StateInterfaces
* \return vector of descriptions to the unlisted StateInterfaces
*/
virtual std::vector<std::shared_ptr<StateInterface>> export_state_interfaces_2()
virtual std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2()
{
// return empty vector by default.
return {};
Expand All @@ -165,8 +166,24 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
*/
virtual std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
{
std::vector<std::shared_ptr<StateInterface>> state_interfaces = export_state_interfaces_2();
state_interfaces.reserve(sensor_state_interfaces_.size());
// import the unlisted interfaces
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
export_state_interfaces_2();

std::vector<std::shared_ptr<StateInterface>> state_interfaces;
state_interfaces.reserve(
unlisted_interface_descriptions.size() + sensor_state_interfaces_.size());

// add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
// maps.
for (const auto & description : unlisted_interface_descriptions)
{
auto name = description.get_name();
unlisted_state_interfaces_.insert(std::make_pair(name, description));
auto state_interface = std::make_shared<StateInterface>(description);
sensor_states_.insert(std::make_pair(name, state_interface));
state_interfaces.push_back(state_interface);
}

for (const auto & [name, descr] : sensor_state_interfaces_)
{
Expand Down Expand Up @@ -224,10 +241,12 @@ class SensorInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
HardwareInfo info_;

std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;

std::unordered_map<std::string, std::shared_ptr<StateInterface>> sensor_states_;
std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;

rclcpp_lifecycle::State lifecycle_state_;

private:
std::unordered_map<std::string, std::shared_ptr<StateInterface>> sensor_states_;
};

} // namespace hardware_interface
Expand Down
59 changes: 48 additions & 11 deletions hardware_interface/include/hardware_interface/system_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI

/**
* Override this method to export custom StateInterfaces which are not defined in the URDF file.
* Those interfaces will be added to the unlisted_state_interfaces_ map.
*
* Note method name is going to be changed to export_state_interfaces() as soon as the deprecated
* version is removed.
*
* \return vector of shared pointers to the created and stored StateInterfaces
* \return vector of descriptions to the unlisted StateInterfaces
*/
virtual std::vector<std::shared_ptr<StateInterface>> export_state_interfaces_2()
virtual std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2()
{
// return empty vector by default.
return {};
Expand All @@ -200,10 +201,25 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
*/
std::vector<std::shared_ptr<StateInterface>> on_export_state_interfaces()
{
std::vector<std::shared_ptr<StateInterface>> state_interfaces = export_state_interfaces_2();
// import the unlisted interfaces
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
export_state_interfaces_2();

std::vector<std::shared_ptr<StateInterface>> state_interfaces;
state_interfaces.reserve(
joint_state_interfaces_.size() + sensor_state_interfaces_.size() +
gpio_state_interfaces_.size());
unlisted_interface_descriptions.size() + joint_state_interfaces_.size() +
sensor_state_interfaces_.size() + gpio_state_interfaces_.size());

// add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
// maps.
for (const auto & description : unlisted_interface_descriptions)
{
auto name = description.get_name();
unlisted_state_interfaces_.insert(std::make_pair(name, description));
auto state_interface = std::make_shared<StateInterface>(description);
system_states_.insert(std::make_pair(name, state_interface));
state_interfaces.push_back(state_interface);
}

for (const auto & [name, descr] : joint_state_interfaces_)
{
Expand Down Expand Up @@ -252,13 +268,14 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI

/**
* Override this method to export custom CommandInterfaces which are not defined in the URDF file.
* Those interfaces will be added to the unlisted_command_interfaces_ map.
*
* Note method name is going to be changed to export_command_interfaces() as soon as the
* deprecated version is removed.
*
* \return vector of shared pointers to the created and stored StateInterfaces
* \return vector of descriptions to the unlisted CommandInterfaces
*/
virtual std::vector<std::shared_ptr<CommandInterface>> export_command_interfaces_2()
virtual std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2()
{
// return empty vector by default.
return {};
Expand All @@ -273,9 +290,25 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
*/
std::vector<std::shared_ptr<CommandInterface>> on_export_command_interfaces()
{
std::vector<std::shared_ptr<CommandInterface>> command_interfaces =
// import the unlisted interfaces
std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
export_command_interfaces_2();
command_interfaces.reserve(joint_command_interfaces_.size() + gpio_command_interfaces_.size());

std::vector<std::shared_ptr<CommandInterface>> command_interfaces;
command_interfaces.reserve(
unlisted_interface_descriptions.size() + joint_command_interfaces_.size() +
gpio_command_interfaces_.size());

// add InterfaceDescriptions and create the CommandInterfaces from the descriptions and add to
// maps.
for (const auto & description : unlisted_interface_descriptions)
{
auto name = description.get_name();
unlisted_command_interfaces_.insert(std::make_pair(name, description));
auto command_interface = std::make_shared<CommandInterface>(description);
system_commands_.insert(std::make_pair(name, command_interface));
command_interfaces.push_back(command_interface);
}

for (const auto & [name, descr] : joint_command_interfaces_)
{
Expand Down Expand Up @@ -403,10 +436,14 @@ class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeI
std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;

std::unordered_map<std::string, std::shared_ptr<StateInterface>> system_states_;
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> system_commands_;
std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;

rclcpp_lifecycle::State lifecycle_state_;

private:
std::unordered_map<std::string, std::shared_ptr<StateInterface>> system_states_;
std::unordered_map<std::string, std::shared_ptr<CommandInterface>> system_commands_;
};

} // namespace hardware_interface
Expand Down
17 changes: 0 additions & 17 deletions hardware_interface/src/component_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,21 +756,4 @@ std::vector<InterfaceDescription> parse_command_interface_descriptions_from_hard
return component_command_interface_descriptions;
}

std::vector<InterfaceDescription> parse_gpio_command_interface_descriptions_from_hardware_info(
const HardwareInfo & hw_info)
{
std::vector<InterfaceDescription> gpio_command_interface_descriptions;
gpio_command_interface_descriptions.reserve(hw_info.gpios.size());

for (const auto & gpio : hw_info.gpios)
{
for (const auto & command_interface : gpio.command_interfaces)
{
gpio_command_interface_descriptions.emplace_back(
InterfaceDescription(gpio.name, command_interface));
}
}
return gpio_command_interface_descriptions;
}

} // namespace hardware_interface
60 changes: 25 additions & 35 deletions hardware_interface/test/test_component_interfaces_custom_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,23 @@ class DummyActuatorDefault : public hardware_interface::ActuatorInterface
{
std::string get_name() const override { return "DummyActuatorDefault"; }

std::vector<std::shared_ptr<hardware_interface::StateInterface>> export_state_interfaces_2()
override
std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2() override
{
std::vector<std::shared_ptr<hardware_interface::StateInterface>> interfaces;
auto unlisted_state_interface = std::make_shared<hardware_interface::StateInterface>(
info_.joints[0].name, "some_unlisted_interface", nullptr);
actuator_states_.insert(
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
std::vector<hardware_interface::InterfaceDescription> interfaces;
hardware_interface::InterfaceInfo info;
info.name = "some_unlisted_interface";
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
interfaces.push_back(unlisted_state_interface);

return interfaces;
}

std::vector<std::shared_ptr<hardware_interface::CommandInterface>> export_command_interfaces_2()
override
std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2() override
{
std::vector<std::shared_ptr<hardware_interface::CommandInterface>> interfaces;
auto unlisted_state_interface = std::make_shared<hardware_interface::CommandInterface>(
info_.joints[0].name, "some_unlisted_interface", nullptr);
actuator_commands_.insert(
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
std::vector<hardware_interface::InterfaceDescription> interfaces;
hardware_interface::InterfaceInfo info;
info.name = "some_unlisted_interface";
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
interfaces.push_back(unlisted_state_interface);

return interfaces;
Expand All @@ -100,14 +96,12 @@ class DummySensorDefault : public hardware_interface::SensorInterface
{
std::string get_name() const override { return "DummySensorDefault"; }

std::vector<std::shared_ptr<hardware_interface::StateInterface>> export_state_interfaces_2()
override
std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2() override
{
std::vector<std::shared_ptr<hardware_interface::StateInterface>> interfaces;
auto unlisted_state_interface = std::make_shared<hardware_interface::StateInterface>(
info_.sensors[0].name, "some_unlisted_interface", nullptr);
sensor_states_.insert(
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
std::vector<hardware_interface::InterfaceDescription> interfaces;
hardware_interface::InterfaceInfo info;
info.name = "some_unlisted_interface";
hardware_interface::InterfaceDescription unlisted_state_interface(info_.sensors[0].name, info);
interfaces.push_back(unlisted_state_interface);

return interfaces;
Expand All @@ -124,27 +118,23 @@ class DummySystemDefault : public hardware_interface::SystemInterface
{
std::string get_name() const override { return "DummySystemDefault"; }

std::vector<std::shared_ptr<hardware_interface::StateInterface>> export_state_interfaces_2()
override
std::vector<hardware_interface::InterfaceDescription> export_state_interfaces_2() override
{
std::vector<std::shared_ptr<hardware_interface::StateInterface>> interfaces;
auto unlisted_state_interface = std::make_shared<hardware_interface::StateInterface>(
info_.joints[0].name, "some_unlisted_interface", nullptr);
system_states_.insert(
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
std::vector<hardware_interface::InterfaceDescription> interfaces;
hardware_interface::InterfaceInfo info;
info.name = "some_unlisted_interface";
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
interfaces.push_back(unlisted_state_interface);

return interfaces;
}

std::vector<std::shared_ptr<hardware_interface::CommandInterface>> export_command_interfaces_2()
override
std::vector<hardware_interface::InterfaceDescription> export_command_interfaces_2() override
{
std::vector<std::shared_ptr<hardware_interface::CommandInterface>> interfaces;
auto unlisted_state_interface = std::make_shared<hardware_interface::CommandInterface>(
info_.joints[0].name, "some_unlisted_interface", nullptr);
system_commands_.insert(
std::make_pair(unlisted_state_interface->get_name(), unlisted_state_interface));
std::vector<hardware_interface::InterfaceDescription> interfaces;
hardware_interface::InterfaceInfo info;
info.name = "some_unlisted_interface";
hardware_interface::InterfaceDescription unlisted_state_interface(info_.joints[0].name, info);
interfaces.push_back(unlisted_state_interface);

return interfaces;
Expand Down

0 comments on commit 8dec983

Please sign in to comment.