ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A

Using ClassLoader with nodes and non-default constructors

I’m playing with the composition API in ROS2 (beta1) and ClassLoader::createInstance function takes only a string as a parameter (the name of the class being created). I need to pass a couple of other parameters to the constructor of my class - is there a way to do that?

1 Like

The best way to do so currently is have your object read parameters from the rosparam server, and before you instantiate your object, set those params.

I think the typical pattern is to create a default constructor for your plugin, but use a separate initialize(…) method to feed arguments.

See plugin definition packages such as nav_core in the navigation stack, and controller_interface in ros_control.

1 Like

In general as mentioned by @TheDash and @Paul_Bovbel you must defer your initialization until later in the cycle.

To support loading components not known about at compile time we must require a standard uniform API of all components for the loader to interact with. Almost all plugin systems work with a minimum common API (often implemented in C++ as a base class) and a registration/discovery mechanism for the loader to find available plugins. Sometimes the registration/discovery also includes descriptions etc.

Using parameters is the recommended way to configure nodes as this is the way that configuration can be made at run time by users.

With respect to ROS 2. I’d suggest making sure you’re familiar with our overview of composition as well as our vision of a managed node which adds a standard lifecycle on top which we can provide standardized tooling for managing ROS systems. This also gives a standard way to do initialization, configuration, and other transitions.

For comparison you can also explore the nodelet API in ROS1.

Thanks everyone. Still wondering how could I possibly configure things such as node name or whether I should use intra-process communication. These values are required at the construction time, so initialization function is out of the question. Not sure if I can use parameters for those …

Node name can be configured via rosparam strictly. e.g

ros.init_node(ros.get_param(“my_node/prefix”) + “mynode”);

I can’t remember the exact API calls off the top of my head, but that’s the general approach. Above syntax wont work directly FYI, is what I’m trying to say.