Ask Your Question
6

Path to resource files in C++ node

asked 2013-08-20 03:03:24 -0500

Huibuh gravatar image

updated 2014-01-28 17:17:42 -0500

ngrennan gravatar image

Hello fellow developers,

I have trouble determining the path to a folder containing resource files in a self written C++ catkin package. I am using the following macro in CMakeLists.txt to export the resource files (a couple of .xml files btw.):

install(DIRECTORY resources
        DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

This creates the following "resources" folder:

home/ros/catkin_ws/install/share/opencv_ros_node/resources/

I need to access the resource files in my source code here:

string cascade_path = "/home/ros/catkin_ws/src/opencv_ros_node/src/resources/haarcascade.xml";
haar_cascade.load( cascade_path );

Obviously, I do not want to use a hardcoded absolute path. But how can I determine the correct relative path to the resource folder during run time? (Ideally, this should also work if 'catkin_make install' has not been run explicitly)

Any help is appreciated.

Cheers, Heiko

edit retag flag offensive close merge delete

Comments

I guess you'd be OK if it's in PKG/resources as installed and not PKG/src/resources (or anywhere generically reachable). The src/ directory might not exist for installations.

dornhege gravatar imagedornhege ( 2013-08-20 03:13:11 -0500 )edit

1 Answer

Sort by » oldest newest most voted
8

answered 2013-08-20 03:11:39 -0500

dornhege gravatar image

Use the ros::package API:

ros::package::getPath('PKG_NAME') should give you the package path, where you can append your relative resource path to get a full path.

(under the assumption that this hasn't changed for catkin)

edit flag offensive delete link more

Comments

1

Awesome, thanks for your quick help! The only thing is that I now have to hard code 'PKG_NAME'. Is there a command to determine the name of the package this command is executed in?

Huibuh gravatar imageHuibuh ( 2013-08-20 03:53:56 -0500 )edit

I guess now it depends on what you want to do: If it's the package the source is in (or always the same), hardcoding would be OK. If that's not the case you can read it from a parameter or similar. Resources are usually given as package://MY_PKG/foo. If you need an open file: pkg: resource_retriever

dornhege gravatar imagedornhege ( 2013-08-20 04:49:20 -0500 )edit

The proposes API is still valid with catkin. It resolves the path of the package.xml file which is under INSTALL_PREFIX/share/PKGNAME. You should make sure that the relative path from there is the same as in the source checkout of your package. That ensures that it is found in both devel- and install space. For your example with the resources folder that is the case.

Dirk Thomas gravatar imageDirk Thomas ( 2013-08-20 06:41:39 -0500 )edit

Thanks guys, I got it working :-)

Huibuh gravatar imageHuibuh ( 2013-08-20 20:52:03 -0500 )edit
1

Is there a way to retrieve the name of the package in which a node is contained? eg: ros::package::getPath(ros::this_package::getName())?

VictorLamoine gravatar imageVictorLamoine ( 2017-02-07 09:18:06 -0500 )edit

Thanks for the help!

antoineniotna gravatar imageantoineniotna ( 2019-05-07 03:35:40 -0500 )edit

I keep getting ‘ros::package’ has not been declared errors all the way.

Jägermeister gravatar imageJägermeister ( 2019-05-28 04:31:11 -0500 )edit

#include <ros/package.h>

VictorLamoine gravatar imageVictorLamoine ( 2019-05-28 14:18:13 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-08-20 03:03:24 -0500

Seen: 7,720 times

Last updated: Aug 20 '13