Ask Your Question
0

How do you set namespace in a Python program?

asked 2020-05-17 13:29:41 -0500

tompe17 gravatar image

updated 2020-05-19 16:20:33 -0500

Is it possible to set the namespace in a Python program? I have looked through the APIs and cannot fins any obvious way.

The reason I want to do it is that I run a Jupyter Notebook server and I need to use different namespaces for the same server.

For example: I want to dynamically change the namespace that is used when expanding a name "pose" for example. I want to be able to set the namespace to for example "/uav0" and "pose" as a topic would then be "/uav0/pose". And it is enough to be able to set that once for a Python program that is started with no namespace.

/Tommy Persson, Linköping University

edit retag flag offensive close merge delete

Comments

What do you mean? The namespace for topics? For parameters? Something else?

For topics and parameters, does #q261862 help?

mgruhler gravatar image mgruhler  ( 2020-05-18 03:50:20 -0500 )edit

Yes, for topics and service calls. I did not see how the question you referred to could help. i tried for example:

os.environ["ROS_NAMESPACE"] = "/dji0"
s = rospy.get_namespace ().rstrip("/")
print("Using namespace: {}\n".format(ns))

but it did not work. I have written a lot of my code without the namespace in the specifcation of topics and services. If I set ROS_NAMESPACE before starting my Jupyter server it works but then this server can only handle notebooks with that namespace. I really need a way in the Pthon program to specify the namespace. Or maybe rewrite all code that specified names and use ROS_NAMESPACE environment variable there.

tompe17 gravatar image tompe17  ( 2020-05-18 13:34:29 -0500 )edit

I still don't understand what you are trying to achieve. Could you please clarify by editing your question? Maybe also give a short example?

It could be (at least) any of the following:

  • starting the notebook one time in namespace A, the second time in namespace B.
  • have some topic/parameters in namespace A and some in namespace B
  • dynamically change the namespace of some/all topics/parameters
  • ...
mgruhler gravatar image mgruhler  ( 2020-05-19 01:53:01 -0500 )edit

I want to dynamically change the namespace that is used when expanding a name "pose" for example. I want to be able to set the namespace to for example "/uav0" and "pose" as a topic would then be "/uav0/pose". And it is enough to be able to set that once for a Python program that is started with no namespace.

tompe17 gravatar image tompe17  ( 2020-05-19 16:20:19 -0500 )edit

Just to double-check: "Changing dynamically" means at runtime. I.e. you want to start this and THEN change the namespace WHILE the node is running?

mgruhler gravatar image mgruhler  ( 2020-05-20 00:51:33 -0500 )edit

Yes, or while the Python program is running. I suppose the node is running when you do init. I want to have a Python program were I inside the program can set what

rospy.get_namespace ()

returns. I only need to set it once.

tompe17 gravatar image tompe17  ( 2020-05-20 16:40:19 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-05-20 17:05:34 -0500

tompe17 gravatar image

I found out how to do it. The "trick" was to set ROS_NAMESPACE before "import rospy". So

import os
os.environ["ROS_NAMESPACE"] = "/dji0"
import rospy

worked. Or using Jupyter notebook magic:

%env ROS_NAMESPACE=/dji0
import rospy
edit flag offensive delete link more

Comments

1

While this achieves what you were trying to do, it's not a very ROS-like thing to do, and very well could be the "wrong" thing to do. This solution would override many "normal" name behaviors with rosrun, roslaunch, ROS_NAMESPACE itself, and likely more. This would make your node difficult for others to use and understand. I'd instead be asking yourself if there is something about your architecture or design that is causing you to think that you need this behavior. Perhaps in a separate question, you could ask a different question along the lines of "here's what I'm trying to accomplish and why... is this a good idea?"

jarvisschultz gravatar image jarvisschultz  ( 2020-05-20 18:55:33 -0500 )edit

This is only to be used in a Jupyter Notebook running interactive Python. rosrun or roslaunch will never start such a program. Please explain to me what kind of problem can occur since I do not see any.

And the reason is that I have written my code assuming I have a namespace. Now I needed a configuration were many robots use the same ROS Core and I needed to be able to open different Jupyter Notebooks that works against different robots. And I did not want to start one Jupyter Server per robot since that will be very inconvenient in the browser.

I really cannot see that my design choice to use the namespace functionality should be wrong here.

tompe17 gravatar image tompe17  ( 2020-05-21 18:34:38 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-05-17 13:29:41 -0500

Seen: 23 times

Last updated: May 20