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

Command line parameter server while using launch files?

asked 2018-12-20 10:24:11 -0500

Brolin gravatar image

updated 2018-12-20 10:51:26 -0500

gvdhoorn gravatar image

Hi,

I have written a simple talker node that get the publishing rate (ie. value for rospy.Rate() ) using a command line parameter server. When I run the file using rosrun and give the command line parameter it works fine.

Snippet of program:

if rospy.has_param('~rate'):
    ra = rospy.get_param('~rate')
else:
    rospy.logwarn('no parameter given; using the default value %d' %ra)

rate = rospy.Rate(ra)

Command:

rosrun performance_test py_publisher.py _rate:=50

Now the program publishes at 50Hz.

But I am using a launch file to initiate a talker and a listener node at the same time. Is there any way that I can use some command line parameter so that I can vary the rate every time without having to change the program ?

<launch>    
  <node name='py_publisher_node' pkg='performance_tests' type='py_publisher.py' output='screen' />
  <node name='py_subscriber_node' pkg='performance_tests' type='py_subscriber.py' output='screen' />
</launch>
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-12-20 10:55:22 -0500

gvdhoorn gravatar image

updated 2018-12-21 02:23:04 -0500

I have written a simple talker node that get the publishing rate [..] using a command line parameter server.

A bit of a nit-pick, but there is no such thing as a "command line parameter server".

What happens when you use the _<identifier>:=<value> syntax is that rosrun will pass those to the node and treats those as private parameters of the node. See wiki/rosbash - rosrun for the documentation.

But I am using a launch file to initiate a talker and a listener node at the same time. Is there any way that I can use some command line parameter so that I can vary the rate every time without having to change the program ?

Certainly.

You'd just use the param tag in your launch file as a child of the node tag. See wiki/roslaunch/XML/node for the documentation of node (which links to param).

A short example:

<node name=".." pkg=".." type="..">
  <param name="rate" value="50" />
</node>

Edit:

sure, I can do that. But if I want to change the value every time I run the launch file, I should have to edit the launch file itself. Can I pass the rate parameter like

roslaunch performance_test py_test.launch _rate:=50

Roslaunch works a little different from rosrun, but something like that is possible.

You'd want to take a look at the arg tag (docs).

For your specific example (I've added a default here, which is not actually necessary, but could be a nice thing to do for simple launch files):

<arg name="rate" default="50" />
<node name=".." pkg=".." type="..">
  <param name="rate" value="$(arg rate)" />
</node>

Then invoke it as follows:

roslaunch performance_test py_test.launch rate:=50

although with the default being 50 already this won't result in any different behaviour of course.

Please note that in the end your question is a duplicate of quite a few others, one of which would be #q29461.

edit flag offensive delete link more

Comments

sure, I can do that. But if I want to change the value every time I run the launch file, I should have to edit the launch file itself. Can I pass the rate parameter like

roslaunch performance_test py_test.launch _rate:=50
Brolin gravatar image Brolin  ( 2018-12-20 22:50:24 -0500 )edit

thank you @gvdhoorn.

Brolin gravatar image Brolin  ( 2018-12-22 00:51:48 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2018-12-20 10:24:11 -0500

Seen: 982 times

Last updated: Dec 21 '18