ros2 external
Hello, What the best way to get the package/file path of a external library thats not part of my ROS2 project directly without having to hardcode the path in a launch file or node?
I have a node - say 'node_py' which requires a particular data file that part of another git repo that ive added as a submodule to my project. So my directory structure looks like:
src/
+node
++node
+++node.py
+external/
++otherproject/
+++datafileinotherproject.txt
I've tried making 'external' a package by adding a package.xml but I can't figure out a non-hacky way to get a relative path to that txt file.
Any pointers would be appreciated - im okay with changing the structure a bit if it makes ros2 conventions work. Thanks!
Asked by Faraz Khan on 2018-06-12 14:19:05 UTC
Answers
You would need to install that file somehow (maybe using package_data
in the setup.py
, see: https://setuptools.readthedocs.io/en/latest/setuptools.html?highlight=data_files#including-data-files), or using the install()
macro if you're using CMake.
Since this is Python, you can try to access these files at runtime using:
https://setuptools.readthedocs.io/en/latest/pkg_resources.html
Or if you installed them and want to use ROS to find the files at runtime, you can use the "ament_index" to locate the package's installation directory and find the file relatively from there, see:
Asked by William on 2018-06-12 19:17:20 UTC
Comments
Thanks @william! I tried the ament_index method already but this ends up giving me the 'install' prefix which is not where the actual files get copied over. Another simple alternative: Is there a way to get the BASE path of my project within the ros framework? So the main src directory or src/..
Asked by Faraz Khan on 2018-06-12 20:20:03 UTC
You cannot assume the source folder of your project is available at runtime. This is a pretty significant difference from ROS 1 where catkin provided the source space when using the devel
mode. We don't have that (on purpose) in ROS 2. You need to install the file and find there.
Asked by William on 2018-06-12 20:23:31 UTC
If you're running a temporary script from the source folder then you can use the __file__
in your python script to get the location of it in the src folder and then navigate relatively from there. But that won't work for install or release of your package.
Asked by William on 2018-06-12 20:24:59 UTC
Gotcha - so I guess the idea is that any package in ROS2 would HAVE to be a proper ROS package (with package.xml/setup.py)if I want to refer to it and get installed etc? No support for external non-ros libraries to be referenced by other ROS packages? Or do i misunderstand? Thanks!
Asked by Faraz Khan on 2018-06-12 20:36:03 UTC
I suppose that’s correct, though not all of the terminology you’re using is familiar to me. Having a package.xml doesn’t do anything magic by itself. There are some ways that might help you but we’re not searching your computer for package.xml files at runtime or anything like that.
Asked by William on 2018-06-12 21:47:06 UTC
Gotcha. Lastly: using package_data or include_package_data im unable to get the files to show up in the install directory - Is there something special I have to do for that to happen? Thanks and sorry for the back and forth!
Asked by Faraz Khan on 2018-06-12 22:01:02 UTC
Here's an example of a package that uses package_data
to install files: https://github.com/ros2/ros2cli/pull/87/files We didn't have to do anything else in particular for that case.
Asked by dhood on 2018-06-12 22:10:04 UTC
Oh man thank you its finally done! it install by default in install/lib/python3.6/site-packages. I changed that using setup.cfg's install-lib and it all works now :) Thank you both!
Asked by Faraz Khan on 2018-06-12 22:26:17 UTC
Another example (since I was already sourcing it :D): https://github.com/ros2/examples/blob/45c0976e02c379ebe6518e6983053c2a6045ee9b/rclpy/topics/minimal_publisher/setup.py#L13-L17
Asked by William on 2018-06-12 22:57:37 UTC
Comments