How do you set namespace in a Python program?
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
Asked by tompe17 on 2020-05-17 13:29:41 UTC
Answers
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
Asked by tompe17 on 2020-05-20 17:05:34 UTC
Comments
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?"
Asked by jarvisschultz on 2020-05-20 18:55:33 UTC
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.
Asked by tompe17 on 2020-05-21 18:34:38 UTC
Comments
What do you mean? The namespace for topics? For parameters? Something else?
For topics and parameters, does #q261862 help?
Asked by mgruhler on 2020-05-18 03:50:20 UTC
Yes, for topics and service calls. I did not see how the question you referred to could help. i tried for example:
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.
Asked by tompe17 on 2020-05-18 13:34:29 UTC
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:
A
, the second time in namespaceB
.A
and some in namespaceB
Asked by mgruhler on 2020-05-19 01:53:01 UTC
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.
Asked by tompe17 on 2020-05-19 16:20:19 UTC
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?
Asked by mgruhler on 2020-05-20 00:51:33 UTC
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
returns. I only need to set it once.
Asked by tompe17 on 2020-05-20 16:40:19 UTC