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

how to load the configuration file in package in ros2

asked 2018-05-09 22:12:35 -0600

jzp1025 gravatar image

Hi !

I’m writing a package that needs to read a configuration file .(like .yaml)

I useinstall(FILES xxx.yaml DESTINATION share/${PROJECT_NAME}) to copy the configuration file to the path : /shr/package-name

But i have no idea how to get the right relative path to load it

i’m using the absolute path to read the file now but i want to make it relative so the package can be used in different environment .

looking forward to some suggestions !

edit retag flag offensive close merge delete


I want to know the same thing, but it sounds like you know something I don't know. When you say you use install(...) what are you talking about? Is install() a method of some python module? Where did you get it, or more precisely, how can I use it?

rahvee gravatar image rahvee  ( 2018-05-26 12:46:34 -0600 )edit

Ohhh... Found it myself... Use install() in CMakelists.txt. Good to know, thanks...

rahvee gravatar image rahvee  ( 2018-05-26 13:00:15 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-05-10 10:53:31 -0600

dhood gravatar image

To access the share directory of a package in a generic way you can use the ament index. In particular, get_package_share_directory from ament_index_python, or from ament_index_cpp might be what you're looking for.

There are other useful things that can be done with the ament index, for example the composition demo package "registers" node components in the ament index and then looks up the available node components at run-time without recursively crawling the file system. The ament index docs have more info on the design.

Hope that helps!

edit flag offensive delete link more


Thanks for your help ! I'll try it

jzp1025 gravatar image jzp1025  ( 2018-05-28 02:46:02 -0600 )edit

@jzp1025 did that work for you? If so, could you please mark this as the accepted answer to indicate your question has been resolved.

dhood gravatar image dhood  ( 2018-07-09 17:13:13 -0600 )edit

Question Tools

1 follower


Asked: 2018-05-09 22:11:11 -0600

Seen: 2,012 times

Last updated: May 10 '18