Skip to content

Commit b728d76

Browse files
Add ROS 2 Jazzy compatibility for hardware_interface and PidROS APIs
Jazzy (hardware_interface v4) uses the old on_init(HardwareInfo&) API and 3-arg PidROS constructor, but with new method names (initialize_from_ros_parameters, get_gains, compute_command). The existing code assumed v4+ meant HardwareComponentInterfaceParams and 4-arg PidROS, which only exist in Rolling/Kilted (v5+).
1 parent 75e4fb8 commit b728d76

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

mujoco_ros2_control/include/mujoco_ros2_control/mujoco_system_interface.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#include "transmission_interface/transmission_loader.hpp"
5858

5959
#define ROS_DISTRO_HUMBLE (HARDWARE_INTERFACE_VERSION_MAJOR < 3)
60+
// Jazzy has hardware_interface v4 but still uses old on_init(HardwareInfo&) API and 3-arg PidROS.
61+
// HardwareComponentInterfaceParams and 4-arg PidROS only exist in Rolling/Kilted (v5+).
62+
#define ROS_DISTRO_JAZZY (HARDWARE_INTERFACE_VERSION_MAJOR == 4)
63+
#define USE_OLD_ON_INIT_API (ROS_DISTRO_HUMBLE || ROS_DISTRO_JAZZY)
6064

6165
// defining these for Humble, because they are defined elsewhere in future versions, and we use them in this file
6266
#if ROS_DISTRO_HUMBLE
@@ -85,9 +89,9 @@ class MujocoSystemInterface : public hardware_interface::SystemInterface
8589
~MujocoSystemInterface() override;
8690

8791
hardware_interface::CallbackReturn
88-
// Jazzy introduces a new HarwareComponentInterfaceParams object which doesn't exist in humble. This adds
89-
// compatibility by switching to the old interface, which behaves similarly
90-
#if ROS_DISTRO_HUMBLE
92+
// HardwareComponentInterfaceParams only exists in Rolling/Kilted (v5+).
93+
// Humble and Jazzy both use on_init(HardwareInfo&).
94+
#if USE_OLD_ON_INIT_API
9195
on_init(const hardware_interface::HardwareInfo& info) override;
9296
#else
9397
on_init(const hardware_interface::HardwareComponentInterfaceParams& params) override;

mujoco_ros2_control/src/mujoco_system_interface.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,8 @@ MujocoSystemInterface::~MujocoSystemInterface()
678678
}
679679

680680
hardware_interface::CallbackReturn
681-
// after humble switches from HardwareInfo to HardwareComponentInterfaceParams. This keeps it backwards compatible
682-
// between the two distros
683-
#if ROS_DISTRO_HUMBLE
681+
// Humble and Jazzy use on_init(HardwareInfo&); Rolling/Kilted use HardwareComponentInterfaceParams.
682+
#if USE_OLD_ON_INIT_API
684683
MujocoSystemInterface::on_init(const hardware_interface::HardwareInfo& params)
685684
#else
686685
MujocoSystemInterface::on_init(const hardware_interface::HardwareComponentInterfaceParams& params)
@@ -1696,12 +1695,18 @@ bool MujocoSystemInterface::register_mujoco_actuators()
16961695

16971696
// Initialize PID controllers for actuators that have them configured
16981697
const auto initialize_position_pids = [&]() -> bool {
1699-
// after humble has an additional argument in the PidROS constructor, and uses a different function to initialize from parameters
1698+
// Humble: 3-arg PidROS ctor + old method names; Jazzy: 3-arg ctor + new method names;
1699+
// Rolling/Kilted: 4-arg ctor + new method names
17001700
#if ROS_DISTRO_HUMBLE
17011701
actuator_data.pos_pid = std::make_shared<control_toolbox::PidROS>(
17021702
get_node(), "pid_gains.position." + actuator_data.joint_name, false);
17031703
actuator_data.pos_pid->initPid();
17041704
const auto gains = actuator_data.pos_pid->getGains();
1705+
#elif ROS_DISTRO_JAZZY
1706+
actuator_data.pos_pid = std::make_shared<control_toolbox::PidROS>(
1707+
get_node(), "pid_gains.position." + actuator_data.joint_name, false);
1708+
actuator_data.pos_pid->initialize_from_ros_parameters();
1709+
const auto gains = actuator_data.pos_pid->get_gains();
17051710
#else
17061711
actuator_data.pos_pid = std::make_shared<control_toolbox::PidROS>(
17071712
get_node(), "pid_gains.position." + actuator_data.joint_name, "", false);
@@ -1712,12 +1717,18 @@ bool MujocoSystemInterface::register_mujoco_actuators()
17121717
};
17131718

17141719
const auto initialize_velocity_pids = [&]() -> bool {
1715-
// after humble has an additional argument in the PidROS constructor, and uses a different function to initialize from parameters
1720+
// Humble: 3-arg PidROS ctor + old method names; Jazzy: 3-arg ctor + new method names;
1721+
// Rolling/Kilted: 4-arg ctor + new method names
17161722
#if ROS_DISTRO_HUMBLE
17171723
actuator_data.vel_pid = std::make_shared<control_toolbox::PidROS>(
17181724
get_node(), "pid_gains.velocity." + actuator_data.joint_name, false);
17191725
actuator_data.vel_pid->initPid();
17201726
const auto gains = actuator_data.pos_pid->getGains();
1727+
#elif ROS_DISTRO_JAZZY
1728+
actuator_data.vel_pid = std::make_shared<control_toolbox::PidROS>(
1729+
get_node(), "pid_gains.velocity." + actuator_data.joint_name, false);
1730+
actuator_data.vel_pid->initialize_from_ros_parameters();
1731+
const auto gains = actuator_data.vel_pid->get_gains();
17211732
#else
17221733
actuator_data.vel_pid = std::make_shared<control_toolbox::PidROS>(
17231734
get_node(), "pid_gains.velocity." + actuator_data.joint_name, "", false);

mujoco_ros2_control/test/test_headless_init.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <rclcpp/rclcpp.hpp>
2828

2929
#define ROS_DISTRO_HUMBLE (HARDWARE_INTERFACE_VERSION_MAJOR < 3)
30+
#define ROS_DISTRO_JAZZY (HARDWARE_INTERFACE_VERSION_MAJOR == 4)
31+
#define USE_OLD_ON_INIT_API (ROS_DISTRO_HUMBLE || ROS_DISTRO_JAZZY)
3032

3133
class HeadlessInitTest : public ::testing::Test
3234
{
@@ -122,7 +124,7 @@ class HeadlessInitTest : public ::testing::Test
122124
TEST_F(HeadlessInitTest, HeadlessInitialization)
123125
{
124126
// Test that MujocoSystemInterface can be initialized in headless mode
125-
#if ROS_DISTRO_HUMBLE
127+
#if USE_OLD_ON_INIT_API
126128
auto result = interface_->on_init(hardware_info_);
127129
#else
128130
hardware_interface::HardwareComponentInterfaceParams params;

0 commit comments

Comments
 (0)