代码之家  ›  专栏  ›  技术社区  ›  Antoine

在ROS-Python中使用来自多个主题的数据

  •  3
  • Antoine  · 技术社区  · 7 年前

    我能够显示来自两个主题的数据,但我不能在ROS中实时使用和计算来自这两个主题的数据(用Python代码编写)。

    你有没有想法储存这些数据并进行实时计算?

    谢谢;)

    #!/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()
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Mass Zhou    6 年前

    您需要message\u filter来同步多条消息。阅读此 http://wiki.ros.org/message_filters#Example_.28Python.29-1

        2
  •  1
  •   Flux    7 年前

    可能的解决方案:

    #!/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()
    

    现在您有了一个“数据库”,其形式为 self.orientation self.velocity ,您可以使用它来“实时计算”。

    例如:

    #!/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()