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

Subscribe to Multiple Topics and Callback Function

asked 2014-10-27 18:23:48 -0500

ROSCMBOT gravatar image

updated 2014-10-27 20:05:47 -0500

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-10-27 18:36:00 -0500

ahendrix gravatar image

updated 2014-10-27 20:33:41 -0500

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.

edit flag offensive delete link more

Comments

Thanks. Is it possible to have a callback function with multiple arguments"

ROSCMBOT gravatar image ROSCMBOT  ( 2014-10-27 18:45:30 -0500 )edit

and whether this message filters is related to my question?

ROSCMBOT gravatar image ROSCMBOT  ( 2014-10-27 18:49:15 -0500 )edit

See the rospy Subscriber API

ahendrix gravatar image ahendrix  ( 2014-10-27 18:51:14 -0500 )edit

What do you want to do with multiple callback arguments? You may want to expand your question with more details about your use case.

ahendrix gravatar image ahendrix  ( 2014-10-27 18:52:31 -0500 )edit

Updated my question. Thanks

ROSCMBOT gravatar image ROSCMBOT  ( 2014-10-27 19:30:55 -0500 )edit

I'm assuming these two topics are being published continuously, so I think I want a callback when both topics are updated

ROSCMBOT gravatar image ROSCMBOT  ( 2014-10-27 19:53:30 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-10-27 18:23:48 -0500

Seen: 9,378 times

Last updated: Oct 27 '14