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

Revision history [back]

click to hide/show revision 1
initial version

That looks like it should work. beware that your callback will be called with different messages with different types, so you'll have to check the message type and do something different for each type.

Alternately, you could define a different callback for each topic, have each callback parse the message into a common format, and then pass that to a common method which implements the actual program logic.

EDIT: The original question asked, in short "Can I subscribe and pass the same callback to multiple subscribers". I assumed you wanted to process different messages with different types in the same way, using the same callback.

The original answer was:

That looks like it should work. beware work, bug beware that your callback will be called with with different messages with different different types, so you'll have to check the the message type and do something something different for each type.

Alternately, you could define a a different callback for each topic, topic, have each callback parse the message message into a common format, and then pass pass that to a common method which which implements the actual program logic.

EDIT:

I'm no longer sure what you're expecting ROS to do for you.

  • Do you want a callback whenever you get either message?
  • Do you want a callback only when both topics are updated?
  • Are you just trying to avoid keeping a copy of each most recent message?

EDIT: The original question asked, in short "Can I subscribe and pass the same callback to multiple subscribers". I assumed you wanted to process different messages with different types in the same way, using the same callback.

The original answer was:

That looks like it should work, bug beware that your callback will be called with different messages with different types, so you'll have to check the message type and do something different for each type.

Alternately, you could define a different callback for each topic, have each callback parse the message into a common format, and then pass that to a common method which implements the actual program logic.

EDIT:

I'm no longer sure what you're expecting ROS to do for you.

  • Do you want a callback whenever you get either message?
  • Do you want a callback only when both topics are updated?
  • Are you just trying to avoid keeping a copy of each most recent message?

EDIT:

You can use message_filters to subscribe to multiple topics and get a callback when all topics have new messages. Unless your publishers are explicitly publishing messages with identical timestamps, you'll probably want to use the ApproximateTimeSynchronizer.