ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Parameters from yaml file from launch.py not passed to node

asked 2022-03-29 05:08:24 -0600

kyubot gravatar image

updated 2022-03-29 05:09:34 -0600

Hello,
I am trying to create a node to control a robot using joystick and want to pass parameters such as max speed, rotational speed and etc from a parameter file.
For this project I am running ROS2 foxy on ubuntu 20.04 system.

I added a teleop_joy.yaml file under 'my_teleop/param' and added to the package.

teleop_joy_node:   ros__parameters:
    test: 
      array: ['1','2','3']
    max_fwd_m_s: 0.23
    max_rev_m_s: 0.20
    max_deg_s: 20.0
    use_sim_time: true

I tried to read this file in my launch.py as below. This launch file run teleop_joy and joy_node simultaneously to control a robot.

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
from launch.substitutions import TextSubstitution
from launch_ros.actions import Node
def generate_launch_description():
  teleop_joy_params = LaunchConfiguration(
    'my_teleop_joy_parameter',
    default=os.path.join(
    get_package_share_directory('my_teleop'),
      'param',
      'teleop_joy.yaml'
    )
  )
  joy_config = LaunchConfiguration('joy_config')
  joy_dev = LaunchConfiguration('joy_dev')

  return LaunchDescription([
    DeclareLaunchArgument(
      'my_teleop_joy_parameter',
      default_value=teleop_joy_params
    ),
    Node(
      package='my_teleop',
      namespace='',
      executable='teleop_joy',
      name='teleop_joy_node',
      output='screen',
      emulate_tty=True,
      parameters=[teleop_joy_params],
    ),
    DeclareLaunchArgument(
      'joy_config', 
      default_value='xbox'
    ),
    DeclareLaunchArgument(
      'joy_dev', 
      default_value='/dev/input/js0'
    ),
    DeclareLaunchArgument(
      'config_filepath', 
      default_value=[
        TextSubstitution(text=os.path.join(
          get_package_share_directory(
            'teleop_twist_joy'), 
            'config', '')),
          joy_config, TextSubstitution(text='.config.yaml')
      ]
    ),
    Node(
      package='joy', 
      executable='joy_node', 
      name='joy_node',
      parameters=[{
        'dev': joy_dev,
        'deadzone': 0.3,
        'autorepeat_rate': 20.0,
      }]
    )
  ])

I see both node is running however I could not see parameters except "use_sim_time" I checked this by running

$ ros2 param dump /teleop_joy_node --print
/teleop_joy_node:
  ros__parameters:
    use_sim_time: true

I double check this by changing use_sim_time to false in yaml file and it does reveals the changed parameter. I don't know why other parameters not showing. Any suggestions or comments would be much appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-03-29 23:10:22 -0600

kyubot gravatar image

I kind of figured it out myself by referring a great document here https://roboticsbackend.com/ros2-yaml...

Any parameters included in yaml file has to be declared in the beginning with declare_parameters as below.

def __init__(self):
   super().__init__('teleop_joy_node')
   self.declare_parameters(
            namespace='',
            parameters=[
                ('max_fwd_m_s', None),
                ('max_rev_m_s', None),
                ('max_deg_s', None),
            ])

After adding this, I can see the parameters!

$ ros2 param dump /teleop_joy_node --print
/teleop_joy_node:
  ros__parameters:
    max_deg_s: 20.0
    max_fwd_m_s: 0.25
    max_rev_m_s: 0.2
    use_sim_time: false

I still don't understand why I have to do this but it is working now.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2022-03-29 05:08:24 -0600

Seen: 396 times

Last updated: Mar 29 '22