c++parametersros2

Set Ros2 Parameter from another Node


I have two nodes in c++ that use the same parameter. If I want to reconfigure the node1 parameter_x via the rqt, it would be great if the parameter_x of the node2 is also changed automatically. Is there a possibility for this?

i have try in node1:

auto node = rclcpp::Node::make_shared("node2");
node->set_parameter(rclcpp::Parameter("parameter_x", 50));

Solution

  • i have found the solution:

    auto parameter = rcl_interfaces::msg::Parameter();
    auto request = std::make_shared<rcl_interfaces::srv::SetParametersAtomically::Request>();
    
    client = this->create_client<rcl_interfaces::srv::SetParametersAtomically>(serviceName); // E.g.: serviceName = "/turtlesim/set_parameters_atomically"
    
    parameter.name = parameter_name;  // E.g.: parameter_name = "background_b"
    parameter.value.type = 1          //  bool = 1,    int = 2,        float = 3,     string = 4
    parameter.value.bool_value = true // .bool_value, .integer_value, .double_value, .string_value
    
    request->parameters.push_back(parameter);
    
    while (!client->wait_for_service(1s)) {
        if (!rclcpp::ok()) {
        RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
        return;
        }
        RCLCPP_INFO_STREAM(this->get_logger(), "service " << serviceName <<" not available, waiting again..."); 
    }
    auto result = client->async_send_request(request);