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

ros2 run .. --ros-args is not working

asked 2022-08-08 09:34:57 -0500

alberto gravatar image

updated 2022-08-09 05:36:28 -0500

Hi all,

Whenever I run: ros2 run package_name executable_name --ros-args -p param_name:=param_value and in another terminal: ros2 param get /node_name param_name I get: Parameter not set.

If in the code I try to: this->declare_parameter("param_name"); and then do the run as above, the execution stops immediately.

What I need to do is to pass a parameter from the CLI and read it in the code. What am I doing wrong?

EDIT to answer @ljaniec comment, I want to run something like:

ros2 run pkg_name exe_name --ros-args "string:=aa"

So I put in the code:

std::string string;
this->declare_parameter<std::string>("string", std::string("no"));      
this->get_parameter_or("string", string, std::string("no"));
std::cout << string<< "\n";

Now reading this, a string should be passed as: --ros-args "string:=aa", I also tried other combinations: --ros-args string:="aa" or --ros-args string:=aa, but none works. The execution starts and stops without saying anything. (I don't have problems with int).

edit retag flag offensive close merge delete

Comments

1

Can you provide a minimal "should-be-working" example? I think it will be the easiest way for us to help you

ljaniec gravatar image ljaniec  ( 2022-08-09 05:02:24 -0500 )edit
1

I can see that you are using std::string, try to change it to string (as here). Maybe change the name of the parameter from "string" to `"my_string"`` as well, because it is a special name in ROS (datatype).

This tutorial shows example CLI with setting the string parameters (changed for clarity):

ros2 run demo_nodes_cpp parameter_blackboard --ros-args -p "a_string:=Hello world"

It seems you missed the -p in your --ros-args "string:=aa" - it should be rather --ros-args -p "my_string:=aa"

ljaniec gravatar image ljaniec  ( 2022-08-09 06:38:39 -0500 )edit

Yes I forgot the -p here, but I was using it in the CLI. Also, the param name was only an example, so in the code I'm not using "string".

alberto gravatar image alberto  ( 2022-08-09 07:11:08 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2022-08-09 05:19:08 -0500

alberto gravatar image

updated 2022-08-09 07:58:25 -0500

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, I had a problem with string parameters, so I surrounded the 2 functions with a 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" .

edit flag offensive delete link more

Comments

In ROS2 you have string as a datatype, not C++ std::string

ljaniec gravatar image ljaniec  ( 2022-08-09 06:32:57 -0500 )edit

Mmh this should be the solution, but I receive some compiler error using this->declare_parameter<string>(.., ..);:

error C2065: 'string': undeclared identifier.

Moreover, the behaviour is strange, in fact to declare an int param I should use int64, but I only use int.

alberto gravatar image alberto  ( 2022-08-09 07:08:28 -0500 )edit

Ok, I misled you, this example uses std::string so it shouldn't be that. Your last remark about yes and ok being booleans are surprising for sure! I didn't find any mention of this here nor here. Maybe it is worth filling out the issue in the rclcpp repository?

ljaniec gravatar image ljaniec  ( 2022-08-09 08:16:18 -0500 )edit
1

Yes, I think I'll do it!

alberto gravatar image alberto  ( 2022-08-09 08:28:54 -0500 )edit
1

answered 2022-08-08 22:00:38 -0500

ravijoshi gravatar image

Since Dashing, you need to first declare a parameter before getting or setting it. If you try to get or set an undeclared parameter you will either get an exception thrown, e.g. ParameterNotDeclaredException, or in certain cases you will get an unsuccessful result communicated in a variety of ways (see specific functions for more details).

May I request you to read the documentation at the following URL:

https://docs.ros.org/en/foxy/Releases...

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2022-08-08 09:34:57 -0500

Seen: 546 times

Last updated: Aug 09 '22