Monday, October 21, 2019

Setting and passing ROS double array parameters to a ROS C++ node

I tried to pass an array or list of double parameters to a ROS node program through the ROS rosrun program but my C++ ROS node program could not read the double array parameter. For example, the screenshot below shows the command to run a ROS node (hello_doubles) from the package beginner_tutorials and setting my_doubles_array parameter with the double list [1.1, 2.2, 3.3]:

$ rosrun beginner_tutorials hello_doubles _my_doubles_array:="[1.1,2.2,3.3]"

Note that after running the command, the ROS parameter server contains a parameter /hello_doubles/my_doubles_array with a string value of '[1.1, 2.2., 3.3]' and not the expected double array [1.1, 2.2, 3.3].

Eventually, I realized the rosrun program passes the list as a string instead of an array of doubles. In order to pass a double array parameter, the following methods could be used instead: (1) use the rosparam set command, or (2) use the rosparam load command.

Use the rosparam set command to pass a double array parameter
  1. In a Terminal, type in the following command:

    $ rosparam set /hello_doubles/my_double_array "[1.1, 2.2, 3.3]"

Use the rosparam load command to set a double array parameter
  1. Using a text editor, create a yaml file e.g. hello_doubles.yaml with the following lines:



    where my_doubles_array is the name of the double array parameter
  2. In a Terminal, type in the following command to load the yaml file:

    $ rosparam load /path/to/hello_doubles.yaml

No comments: