I'll follow up with a solution to this problem for anyone else who needs to implement a similar node.
First, make sure you add your loadable configuration, load_params.y[a]ml
to your config
folder, as such:
colcon_ws
└── src
├── my_pkg
│ ├── config
│ │ └── load_params.yml
│ ├── my_pkg
│ │ ├── __init__.py
│ │ ├── my_node.py
│ │ └── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── my_node.cpython-38.pyc
│ ├── launch
│ │ ├── backend_launcher.launch.py
│ │ └── frontend_launcher.launch.xml
│ ├── package.xml
│ ├── resource
│ │ └── my_pkg
│ ├── setup.cfg
│ ├── setup.py
│ └── test
│ ├── test_copyright.py
│ ├── test_flake8.py
│ └── test_pep257.py
In your setup.py
, modify it accordingly to have a copy of config
in your install
share space (doesn't hurt to add your launchers too):
import os
from glob import glob
...
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
# Include all configuration files
('share/' + package_name + '/config', glob('config/*')),
# Include all launch files
(os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*')))
],
...
)
Once you've done this, you can invoke, in any Python file that you have placed in install
(be it a node or launch.py) the following:
import yaml
from ament_index_python.packages import get_package_share_directory
...
share_dir = get_package_share_directory(PKG_NAME) # modify this accordingly
fname = share_dir + f"/config/load_params.yaml" # use os.path.join if you want
with open(fname, 'r') as file:
configs = yaml.safe_load(file)
# # note that you will most likely extract a field as the following
# # or, using ['<ns>/<node>'] instead of ['/**']
# config_field = configs['/**']['ros__parameters']['field_name']
Using this in launch backends helps with conditional statements tied in with launch file generation that are not intrinsic properties of the sub-launch files themselves (i.e. how many launch files are you spawning).
In nodes, this allows your nodes to access configurations rather than node parameters, according to the distinction made in the previously given answer
Hope this helps anyone else who is stumped!