Ask Your Question
0

parameter communication by using launch file

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

sasadasd gravatar image

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

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;                                             
  nh.setParam("param_str",a); 

ros::Rate r(10);
while(ros::ok())
{
  nh.getParam("param_str", b);       
  ROS_INFO_STREAM(b);        
  r.sleep();
}
}

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

then,I make the following launch file.

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

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

Comments

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 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

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

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

Comments

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 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

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

Seen: 259 times

Last updated: Aug 29 '16