ROS2 equivalent of ros::package::getPath
Is there a ROS2 API with similar functionality to ros::package::getPath
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
The closest equivalent to what ros::package::getPath()
returns in ROS 1 is what we call the package's "share directory", which is essentially the package's "install prefix" plus a standard (FHS based) relative path of share/<package name>
. This is where we put the package.xml
and where you should install architecture independent resources like images, launch files, config files, etc...
So, as an example, if your package is called foo
and you installed to /home/user/my_ros2_ws/install
, then the package share directory would be /home/user/my_ros2_ws/install/share/foo
. But to get to that path you need to know (at runtime) where the package was installed to.
In ROS 2 this information is provided by the resource index:
https://github.com/ament/ament_cmake/...
This index is populated at compile time (rather then by crawling the filesystem at runtime as in ROS 1).
This index can be queried in various ways, but it can be used to get the installation prefix of a package (i.e. something like /opt/ros/ardent/
or ~/my_ros2_ws/install/
), if it is installed on the system.
Once you know that you can easily add the relative share directory path to the install prefix yourself.
However, there are convenience API's in both C++ and Python to get the "share directory" of a package directly:
API:
https://github.com/ament/ament_index/...
Example:
from ament_index_python.packages import get_package_share_directory
# may raise PackageNotFoundError
package_share_directory = get_package_share_directory('my_package_name')
API:
https://github.com/ament/ament_index/...
Example:
#include <ament_index_cpp/get_package_share_directory.hpp>
// may throw ament_index_cpp::PackageNotFoundError exception
std::string package_share_directory = ament_index_cpp::get_package_share_directory("my_package_name");
With the command line you can only get the package install prefix right now, but it would be simple to add the share directory as well.
Docs (only in -h
for now):
% ros2 pkg prefix -h
usage: ros2 pkg prefix [-h] package_name
Output the prefix path of a package
positional arguments:
package_name The package name
optional arguments:
-h, --help show this help message and exit
Example:
% ros2 pkg prefix rclcpp
/Users/william/ros2_ws/install_debug
I made an issue to add the share directory to the command line tool, help welcome :)
In ROS 2 this information is provided by the resource index which is populated at compile time (rather then by crawling the filesystem as in ROS 1). This index can be queried in various ways:
ros2 pkg prefix rclcpp
import ament_index_python
ament_index_python.get_resource('package', 'rclcpp')
In either of the cases the result is the prefix path where the specific package is being installed. Depending on that information you are looking for you can construct derived paths manually. E.g. the manifest is in the location <prefix>/share/<pkgname>
, the include directory is in <prefix>/include
etc.
The exact equivalent in Python is this function:
Sorry, that was the wrong one for Python, I'll edit the comment. The C++ one is here: https://github.com/ament/ament_index/...
@mkhansen, do you mean why is this not the example in this answer or why is it not the answer to the original question? I would say ament_index_cpp::get_package_prefix()
isn't the closest replacement for ros::package::getPath
, but I explain that in my answer above.
Asked: 2018-04-12 15:10:24 -0500
Seen: 18,697 times
Last updated: Jul 21 '22
@GregB can you accept and answer or ask for more clarification? Thanks!