Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you use a YAML config file with ROS2 without having to change where the file is located in the code. #11

Open
jhiggason opened this issue Dec 25, 2023 · 3 comments

Comments

@jhiggason
Copy link
Owner

The last update I made to the ROS2 stuff was incorporating a YAML config file. The yaml file is located under params_pkg/params/robot_params.yaml and a change needs to be made to the tank control package so it works in your enviornment. This line in the tank_control_pkg/tank_control_pkg/tank_control.py file needs line 88 change to your environment : self.config = self.load_yaml_config("/home/jeffh/ros2_ws/src/params_pkg/params/robot_params.yaml")

In my case you can see my home folder is jeffh, that is probably what needs to be changed on yours as well.

Looks like the same needs to be changed for the robot_peripherals_pkg.py as well. Line 19 self.config = self.load_yaml_config("/home/jeffh/ros2_ws/src/params_pkg/params/robot_params.yaml")

Is there a way for us to use a yaml config file without having to change the code to the correct user's home folder?

@matt-desmarais
Copy link

I think you could replace /home/user with ~

@jhiggason
Copy link
Owner Author

I swear I tried that but I think ROS2 had issues with it??? I will definitely try it again because that seems easier than the other solutions I'm finding.

@matt-desmarais
Copy link

To effectively use ~ as a reference to the home directory in your ROS2 application, you need to manually expand it in the code that reads the YAML file. Here's how you might do it in Python:

import os
import yaml

with open('config.yaml', 'r') as file:
    config = yaml.safe_load(file)

config['path'] = os.path.expanduser(config['path'])

The os.path.expanduser function is specifically designed to expand ~ to the current user's home directory. By doing this in your code, you allow ~ in your YAML file to behave as users might expect, similar to its behavior in the shell.

https://chat.openai.com/share/f674013f-feb8-4b3d-b6e3-a5725e0b34b2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants