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

Revision history [back]

click to hide/show revision 1
initial version

I managed to make it work with an int parameter. This is what I did:

int n;
this->declare_parameter<int>("param", 0);
this->get_parameter_or("param", n, 0);

And then use: ros2 run pkg_name exe_name --ros-args param:=1.

So it was important to specify the parameter type.

Still, I have some problems sending a string.

I managed to make it work with an int parameter. This is what I did:

int n;
this->declare_parameter<int>("param", 0);
this->get_parameter_or("param", n, 0);

And then use: ros2 run pkg_name exe_name --ros-args param:=1.

So it was important to specify the parameter type.

Still, I have some problems sending Nevertheless, doing the same thing while changing every int with a string. std::string doesn't work.

I managed to make it work with an int parameter. This is what I did:

int n;
this->declare_parameter<int>("param", 0);
this->get_parameter_or("param", n, 0);

And then use: ros2 run pkg_name exe_name --ros-args param:=1.

So it was important to specify the parameter type.

Nevertheless, doing I had a problem with string parameters, so I surrounded the same thing while changing every int 2 functions with a std::string doesn't work.

try-catch:

try {
    this->declare_parameter<std::string>("calib", "no");
    this->get_parameter_or("calib", calib, std::string("no"));
}
catch (std::exception e) {
    std::cout << e.what() << "\n";
}

And I found out a strange behaviour: if I use

ros2 run pkg_name exe_name --ros-args -p "calib:=yes"

I get an exception that says: parameter 'calib' has invalid type: expected [string] got [bool].

While using something else

ros2 run pkg_name exe_name --ros-args -p "calib:=ok"

The param is read correctly.

So it seems that true, false, yes and no are recognised as bool. In fact, using:

try {
    this->declare_parameter<bool>("calib", false);
    this->get_parameter_or("calib", calib, false);
}
catch (std::exception e) {
    std::cout << e.what() << "\n";
}

I can run correctly: ros2 run pkg_name exe_name --ros-args -p "calib:=yes", while having the inverse exception with "calib:=ok" .