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

Getting rosparam from node namespace within bash script?

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

lucasw gravatar image

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

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="test.sh">
  <param name="do_something" value="true" />
</node>

And then test.sh:

#!/bin/sh
...
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
1

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

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

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.

EDIT

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

Launch File:

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

test.sh:

#!/bin/bash
val=$(rosparam get do_something)
echo "PARAM = ${val}"
edit flag offensive delete link more

Comments

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

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

lucasw gravatar image

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

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()?) .

https://github.com/lucasw/testbot/blo...

<launch>
   <group ns="nest">
      <node name="wrap_shell_script" pkg="testbot_utils" type="wrap_shell_script.py" 
            output="screen">
        <param name="pkg" value="testbot_utils" />
        <param name="script" value="test.sh" />
        <param name="test_param" value="have_correct_namespace" />
      </node>
   </group>
</launch>

And the wrapper script:

#!/usr/bin/env python

import rospy
import subprocess

rospy.init_node('wrap_shell_script')

# 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")

subprocess.call('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

Stats

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

Seen: 1,069 times

Last updated: Nov 14 '15