0
$\begingroup$

Rosanswers logo

I am a beginner to ROS so am not sure why this is happening.

<launch>
  <node name="hello_world_node" pkg="hello_world" type="hello_world.py" output="screen">
    <param name="freq" value="5" />
    <param name="text" value="Lauch file is working" />
  </node>
</launch>

this is my launch file.

and below is my node hello_world

#! /usr/bin/env python

import rospy
rospy.init_node("hello_world")


#initialising parameters
freq = rospy.get_param('freq',20)
text = rospy.get_param('text',"Hello World")
full_string = text + " %s" %rospy.get_time()
rate = rospy.Rate(freq)
while not rospy.is_shutdown():
    print full_string
    rate.sleep()

now here is the weird part,

if i list out the parameters separately outside the node in the launch file it seems to work.

<launch>

  <node name="hello_world_node" pkg="hello_world" type="hello_world.py" output="screen" />

  <param name="freq" value="0.5" />
  <param name="text" value="Hello World from Launch file" />

</launch>

any idea why the second format works but the first one doesn't?


Originally posted by Anirudhkochhar on ROS Answers with karma: 3 on 2021-01-18

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

http://wiki.ros.org/roslaunch/XML/param

"You can also set private parameter across a group of nodes by using the ~param syntax (see ROS names) in a tag. The declared parameter will be set as a local parameter in the tags that follow that are in the same scope (i.e. group or ns tag). "

You're making it a private param. In example 1, what do you get when you run the node and enter in the following command:

rosparam list


Originally posted by cmcheung with karma: 32 on 2021-01-19

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Anirudhkochhar on 2021-01-19:
interesting, When i run only the node i don't get any parameters at all

/rosdistro
/rosversion
/run_id

this is what the command returns however when i run the example 1 launch file, i get

/hello_world_node/freq
/hello_world_node/text
/rosdistro
/roslaunch/uris/host_anirudh
/rosversion
/run_id

Comment by cmcheung on 2021-01-19:
When you run only the node, there are no parameters that are being loaded.

In the output for example one, notice that the parameters exist, but they are under the name space /hello_world_node. So your code should be

 #initialising parameters
freq = rospy.get_param('/hello_world_node/freq',20)
text = rospy.get_param('/hello_world_node/text',"Hello World")

Or something to that effect. I can't guarantee that first slash is needed.

Comment by tryan on 2021-01-22:
The syntax above accesses them with an absolute parameter name. You can also access private parameters by relative name with '~' syntax. For example:

freq = rospy.get_param('~freq`, 20)

Comment by Anirudhkochhar on 2021-01-22:
thanks, that explains it rather well

$\endgroup$

Your Answer

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