Ask Your Question

Revision history [back]

You can use the same callback to subscribe to different topics and check the type of the received message (seeing msg._type) on the callback.

If you are subscribing to two different topics that published messages of types sensor_msgs/LaserScan and nav_msgs/Odometry, for example, then your callback would be something like:

def common_callback(self, msg):

        input_message_type = str(msg._type)

        rospy.logdebug("msg._type: "+input_message_type)

        if input_message_type == "sensor_msgs/LaserScan":
            rospy.loginfo("sub_laserscan called me, with type msg: "+input_message_type)
            # doSomething()

        elif input_message_type == "nav_msgs/Odometry":
            rospy.loginfo("sub_odom called me, with type msg==>"+input_message_type)
            # doSomethingElse()

This callback example I got from this answer.