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

Python class for subscribing data for several topics

asked 2016-01-25 10:08:03 -0600

Randerson gravatar image

updated 2016-01-25 12:00:38 -0600

Hello all, I just get started with ros and now I am working with bagfiles and subscriber. Now I am trying to subscriber several topics from a bagfile using one node written in python. The following code is my attempt to achieve the aforementioned behavior. Nevertheless, this solution does not work. Someone can give me instruction on how I should create the python class ListenerVilma??? What are the best way to do this?

Thanks

#!/usr/bin/env python

import rospy
from rosgraph_msgs.msg import Clock
from diagnostic_msgs.msg import DiagnosticArray

class ListenerVilma:
    """Class that listens all topics of the file vilmafeagri"""

    def Clock_(self):
        """Method that listens the topic /clock if the file vilmafeagri"""
        def __init__(self):
            self.listener()


        def callback(self, clock):
            print clock


        def listener(self):
            rospy.Subscriber('clock', Clock, self.callback)


    def Diagnostics_(self):
        """Method that listen the topic /diagnostics from rosbag file vilmafeagri"""
        def __init__(self):
            self.listener()


        def callback(self, diagnostics):
            print diagnostics


        def listener(self):
            rospy.Subscriber('diagnostics', DiagnosticArray, self.callback)


if __name__ == '__main__':

    rospy.init_node('listener', anonymous=True)
    ListenerVilma.Clock_()
    rospy.spin()

The following performs better the behavior that I wanted. Now, the question is: Is this solution fast??? There better solutions???

class ListenerVilma:

def CLOCK(self):

    def clock_sub():
        rospy.Subscriber('clock', Clock, clock_callback)

    def clock_callback(clock):
        print clock

    clock_sub()


def DIAGNOSTICS(self):

    def diagnostics_sub():
        rospy.Subscriber('diagnostics', DiagnosticArray, diagnostics_callback)

    def diagnostics_callback(diagnostics):
        print diagnostics

    diagnostics_sub()

    if __name__ == '__main__':
        rospy.init_node('listener', anonymous=True)
        myVilma = ListenerVilma()
        myVilma.CLOCK()
        myVilma.DIAGNOSTICS()
        rospy.spin()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-01-25 10:45:53 -0600

mgruhler gravatar image

I'm not quite sure, what you are trying to achieve, with those __init__ etc. inside the functions Clock_ and Diagnostics_.

I would go for something like this:

def diagnostics_callback(diagnostics):
    print diagnostics

def clock_callback(clock):
    print clock

if __name__ == '__main__':
    rospy.init_node('listener', anonymous=True)
    diagnostics_sub = rospy.Subscriber('diagnostics', DiagnosticArray, diagnostics_callback)
    clock_sub = rospy.Subscriber('clock', Clock, clock_callback)
    rospy.spin()

or, if you want to use classes:

class ListnerVilma:
    def __init__(self):
        self.diagnostics_sub = rospy.Subscriber('diagnostics', DiagnosticArray, self.diagnostics_callback)
        self.clock_sub = rospy.Subscriber('clock', Clock, self.clock_callback)

    def diagnostics_callback(self, diagnostics):
        print diagnostics

    def clock_callback(self, clock):
        print clock

if __name__ == '__main__':
    rospy.init_node('listener', anonymous=True)
    myVilma = ListenerVilma()
    rospy.spin()

That should do the trick (from the top of my head, most probably not error free ;-) )

edit flag offensive delete link more

Comments

Thanks... I did some adaptations on you example code and it worked. :)

Randerson gravatar image Randerson  ( 2016-01-25 11:51:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-25 10:08:03 -0600

Seen: 10,162 times

Last updated: Jan 25 '16