ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Refer to current package's root name

asked 2017-04-24 04:47:12 -0600

nbro gravatar image

What's the best way to refer to the name of the package's root folder?

For example, if I had a package named X and I wanted to read files from, e.g., the bag subfolder (i.e. X/bag) from either inside a script under scripts (i.e. X/scripts/read_file_from_bag_subfolder.py) or from a module under src (e.g. src/X/read_file_from_bag_subfolder.py), what would be the best way to do it?

I know I can use rospack as I explained here http://wiki.ros.org/Packages , but that requires you to manually specify the name of the current package, but if it changes, then you would also need to change that code, so maybe a relative way of doing it would be better, even though this may also cause problems if you decide to move your code to another subfolder.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2017-04-24 21:37:55 -0600

Geoff gravatar image

Because of the relatively loose coupling between a source file and the package it resides it, I don't think it is easy to find out the package name from within the source file. You could do it by looking for specific parent directories, but this will need to be different depending on if the file has been installed or not (if not, then look for the nearest parent with a package.xml file, if so and installed like a binary then look for the immediate parent directory of the executable). I think this is very fragile, however.

If you don't want to hard-code the package name, which is understandable, and would prefer not to use relative paths inside the package (which might break if the package gets installed), then the best I can come up with is to receive the package name as a command line argument or a ROS parameter inside the script. Then you can specify the package name on the command line or in the launch file. This is really only moving the hard-coding somewhere else, though, and if the script is intended to be run from the command line it would be a pain to use. I think that if the script is always run from a launch file, I would consider putting the package name in a parameter but I do not think the maintenance trade-off is that beneficial. If it's run from the command line, I would personally prefer to hard-code the package name in the script.

By the way, the resource_retriever package is often useful when loading resources, both local and remote. (It won't solve your hard-code-the-package-name problem, though.)

edit flag offensive delete link more
1

answered 2017-04-24 05:27:47 -0600

I'll refer to roscpp, but I expect the same goes for rospy.

You can use ros::package::getPath("<my-package-name>") to get the path to <my-package-name>. You'll have to include ros/package.h.

If you think your package name is going to change, then add a parameter to the parameter server and retrieve the name from there.

edit flag offensive delete link more

Comments

1

Did you read all my question? I mentioned I know there's a way to do it using the rospack module (in Python), which is equivalent to the C++ code you're mentioning, which is also reported in that same article I'm linking you to.

nbro gravatar image nbro  ( 2017-04-24 05:31:53 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-04-24 04:47:12 -0600

Seen: 3,275 times

Last updated: Apr 24 '17