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

Getting rosparam from node namespace within bash script?

asked 2015-11-13 12:10:59 -0600

lucasw gravatar image

updated 2020-11-21 12:04:53 -0600

How do I duplicate the behavior in a bash script of python or c++ nodes where getting a rosparam gets it relative to the nodehandle/rospy namespace? I'd like to launch a .sh node in a roslaunch file and set parameters in the node namespace, but how does the shell script know what that namespace is when it runs rosparam get to get it?

roslaunch file:

<node name="foo" pkg="bar" type="">
  <param name="do_something" value="true" />

And then

do_something=`rosparam get do_something`

But that will get /do_something, not the node namespace.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2015-11-13 14:30:45 -0600

updated 2015-11-13 14:37:10 -0600

An obvious answer would be that you could use the full name of the parameter e.g. /foo/do_something.

I'm guessing you are looking for something a bit fancier. If the ROS_NAMESPACE environment variable is set, then the call to rosparam will automatically happen inside of that namespace. So you could use the <env> tag to set that environment variable to be the same as your node name, then the name should get resolved correctly.

I'm not exactly sure if that answers your question, there may be other solutions as well.


Here's an example... I tested this and it works.

Launch File:

  <node name="foo" pkg="bar" type="" output="screen">
    <env name="ROS_NAMESPACE" value="foo" />
    <param name="do_something" value="true" />

val=$(rosparam get do_something)
echo "PARAM = ${val}"
edit flag offensive delete link more


Tracking the namespace manually gets error prone and cumbersome for nested namespaces, it would be nice if there was a special roslaunch variable that was the current namespace.

lucasw gravatar image lucasw  ( 2015-11-14 05:46:21 -0600 )edit

answered 2015-11-14 05:57:24 -0600

lucasw gravatar image

updated 2015-11-14 07:33:30 -0600

I made a python wrapper that takes a shell script as a parameter, and launches the script int the rospy.get_name() namespace (or should it optionally do rospy.get_namespace()?) .

   <group ns="nest">
      <node name="wrap_shell_script" pkg="testbot_utils" type="" 
        <param name="pkg" value="testbot_utils" />
        <param name="script" value="" />
        <param name="test_param" value="have_correct_namespace" />

And the wrapper script:

#!/usr/bin/env python

import rospy
import subprocess


# TODO get other script arguments?
#namespace = rospy.get_namespace()
# assume the shell script only wants private params
namespace = rospy.get_name()

script = rospy.get_param("~script")
pkg    = rospy.get_param("~pkg")'ROS_NAMESPACE=' + namespace + ' rosrun ' + pkg + ' ' + script, shell=True)

So there the wrapper assumes the script will only want private params when it does a rosparam get, and the script can't use params named pkg or script because they are in the same namespace.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-11-13 12:10:59 -0600

Seen: 1,061 times

Last updated: Nov 14 '15