0
$\begingroup$

Rosanswers logo

Hello,

I would like to write a node in python which subscribes to multiple topics using only one callback function. What's the correct way to do this? For instance, I want to compare the values published on two different topics (cmd_vel and base_velocity) in a callback function. I'm assuming these two topics are being published continuously, so I think I want a callback when both topics are updated

def callback(data):
    if cmdVel == baseVelocity:
        # Do Step 1
    else:
        # Do Step 2
    
def listener():
    
    rospy.Subscriber('cmd_vel', Twist, callback)
    rospy.Subscriber('base_velocity', Twist, callback)
           
    rospy.spin()

if __name__ == '__main__':
    
    rospy.init_node('robot_controller', anonymous=True)
    listener()

Thanks


Originally posted by ROSCMBOT on ROS Answers with karma: 651 on 2014-10-27

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by ahendrix with karma: 47576 on 2014-10-27

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by ROSCMBOT on 2014-10-27:
Thanks. Is it possible to have a callback function with multiple arguments"

Comment by ROSCMBOT on 2014-10-27:
and whether this message filters is related to my question?

Comment by ahendrix on 2014-10-27:
See the rospy Subscriber API

Comment by ahendrix on 2014-10-27:
What do you want to do with multiple callback arguments? You may want to expand your question with more details about your use case.

Comment by ROSCMBOT on 2014-10-27:
Updated my question. Thanks

Comment by ROSCMBOT on 2014-10-27:
I'm assuming these two topics are being published continuously, so I think I want a callback when both topics are updated

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.