ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
With the risk of answering an xy-problem: the package name is for roscpp
based nodes is passed as a compile time constant.
An example gcc
command line for an arbitrary node:
/usr/lib/ccache/c++ -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\"my_pkg\" ...
Note the ROS_PACKAGE_NAME
here, set to my_pkg
in this case.
I believe this is the only time roscpp
nodes have access to the name of the package they are hosted in.
2 | No.2 Revision |
With the risk of answering an xy-problem: the package name is for roscpp
based nodes is passed as a compile time constant.
An example gcc
command line for an arbitrary node:
/usr/lib/ccache/c++ -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\"my_pkg\" ...
Note the ROS_PACKAGE_NAME
here, set to my_pkg
in this case.
I believe this is the only time roscpp
nodes have access to the name of the package they are hosted in.
Edit: after your edit: I'm not entirely sure I completely understand what you are describing, but afaik there is no way to conclusively determine in which package a node is at runtime.
At compile-time this is easier, as we can ask CMake to add definitions to the compilation flags. This is what ROS_PACKAGE_NAME
does, and which you already appeared to be using before the refactor you describe.
I would perhaps suggest to generate a .h
which gets included in your derived class and which contains a std::string
constand which is used to pass the package name to the ctor of your base node class. This way, the package name would be set at build time of the derived class, and the super class would still be able to access it.
Even after your edit I don't really understand why the package name is needed specifically, but I'm sure you have valid reasons.
3 | No.3 Revision |
With the risk of answering an xy-problem: the package name is for roscpp
based nodes is passed as a compile time constant.
An example gcc
command line for an arbitrary node:
/usr/lib/ccache/c++ -DROSCONSOLE_BACKEND_LOG4CXX -DROS_BUILD_SHARED_LIBS=1 -DROS_PACKAGE_NAME=\"my_pkg\" ...
Note the ROS_PACKAGE_NAME
here, set to my_pkg
in this case.
I believe this is the only time roscpp
nodes have access to the name of the package they are hosted in.
Edit: after your edit: I'm not entirely sure I completely understand what you are describing, but afaik there is no way to conclusively determine in which package a node is at runtime.
At compile-time this is easier, as we can ask CMake to add definitions to the compilation flags. This is what ROS_PACKAGE_NAME
does, and which you already appeared to be using before the refactor you describe.
I would perhaps suggest to generate a .h
which gets included in your derived class and which contains a std::string
constand which is used to pass the package name to the ctor of your base node class. class (ie, via the node_name
argument). This way, the package name would be set at build time of the derived class, and the super class would still be able to access it.
Even after your edit I don't really understand why the package name is needed specifically, but I'm sure you have valid reasons.