ros2 run .. --ros-args is not working
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).
Can you provide a minimal "should-be-working" example? I think it will be the easiest way for us to help you
I can see that you are using
std::string
, try to change it tostring
(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"
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".