Gathering Data from a .yaml file

I’m in section 4 of the rosject for ROS2 Navigation. I have successfully recorded coordinate information about different points around the map to a file called spots.yaml.
After submitting this command:

ros2 run project_path_planning move_to_spot --ros-args --params-file spots.yaml -p spot_name:=corner1

I get this error:

user:~/ros2_ws$ ros2 run project_path_planning move_to_spot --ros-args --params-file spots.yaml -p spot_name:=corner1[ERROR] [1713052429.485647395] [rcl]: Failed to parse global arguments
Traceback (most recent call last):
  File "/home/user/ros2_ws/install/project_path_planning/lib/project_path_planning/move_to_spot", line 33, in <module>
    sys.exit(load_entry_point('project-path-planning==0.0.0', 'console_scripts', 'move_to_spot')())
  File "/home/user/ros2_ws/install/project_path_planning/lib/python3.10/site-packages/project_path_planning/move_to_spot.py", line 53, inmain
    rclpy.init(args=None)
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/__init__.py", line 89, in init
    return context.init(args, domain_id=domain_id)
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/context.py", line 72, in init
    self.__context = _rclpy.Context(
rclpy._rclpy_pybind11.RCLError: failed to initialize rcl: Couldn't parse params file: '--params-file spots.yaml'. Error: Error opening YAML file, at ./src/parser.c:270, at ./src/rcl/arguments.c:406
[ros2run]: Process exited with failure 1

I need some help understanding what this error is trying to tell me and how I can go about fixing it.

Here is spots.yaml:

move_to_pose_node:
  ros__parameters:
    corner1:
      omega: -0.9683634756224331
      x: 0.7958630374314711
      y: 0.615868777894957
    corner2:
      omega: -0.010386799592166191
      x: -0.4593073968372557
      y: -0.7798336337465239
    pedestrian:
      omega: -0.01275618862992205
      x: 0.1160198117051934
      y: 0.5971075792413882

Thanks for your help.

Hi,

This error could mean various things:

  1. That you havent sources ROS2 workspace and it just doenst know what to do with the code.
  2. Check that the files is in the location you are stating and has all the permission open ( chmod 777 spots.yaml )
  3. Can you execute this command?
    python3 -c 'import yaml; print(yaml.safe_load(open("spots.yaml")))'

Maybe is something else but I would try these steps first :wink:

  1. As constructsim.com has taught me, I run this command every time I build a package (and also in every terminal)
source ~/ros2_ws/install/setup.bash
  1. This was something I considered as well. I made sure the file had full permissions.
user:~/ros2_ws/src/project_path_planning/config$ ls -l
total 24
-rw-r--r-- 1 user user 1294 Mar 29 23:47 behavior.xml
-rw-r--r-- 1 user user 2096 Mar 29 23:50 bt_navigator.yaml
-rw-r--r-- 1 user user 1951 Mar 29 23:47 controller.yaml
-rw-r--r-- 1 user user  262 Mar 30 00:51 planner_server.yaml
-rw-r--r-- 1 user user  570 Mar 29 23:47 recovery.yaml
-rwxrwxrwx 1 user user  351 Apr 14 00:08 spots.yaml
user:~/ros2_ws$ source ~/ros2_ws/install/setup.bash
user:~/ros2_ws$ python3 -c 'import yaml; print(yaml.safe_load(open("spots.yaml")))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'spots.yaml'
user:~/ros2_ws$ python3 -c 'import yaml; print(yaml.safe_load(open("~/ros2_ws/src/project_path_planning/config/spots.yaml")))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '~/ros2_ws/src/project_path_planning/config/spots.yaml'
user:~/ros2_ws$

How does your command know where to look for “spots.yaml”? I thought that the file path might be missing from your command so I tried it with the direct file path to “spots.yaml” but still got the same error. I don’t understand the error this command produced. The file exists and I think its in the right directory.

I amended the command you suggested from 3 to

python3 -c 'import yaml, os; print(yaml.safe_load(open(os.path.expanduser("~/ros2_ws/src/project_path_planning/config/spots.yaml"))))'

The result:

user:~/ros2_ws$ python3 -c 'import yaml, os; print(yaml.safe_load(open(os.path.expanduser("~/ros2_ws/src/project_path_planning/config/spots.yaml"))))'
{'move_to_pose_node': {'ros__parameters': {'corner1': {'omega': -0.9683634756224331, 'x': 0.7958630374314711, 'y': 0.615868777894957}, 'corner2': {'omega': -0.010386799592166191, 'x': -0.4593073968372557, 'y': -0.7798336337465239}, 'pedestrian': {'omega': -0.01275618862992205, 'x': 0.1160198117051934, 'y': 0.5971075792413882}}}}

I changed the action client command to this:

ros2 run project_path_planning move_to_spot --ros-args --params-file /home/user/ros2_ws/project_path_planning/config/spots.yaml -p spot_name:=corner1

and it works.

What this tells me is that my spots.yaml file is not in the default place that this command line is looking for. Where is the default location for this parameter file?

Hi @pharva ,

When a compiled ROS2 package is launched or run, it will look for all its dependent files in the install directory.
So the file path should actually be:

/home/user/ros2_ws/install/project_path_planning/share/project_path_planning/config/spots.yaml

It is strange to me that your package is looking for the spots.yaml file in the following path:

This is of course conditioned on the setup.py (for Python package) or the CMakeLists.txt file (for C++ package).
You should have included the config folder path in the setup.py or CMakeLists.txt file to be installed when the package is built.

You will have something similar to the following lines:
If using setup.py:

(os.path.join('share', package_name), glob('config/*')),

If using CMakeLists.txt:

install(DIRECTORY 
  ... # other folder names
  config # <--- add this line
  ... # other folder names
  DESTINATION share/${PROJECT_NAME}
)

So technically, if you include the config folder path to the install path, then your path references should be correct.

I hope this helps!

Regards,
Girish

@girishkumar.kannan
Thank you for the reply. Below is the setup.py file for the python package. Indeed the config folder is included into setup file.

from setuptools import setup
import os
from glob import glob

package_name = 'project_path_planning'

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']),
        (os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')),
        (os.path.join('share', package_name, 'config'), glob('config/*')),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='user',
    maintainer_email='user@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'spot_recorder=project_path_planning.spot_recorder:main',
            'move_to_spot=project_path_planning.move_to_spot:main'
        ],
    },
)

Do you have any other suggestions? A solution to this question is not critical as I have found a way around the problem. If the topic/post needs to be closed, I understand.

Hi @pharva ,

I am not exactly sure why your run command requires the path to the spots.yaml file specified in a different (unconventional) way. It is still strange to me.

What was the output that you got when you executed the command above with the path I referenced? This following command should work after proper compilation.

ros2 run project_path_planning move_to_spot --ros-args --params-file /home/user/ros2_ws/install/project_path_planning/share/project_path_planning/config/spots.yaml -p spot_name:=corner1

Perhaps you can try compiling from scratch by removing build, install and log folders and that could fix your issue.

cd ~/ros2_ws
rm -rf ./build ./install ./log
colcon build
source install/setup.bash

I hope that helps. Let me know if this worked.

Regards,
Girish

Your command successfully called the action server. I wasn’t fast enough to copy the output before the feedback from the server filled the terminal window.

Deleting the install and build folders did not fix the problem.

I’ll continue to use:

ros2 run project_path_planning move_to_spot --ros-args --params-file /home/user/ros2_ws/src/project_path_planning/config/spots.yaml -p spot_name:=corner1

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.