0
$\begingroup$

Rosanswers logo

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>

Originally posted by Brolin on ROS Answers with karma: 35 on 2018-12-20

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2018-12-20

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by Brolin on 2018-12-20:
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

Comment by Brolin on 2018-12-22:
thank you @gvdhoorn.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.