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

2024-01-06

我能够显示来自两个主题的数据,但无法在 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()

可能的解决方案:

#!/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 and 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐