Install other files in a ROS2 Python package
You can virtually put everything you
want in a ROS2 package. There is no
hard rule about what to do, but some
conventions make it easier for you.
Let’s see how to install launch files
and YAML config files. Those are among
the most common things you’ll add to
packages when you develop your ROS2
application. Launch files
Create a launch/ folder at the root of
your package. You’ll put all your
launch files inside this folder.
cd ~/ros2_ws/src/my_python_pkg/
mkdir launch
Now, to install those launch files,
you need to modify setup.py.
import os
from glob import glob
from setuptools import setup
...
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')),
],
...
For our example, with package name
my_python_pkg
, this will install all
launch files from the launch/
folder, into
~/ros2_ws/install/my_python_pkg/share/my_python_pkg/launch/
.
Note: you only need to modify
setup.py
once. After that, every
time you add a launch file you’ll just
need to compile your package so that
the file is installed, that’s it.
Then, to start a launch file:
ros2 launch package_name
launch_file_name
YAML config files
You can follow the same technique to
install YAML config files.
Create a config/ folder at the root of
your package. You’ll put all your YAML
files here. $ cd
~/ros2_ws/src/my_python_pkg/ $ mkdir
config
To install YAML files, again, modify
setup.py
. Add a new line in the
data_files
array:
...
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name, 'launch'),
glob('launch/*.launch.py')), (os.path.join('share', package_name, 'config'),
glob('config/*.yaml')),
],
...
Still with the my_python_pkg
example, the YAML files will be
installed into
~/ros2_ws/install/my_python_pkg/share/my_python_pkg/config/
.
see answer here: https://answers.ros.org/question/3497...