jhiggason / yahboomg1tank

ROS2 for YAHBOOM G1 Tank
https://opensar.net
MIT License
9 stars 0 forks source link

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 9 months ago

jhiggason commented 9 months ago

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 commented 9 months ago

I think you could replace /home/user with ~

jhiggason commented 9 months ago

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 commented 9 months ago

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