pluginlib : loading a plugin that's a template class and a constructor argument

asked 2023-06-15 00:00:58 -0500

sameh4 gravatar image

While it's possible to export a plugin class that's a template class, I am not sure how the packaging part will work. This in specific reference to ros2 control's hardware interface class. While working on that class, I noticed that I have two ways for making this class a bit more generic:

  1. Use the standard abstract base class and sub classes (which I personally don't prefer and I think most of the C++ community tends to agree)

  2. use a traditional templated class with constructor based dependency injection.

For example:

template<typename MotorController>
class DifferentialDriveControllerHardwareInterface : public hardware_interface::SystemInterface
{
DifferentialDriveControllerHardwareInterface(MotorController motorController);
}
#include "pluginlib/class_list_macros.hpp"
PLUGINLIB_EXPORT_CLASS(
    DifferentialDriveControllerHardwareInterface<SabertoothSerialMotorController>, hardware_interface::SystemInterface)

You can image of course different types of motor controllers, or even different version of the Sabertooth controller, where one uses serial communication and another uses PWM, etc.

The problem is that while this does compile, it won't actually work. As you can see, the constructor needs an instance of that motor controller class. Moreover, the packaging in CMake just wouldn't work

<library path="xiaocars_control">
    <class name="xiaocars_control/DifferentialDriveControllerHardwareInterface"
           type="xc::control::mocks::DifferentialDriveMockHardwareInterface"
           base_class_type="hardware_interface::SystemInterface">
        <description>
            The ros2_control Differntial Drive example using a system hardware interface-type. It uses velocity command and position state interface. The example is the starting point to implement a hardware interface for differential-drive mobile robots.
        </description>
    </class>
</library>
pluginlib_export_plugin_description_file(hardware_interface xcars/duckiebot/urdf/duckie_bot_mock_control.xml)

So my options are (as I see them)

  1. write up a main.cpp with node composition, and load the plugin manually, but I am not sure how to do that?
  2. abandon the template idea and use poorly designed OO with very tight coupling, because how can a plugin have a constructor with an argument??
edit retag flag offensive close merge delete