How to Subscribe to a topic that one node is publishing on while publishing on the same topic for a different node?

asked 2019-09-23 13:50:07 -0600

Previn gravatar image

updated 2019-09-23 19:24:03 -0600

jayess gravatar image


I am working on a leader-follower turtlebot program and I have two turtlebots that will be sending information back and forth to one another.

My question arises because while writing the code it looks like I will run into issues with the follower turtlebot when trying to publish to the cmd_vel topic to make it move.

My leader turtlebot script is coded and working to be operated with a bluetooth controller. The leader turtlebot is subscribed to the cmd_vel topic, and my matlab script is publishing to the cmd_vel topic and the turtlebot moves and operates using:

lets call it X1-Velocity and Z1_Velocity.

What I want the follower turtlebot to do is to subscribe to the same topic that the leader is subscribed to but I need to also subscribe from my follower matlab script to edit things like velocity relative to a distance threshold on the follower bot.

My follower script needs to take the X1_Velocity and Z1_Velocity as inputs and change them based on certain conditions (easy enough), but when republishing the new values: X2_Velocity and Z2_velocity I need to publish those for the follower bot, and not the leader as the leader is strictly controlled by the bluetooth controller.

Is there any way of publishing to a different topic in the follower matlab script and having the follower turtlebot subscribed to that topic?

Basically is there any way to do this:

Leader Bot: Subscribed to cmd_vel_1 Leader Code: Publishing to cmd_vel_1

Follower Bot: Subscribed to cmd_vel_2 Follower Code: Subscribed to cmd_vel_1 and Publishing to cmd_vel_2

I hope I explained well!?

edit retag flag offensive close merge delete



Instead of describing your code can you show it? That'll make things much clearer

jayess gravatar image jayess  ( 2019-09-23 19:23:34 -0600 )edit

Not sure about the explained well part, I'm getting a strong XY problem vibe from it. You talk a lot about what you have, but not much about what you want to achieve. From what I understand you are trying to make one robot follow the other one? If so I would suggest something like this:

  1. Leader bot is subscribed to cmd_vel_1, does not publish anything.
  2. There is a node that subscribes to cmd_vel_1 and does the "change based on certain conditions" and publishes cmd_vel_2
  3. Follower bot subscribes to cmd_vel_2.

If you want your second bot to only use messages that have definitely been consumed by the leader you can adjust point 1 to publish cmd_vel_received and in point 2 subscribe to that topic instead.

would that solve your problem or am I misunderstanding something?

It is possible to subscribe to a topic that is published to by multiple ...(more)

Reamees gravatar image Reamees  ( 2019-09-26 06:48:37 -0600 )edit