0
$\begingroup$

Rosanswers logo

If not, is there a way to declare a global parameter for all nodes?


Originally posted by ahtsan on ROS Answers with karma: 112 on 2019-06-20

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Parameters are per node in ROS 2. There is no a global parameter server like ROS 1. It is possible to set a param on a node with ros2 param set

$ ros2 run demo_nodes_cpp parameter_blackboard &
...
$ ros2 param set /parameter_blackboard foo bar
Set parameter successful
$ ros2 param list /parameter_blackboard
  foo
  use_sim_time

If you'd like to set a parameter on all running nodes, you'll need to enumerate them and set the parameter on each one. If you would like to set a parameter on a lot of nodes that you're launching, the parameter should be passed into each launch_ros.actions.Node.

import launch
import launch_ros


def generate_launch_description():
    use_sim_time = launch.substitutions.LaunchConfiguration('use_sim_time', default=False)

    return launch.LaunchDescription(
        [
            launch_ros.actions.Node(
                package='demo_nodes_cpp', node_executable='talker',
                node_name='talker',
                parameters=[{'use_sim_time': use_sim_time}]),
            launch_ros.actions.Node(
                package='demo_nodes_cpp', node_executable='listener',
                node_name='listener',
                parameters=[{'use_sim_time': use_sim_time}]),
        ])

I used a LaunchConfiguration in this example. You can give it a value on the command line

$ ros2 launch example_launch.py use_sim_time:=True
...
$ ros2 param get /listener use_sim_time
Boolean value is: True

Originally posted by sloretz with karma: 3061 on 2019-06-20

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by ahtsan on 2019-06-20:
Perfect. Does ros2 param set /node_name key value trigger the on_parameter_event if a SyncParametersClient is defined inside the node? What I want to achieve is to perform some tasks if a parameter is true in the node.

Comment by ahtsan on 2019-06-20:
I am answering to my own question: Yes, it does.

Comment by ahtsan on 2019-06-20:
We can pass argument to set the parameter value for launch file. Is it possible when we run a standalone node? like ros2 run demo_nodes_cpp parameter_blackboard foo:=true.

Comment by marguedas on 2019-06-20:
It doesn't seem to be possible at the moment. But it is listed on the feature list for the next ROS 2 distribution: https://github.com/ros2/ros2/issues/734

passing key-value parameters on CLI

Comment by ahtsan on 2019-06-20:
Can I use the same node for another rclcpp::SyncParametersclient? I have a node named foo_node running and do

void Foo::init()
{
  auto parameters_client = std::make_shared<rclcpp::SyncParametersClient>(this);

  this->declare_parameter("foo");

  auto sub = parameters_client->on_parameter_event(
    [this](const rcl_interfaces::msg::ParameterEvent::SharedPtr event) -> void
    {
      for (auto & changed_param : event->changed_parameters)
        if (changed_param.name == "foo")
        {
          RCLCPP_INFO(this->get_logger(), "Foo is set");
        }
    });
}

and when I run ros2 set param /foo_node foo true in another process, the callback is not triggered but instead it logs [INFO] [Logger]: Hey.

Any thoughts?

Comment by ahtsan on 2019-06-20:
I am answering my self again: I have to make sure there is at least one reference to sub, otherwise it destroy itself.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.