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

parameter communication by using launch file

asked 2016-08-28 03:41:17 -0600

sasadasd gravatar image

updated 2016-08-29 05:55:38 -0600

gvdhoorn gravatar image

I'd like to use parameter communication by using launch file.

First, I make the following node.

#include <ros/ros.h>                       
int a=0;
int b;
int main(int argc, char **argv) 
  ros::init(argc, argv, "param_test");    
  ros::NodeHandle nh;                                             

ros::Rate r(10);
  nh.getParam("param_str", b);       

This node is only showing the value of "b".

then,I make the following launch file.

<node pkg="ros_p" name="ros_p_node" type="ros_p_node" output="screen"/>
<param name="param_str" type="int" value="1" />

I want to change the value of "b" to 1,but it showed 0.

waht is it incorrect?

edit retag flag offensive close merge delete


I want to set the value 'b' by using launch file even though the initial value(a=0) is allocated .

sasadasd gravatar image sasadasd  ( 2016-08-28 03:47:39 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2016-08-28 11:45:54 -0600

ahendrix gravatar image

roslaunch guarantees that all of the parameters set in the launch file will be set before your node starts.

So roslaunch sets the parameter to 1 before starting your node.

Once your node starts, it immediately sets the parameter to 0.

Then your node reads the parameter, and gets the value 0.

It sounds like you want your node to have a default value for the parameter, and you want to override that from the launch file. the common way to do this is by explicitly specifying a default value when you get parameter.

This is documented on the roscpp parameters page, and the syntax for it looks like:

int b;
nh.param<int>("param_str", b, 0);

This will fetch the value of the param_str parameter into b, and use the value 0 if the parameter is not set. You can then set param_str in your launch file (as you have already done) to override the default value of 0.

edit flag offensive delete link more


I achieved my goal by eliminating "nh.setParam("param_str",a);" and "nh.getParam("param_str", b)" and replacing nh.param<int>("param_str", b, 0).

Thank you so much.

sasadasd gravatar image sasadasd  ( 2016-08-28 16:19:34 -0600 )edit

Question Tools


Asked: 2016-08-28 03:41:17 -0600

Seen: 656 times

Last updated: Aug 29 '16