Creating Multiple Plugins from Single Package in ROS2
I am experimenting with the pluginlib and wondering if it is possible to create two plugin classes from a single package? Is it recommended to rather do separate packages for the different classes?
In my current workspace I have successfully compiled and run three packages:
control_framework - Contains executable and imports the plugins controller - Contains base class linear_controller - Contains two plugin classes
I can create multiple instances of the LinearController1, but as soon as I try and load LinearController2 I get the following error:
The plugin failed to load for some reason. Error: MultiLibraryClassLoader: Could not create object of class type linear_controller::LinearController2 as no factory exists for it. Make sure that the library exists and was explicitly loaded through MultiLibraryClassLoader::loadLibrary()
I know that LinearController2 exists and was exported correctly based on the console printout when I made a typo:
The plugin failed to load for some reason. Error: According to the loaded plugin descriptions the class linear_controller::LinearControlle2 with base class type control_base::Controller does not exist. Declared types are linear_controller::LinearController1 linear_controller::LinearController2
Here is how I am importing them in the ControlFramework.cpp file
ControlFramework.cpp
#include <iostream>
#include <pluginlib/class_loader.hpp>
#include <controller/Controller.h>
int main(int argc, char** argv)
{
pluginlib::ClassLoader<control_base::Controller> ctrl_loader("controller", "control_base::Controller");
try
{
std::cout << "Create Controller 1\n";
std::shared_ptr<control_base::Controller> ctrl1 = ctrl_loader.createSharedInstance("linear_controller::LinearController1");
std::cout << "Init Controller 1\n";
ctrl1->initialize(10.0, 0.0, 0.0);
std::cout << "Controller Output 1: " << ctrl1->computeFeedback(2.0) << "\n";
std::cout << "Create Controller 2\n";
std::shared_ptr<control_base::Controller> ctrl2 = ctrl_loader.createSharedInstance("linear_controller::LinearController2");
std::cout << "Init Controller 2\n";
ctrl2->initialize(5.0, 0.0, 0.0);
std::cout << "Controller Output 2: " << ctrl2->computeFeedback(2.0) << "\n";
}
catch(pluginlib::PluginlibException& ex)
{
std::cout << "The plugin failed to load for some reason. Error: " << ex.what() << "\n";
}
return 0;
}