Search code examples
pythonpublish-subscriberosrospy

Use data from multiple topics in ROS - Python


I'm able to display data from two topics but I can't do use and compute data in real time from these two topics in ROS (written in Python code).

Have you got any idea to stock this data and compute in real time ?

Thanks ;)

#!/usr/bin/env python

import rospy
import string
from std_msgs.msg import String 
from std_msgs.msg import Float64MultiArray
from std_msgs.msg import Float64
import numpy as np


class ListenerVilma:

    def __init__(self):
        self.orientation = rospy.Subscriber('/orientation', Float64MultiArray , self.orientation_callback)
        self.velocidade = rospy.Subscriber('/velocidade', Float64MultiArray, self.velocidade_callback)

    def orientation_callback(self, orientation):
        print orientation

    def velocidade_callback(self, velocidade):
        print velocidade


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

Solution

  • Possible solution:

    #!/usr/bin/env python
    
    import rospy
    from std_msgs.msg import Float64MultiArray
    
    
    class Server:
        def __init__(self):
            self.orientation = None
            self.velocity = None
    
        def orientation_callback(self, msg):
            # "Store" message received.
            self.orientation = msg
    
        def velocity_callback(self, msg):
            # "Store" the message received.
            self.velocity = msg
    
    
    if __name__ == '__main__':
        rospy.init_node('listener')
    
        server = Server()
    
        rospy.Subscriber('/orientation', Float64MultiArray , server.orientation_callback)
        rospy.Subscriber('/velocity', Float64MultiArray, server.velocity_callback)
    
        rospy.spin()
    

    Now you have a "stock of data" in the form of self.orientation and self.velocity, and you can use that to "compute in real time".

    For example:

    #!/usr/bin/env python
    
    import rospy
    from std_msgs.msg import Float64MultiArray
    
    
    class Server:
        def __init__(self):
            self.orientation = None
            self.velocity = None
    
        def orientation_callback(self, msg):
            # "Store" message received.
            self.orientation = msg
    
            # Compute stuff.
            self.compute_stuff()
    
        def velocity_callback(self, msg):
            # "Store" the message received.
            self.velocity = msg
    
            # Compute stuff.
            self.compute_stuff()
    
        def compute_stuff(self):
            if self.orientation is not None and self.velocity is not None:
                pass  # Compute something.
    
    
    if __name__ == '__main__':
        rospy.init_node('listener')
    
        server = Server()
    
        rospy.Subscriber('/orientation', Float64MultiArray , server.orientation_callback)
        rospy.Subscriber('/velocity', Float64MultiArray, server.velocity_callback)
    
        rospy.spin()