Assign topic and data_class to subscriber using values from env

asked 2021-10-28 02:41:05 -0500

texdade gravatar image

Hi all!

I'm making a general-purpose ROS node using Python's rospy. I would need to be able to create the subscribers and publishers in this node without using static values. I'll explain myself better: right now when I define a pub/sub, I use the following code:

rospy.Publisher('topic_123', Float32, queue_size=10)

My problem is that now I would like to be able to read the topic name and the message type from an environment variable and use those. While this is easy for the topic name, I have no idea of how to do that with the message type, specifically I don't know how to convert the string that I read from the environment variable to a class.

Thank you in advance!

edit retag flag offensive close merge delete

Comments

1

Could you maybe explain why you want to do that? For generic subscribers we have existing infrastructure, so you would not need to invent something yourself.

Also: why an environment variable, and not a command line argument or ROS parameter?

gvdhoorn gravatar image gvdhoorn  ( 2021-10-28 03:09:18 -0500 )edit

Since the ROS node is general-purpose, the final users may want to swap the topics that the node reads/writes to. Reading the env variables, the node runs inside a k8s pod so I use a ConfigMap that gets then mapped to the pod in the form of environment variables.

texdade gravatar image texdade  ( 2021-10-28 03:13:44 -0500 )edit

Does your node need to interact with the received data? By which I mean: should it be able to access the fields of the messages or would it be sufficient to be able to treat the message as a blob?

A generic subscriber will allow the latter. Typed subscriptions the former.

Many of the ROS 1 command line tools written in Python can work with generic messages or derive types from either user input or introspection of the node graph. So this is all possible.

gvdhoorn gravatar image gvdhoorn  ( 2021-10-28 03:16:12 -0500 )edit

I don't need to interact with the data, just store it and send it later based on some parameters. I will look for the generic sub. Thanks!

texdade gravatar image texdade  ( 2021-10-28 06:09:38 -0500 )edit

Hi again, I've looked into the generic sub, but can I also do it with rospy? I couldn't find anything

texdade gravatar image texdade  ( 2021-10-29 04:00:48 -0500 )edit