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

running multiple subscribers to same topic

asked 2022-05-22 06:46:08 -0600

distro gravatar image

updated 2022-05-22 07:05:42 -0600

if I have a node with 2 subscribers subscribed to same topic, and lets say I have a publisher queue of 5--[5,4,3,2,1]. how can I make is so that the first subscriber takes the 1st message in the queue and the second subscriber simultaneously takes the second message and so and so forth? I'm using rospy

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-05-22 18:57:40 -0600

LuisEFA1998 gravatar image

I think your question is a bit ambiguous, the way you access the content of a message depends on the type of message you are working with. Each message has its own attributes.

I don't know if what you want is for the first subscriber node to read the first message published by the publisher node, then the second node to read the second message, and so on. Namely:

Example of messages from a topic in the terminal:

hello_world_1 -> Read by the first node

hello_world_2 -> Read by the second node

hello_world_3 -> Read by the third node . . .

hello_world_i -> Read by the ith node

If what you are looking for is the above you can use an i counter to identify each published message.

Now, if what you are looking for is to read a message that continuously publishes a vector and from that vector you want a node to read a component, another node another component, and so on, you would have to use the callback function of the subscriber nodes to store the message vector inside a variable in your code, this way you can select the component you want to read with each node. Remember that the callback(msg_type msg) function receives the message you are working with as an input parameter, and within it you can directly access each of the variables, vectors and other attributes that the message in question has. For example, if the message type you are using is std_msgs/String, with the callback function you can access the Data attribute to read the text stored there.

Maybe I'm misunderstanding your question, but if you give a little more context about what exactly you're looking to do, then I'll be able to help you.

edit flag offensive delete link more

Comments

A structure like Python Queue could help preserve the order of messages if nodes were processing them at different rates.

ljaniec gravatar image ljaniec  ( 2022-05-23 01:45:34 -0600 )edit
1

@LuisEFA1998 I want what you said in your first paragraph but I want this all to occur within a single node. I want all my subscribers and publisher to be inside one node.where the subscribers work on different messages in the queue. for instance subscriber 1 works on message 1subscriber 2 works on message 2 and so on, simultaneously. Not sure if this is possible in ROS, right now I m using python queueing and multithreading. But I want to see if there is a ROS specific solution so I can compare.

distro gravatar image distro  ( 2022-05-23 12:09:45 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-05-22 06:46:08 -0600

Seen: 144 times

Last updated: May 22 '22