在 ROS 中使用来自多个主题的数据 - Python [英] Use data from multiple topics in ROS - Python

查看:73
本文介绍了在 ROS 中使用来自多个主题的数据 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

谢谢;)

#!/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.orientationself.velocity 形式的数据库存",你可以用它来实时计算".

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".

例如:

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

这篇关于在 ROS 中使用来自多个主题的数据 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆