0
$\begingroup$

Rosanswers logo

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()



    

 

Originally posted by Randerson on ROS Answers with karma: 236 on 2016-01-25

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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 ;-) )


Originally posted by mgruhler with karma: 12390 on 2016-01-25

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Randerson on 2016-01-25:
Thanks... I did some adaptations on you example code and it worked. :)

$\endgroup$

Your Answer

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