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

exampe of using AsyncParametersClient for multiple params [closed]

asked 2020-11-02 21:19:51 -0600

lutinplunder gravatar image

I am trying to use a parameter node to store all the params that define a robot using the guide HERE.

The examples show are for only one param, how would you do set it up for multiple params? Do I really have to do all those lines of code for each param?

class TestGetGlobalParam : public rclcpp::Node
{
public:
    TestGetGlobalParam() : Node("test_get_global_param")
    {
        parameters_client = 
            std::make_shared<rclcpp::AsyncParametersClient>(this, "/global_parameter_server");

            parameters_client->wait_for_service();
            auto parameters_future = parameters_client->get_parameters(
                {"my_global_param"},
                std::bind(&TestGetGlobalParam::callbackGlobalParam, this, std::placeholders::_1));
        }
        void callbackGlobalParam(std::shared_future<std::vector<rclcpp::Parameter>> future)
        {
            auto result = future.get();
            auto param = result.at(0);
            RCLCPP_INFO(this->get_logger(), "Got global param: %s", param.as_string().c_str());
        }
private:
    std::shared_ptr<rclcpp::AsyncParametersClient> parameters_client;
};

While I know ROS2 is trying to get away from global params, but for the project I have envisioned it's the best way I can think of. I am trying to write a set of packages for legged robots (3 or 4 dof, and 4, 6 or 8 legs) where the user defines their robot via YAML and the code sets up the low level controller for IK, gait, etc.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by lutinplunder
close date 2020-11-05 20:21:46.732869

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-11-04 12:57:16 -0600

jacobperron gravatar image

The call to get_parameters may take one or more parameter names. Check out this demo where multiple parameters are requested in a single service call:

parameters_client->get_parameters({"foo", "baz", "foobarbaz", "toto"});

Also note, the type of the future argument in the callback is a vector of parameters. This vector should contain all of the parameters requested.

Hopefully this answers your question.

edit flag offensive delete link more

Comments

thanks jacobperron, the ROS2 documentation is even worse than the ROS1 version. given what you told me and what the demo shows I should be able to make it work by using get_parameters to retrieve all the variables I need and then simply map the necessary values from the array to a local name.

lutinplunder gravatar image lutinplunder  ( 2020-11-04 20:22:21 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2020-11-02 21:19:51 -0600

Seen: 974 times

Last updated: Nov 04 '20