Skip to content

Commit 7169ebb

Browse files
soham2560omichel
andauthored
Added hardware_interface ver 5.x.x support (#1042)
* added hardware_interface ver 5.x.x support Signed-off-by: soham2560 * added ver5.3 support * added clang changes * fixed logic for testing ver * Update webots_ros2_control/src/Ros2Control.cpp Co-authored-by: Olivier Michel <[email protected]> * Update webots_ros2_control/src/Ros2Control.cpp Co-authored-by: Olivier Michel <[email protected]> --------- Co-authored-by: Olivier Michel <[email protected]>
1 parent 7435cfb commit 7169ebb

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

webots_ros2_control/include/webots_ros2_control/Ros2ControlSystem.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ namespace webots_ros2_control {
5454

5555
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_init(
5656
const hardware_interface::HardwareInfo &info) override;
57+
#if HARDWARE_INTERFACE_VERSION_MAJOR > 5 || (HARDWARE_INTERFACE_VERSION_MAJOR == 5 && HARDWARE_INTERFACE_VERSION_MINOR >= 3)
58+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_init(
59+
const hardware_interface::HardwareComponentInterfaceParams &params) override;
60+
#endif
5761
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_activate(
5862
const rclcpp_lifecycle::State & /*previous_state*/) override;
5963
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_deactivate(

webots_ros2_control/src/Ros2Control.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,56 @@ const double CONTROLLER_MANAGER_ALLOWED_SAMPLE_ERROR_MS = 1.0;
3636

3737
namespace webots_ros2_control {
3838

39-
#if HARDWARE_INTERFACE_VERSION_MAJOR >= 4 && HARDWARE_INTERFACE_VERSION_MINOR >= 12
39+
#if HARDWARE_INTERFACE_VERSION_MAJOR > 5 || (HARDWARE_INTERFACE_VERSION_MAJOR == 5 && HARDWARE_INTERFACE_VERSION_MINOR >= 3)
40+
class WebotsResourceManager : public hardware_interface::ResourceManager {
41+
public:
42+
WebotsResourceManager(webots_ros2_driver::WebotsNode *node) :
43+
hardware_interface::ResourceManager(node->get_node_clock_interface(), node->get_node_logging_interface()),
44+
mHardwareLoader("webots_ros2_control", "webots_ros2_control::Ros2ControlSystemInterface"),
45+
mLogger(node->get_logger().get_child("WebotsResourceManager")) {
46+
mNode = node;
47+
}
48+
49+
WebotsResourceManager(const WebotsResourceManager &) = delete;
50+
51+
bool load_and_initialize_components(const hardware_interface::ResourceManagerParams &param) override {
52+
components_are_loaded_and_initialized_ = true;
53+
54+
std::vector<hardware_interface::HardwareInfo> controlHardware;
55+
try {
56+
controlHardware = hardware_interface::parse_control_resources_from_urdf(param.robot_description);
57+
} catch (const std::runtime_error &ex) {
58+
throw std::runtime_error("URDF cannot be parsed by a `ros2_control` component parser: " + std::string(ex.what()));
59+
}
60+
for (unsigned int i = 0; i < controlHardware.size(); i++) {
61+
const std::string pluginName = controlHardware[i].hardware_plugin_name;
62+
63+
std::unique_ptr<webots_ros2_control::Ros2ControlSystemInterface> webotsSystem;
64+
try {
65+
webotsSystem = std::unique_ptr<webots_ros2_control::Ros2ControlSystemInterface>(
66+
mHardwareLoader.createUnmanagedInstance(pluginName));
67+
} catch (pluginlib::PluginlibException &ex) {
68+
RCLCPP_ERROR(mLogger, "The plugin failed to load for some reason. Error: %s\n", ex.what());
69+
continue;
70+
}
71+
72+
webotsSystem->init(mNode, controlHardware[i]);
73+
hardware_interface::HardwareComponentParams component_params;
74+
component_params.hardware_info = controlHardware[i];
75+
component_params.clock = mNode->get_clock();
76+
component_params.logger = mNode->get_logger();
77+
import_component(std::move(webotsSystem), component_params);
78+
}
79+
80+
return components_are_loaded_and_initialized_;
81+
}
82+
83+
private:
84+
webots_ros2_driver::WebotsNode *mNode;
85+
pluginlib::ClassLoader<webots_ros2_control::Ros2ControlSystemInterface> mHardwareLoader;
86+
rclcpp::Logger mLogger;
87+
};
88+
#elif HARDWARE_INTERFACE_VERSION_MAJOR == 5 || (HARDWARE_INTERFACE_VERSION_MAJOR == 4 && HARDWARE_INTERFACE_VERSION_MINOR >= 12)
4089
class WebotsResourceManager : public hardware_interface::ResourceManager {
4190
public:
4291
WebotsResourceManager(webots_ros2_driver::WebotsNode *node) :
@@ -118,7 +167,7 @@ namespace webots_ros2_control {
118167
}
119168

120169
// Control Hardware
121-
#if HARDWARE_INTERFACE_VERSION_MAJOR >= 4 && HARDWARE_INTERFACE_VERSION_MINOR >= 12
170+
#if HARDWARE_INTERFACE_VERSION_MAJOR > 4 || (HARDWARE_INTERFACE_VERSION_MAJOR == 4 && HARDWARE_INTERFACE_VERSION_MINOR >= 12)
122171
std::unique_ptr<hardware_interface::ResourceManager> resourceManager =
123172
std::make_unique<webots_ros2_control::WebotsResourceManager>(node);
124173
#else

webots_ros2_control/src/Ros2ControlSystem.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ namespace webots_ros2_control {
9898
}
9999
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
100100
}
101+
#if HARDWARE_INTERFACE_VERSION_MAJOR > 5 || (HARDWARE_INTERFACE_VERSION_MAJOR == 5 && HARDWARE_INTERFACE_VERSION_MINOR >= 3)
102+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn Ros2ControlSystem::on_init(
103+
const hardware_interface::HardwareComponentInterfaceParams &params) {
104+
if (hardware_interface::SystemInterface::on_init(params) !=
105+
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS) {
106+
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::ERROR;
107+
}
108+
return rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn::SUCCESS;
109+
}
110+
#endif
101111

102112
std::vector<hardware_interface::StateInterface> Ros2ControlSystem::export_state_interfaces() {
103113
std::vector<hardware_interface::StateInterface> interfaces;

0 commit comments

Comments
 (0)