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

How to load a file contained in a package

asked 2014-06-19 01:23:00 -0500

Mehdi. gravatar image

Let's say I have a file with stored information. I don't want to put the absolute path, neither do I want to roscd mypackage before executing my node so that the file can be found. Is there an elegant way of doing that on both Python and C++?

If it is not possible, how can I then solve the problem when using roslaunch, because then the file is not found anymore even when change my directory to the packages directory before launching.

edit retag flag offensive close merge delete

Comments

Excellent question! I usually set the project source dir as define in CMakeLists.txt ( set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLOCATION=${PROJECT_SOURCE_DIR}) ) and then use this define (LOCATION) in my code to locate the files. --> Works but what would be the proper way to do that??

Wolf gravatar image Wolf  ( 2014-06-19 07:45:42 -0500 )edit

is {PROJECT_SOURCE_DIR} a general flag recognized by CMake or do you replace it by the global path? In case of the latter option this doesn't fully solve the problem.

Mehdi. gravatar image Mehdi.  ( 2014-06-19 20:33:53 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-06-30 04:09:24 -0500

gvdhoorn gravatar image

updated 2014-06-30 04:13:17 -0500

You might be interested in the roslib C++ API. Especially the section on packages.

To get the fully qualified path to a package (from there it should be easy enough to navigate to the actual file) you should probably use ros::package::getPath(const std::string& package_name).

edit flag offensive delete link more

Comments

I think this is the right way to do it. Thanks ! I also found this tutorial that describes how to use the API for both C++ and Python http://wiki.ros.org/Packages#Client_Library_Support

Mehdi. gravatar image Mehdi.  ( 2014-06-30 04:18:25 -0500 )edit
0

answered 2014-06-30 03:17:04 -0500

Mehdi. gravatar image

So after looking around I finally found an easy solution. Just execute the command "rospack find package_name" from your program and get the output of the command back as string. Then just append the name of your file contained in that folder. Of course here I suppose that the name of the package is known and that it will not change. I think this is goes straight forward in Python. In C++ It can be implemented this way:

string get_package_path(char const* package_name) {
    stringstream ss;
    ss << "rospack find " << package_name;
    FILE* pipe = popen(ss.str().c_str(), "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe);
    return result.substr(0, result.length() -1);
}

I remove the last character from the string before returning it because it is a "\n" new line character which is not good when I later append the name of my file contained in the package folder. I hope this could help.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-06-19 01:23:00 -0500

Seen: 824 times

Last updated: Jun 30 '14