简体   繁体   中英

How to read specific parameter from YAML in ROS2 .py Launch file?

I have ROS2 package which has YAML file and.py launch file that starts node. I am reading the all parameters found in the YAML file in the Launch file. I assigned the values I read to the param variable. I want to take the extrinsic parameters from this variable in the YAML file and assign them as the parameters of the static_transform_publisher to the arguments argument. How can I do the that?

Here the YAML file:

/**:
  ros__parameters:
    num_for_pub: 5
    intrinsic: [0.1 , 0.2 , 0.3 , 0.4 , 0.5, 0.6, 0.7, 0.8, 0.9]
    distortion: [1, 2, 3, 4, 5]
    extrinsic: [6, 7, 8, 9, 10, 11]

Here the.py launch file:

def generate_launch_description():
ld = LaunchDescription()

param = os.path.join(
    get_package_share_directory('cloud_projector'),
    'config',
    'cloud_projector.param.yaml'
    )

cloud_projector_node = Node(
    package='cloud_projector',
    namespace='cloud_projector',
    executable='cloud_projector_exec',
    name='cloud_projector',
    parameters = [param]
)

cam_to_lidar_transformer = Node(
    package ='tf2_ros',
    executable='static_transform_publisher',
    arguments=['0','0','1','0','0','0','lidar_frame','camera_frame']
)

ld.add_action(cam_to_lidar_transformer)
ld.add_action(cloud_projector_node)

return ld

Expected Result

The resulted argument in static transform publisher node should look like this:

cam_to_lidar_transformer = Node(
    package ='tf2_ros',
    executable='static_transform_publisher',
    arguments=[x_value,y_value,z_value,roll_value,pitch_value,yaw_value,'lidar_frame','camera_frame']
)

Since this is a regular python script, you can just load the YAML file yourself, ie, given the YAML:

cloud_projector:
  ros__parameters:
    extrinsic: [6, 7, 8, 9, 10, 11]

You can load it using:

import yaml
...
def generate_launch_description():
  ld = LaunchDescription()
  ...
  with open(param, 'r') as f:
    configuration = yaml.safe_load(f)
    print(f'Loaded configuration: {configuration}')
    print(f'Extrinsics: {configuration["cloud_projector"]["ros__parameters"]["extrinsic"]}')
  ...
  return ld

where param is the path to the YAML as in your example. You can then extract the values from configuration["foo"]["ros__parameters"]["extrinsic"]} and pass it to the static_transform_publisher .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM