在 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 中使用来自多个主题的数据 的相关文章

  • AWS Lambda 错误:无法导入模块“function_name”:没有名为“module._module”的模块

    阅读后请特别查看屏幕截图 我正在 AWS Lambda 上部署一个使用该包的 python 脚本impyla它依赖于包bitarray from impala dbapi import connect 我的Python文件名为authori
  • 在Python中解析空选项

    我有一个应用程序 允许您将事件数据发送到自定义脚本 您只需布置命令行参数并指定什么事件数据与什么参数相匹配 问题是这里没有真正的灵活性 您制定的每个选项都将被使用 但并非每个选项都必须有数据 因此 当应用程序构建要发送到脚本的字符串时 某些
  • 使用 Python 连接从 FTP 检索文件

    我构建了这个简单的工具来暴力破解并连接到 ftp 服务器 import socket import ftplib from ftplib import FTP port 21 ip 192 168 1 108 file1 passwords
  • 将 KB/MB/GB 等字符串解析为数值

    为了不发明自行车 我想知道是否有任何库能够将大小字符串 MB KB TB MiB KiB 等 的各种表示形式解析为基于数字字节的值 ActiveState Receipes 有一个示例here http code activestate c
  • 使用自定义元素类在 Python 中解析 xml

    我想使用 Python 的 xml etree ElementTree 模块解析 xml 文档 但是 我希望生成的树对象中的所有元素都具有我定义的一些类方法 这建议创建我自己的 Python 元素类的子类 但我无法告诉解析器在解析时使用我自
  • PyKCS11 不可哈希列表

    我的 python 脚本旨在获取特定 so 库中插槽 令牌的详细信息 输出如下所示 Library manufacturerID Safenet Inc Available Slots 4 Slot no 0 slotDescription
  • 如何从 python 脚本更改 python 文件中的变量值

    我目前有一个 python 文件 其中包含一堆带有值的全局变量 我想从一个单独的 python 脚本永久更改这些值 我尝试过 setattr 等 但似乎不起作用 有没有办法做到这一点 简短的回答是 不 不值得这么麻烦 听起来您正在尝试创建一
  • Unpickle 二进制文件为文本[重复]

    这个问题在这里已经有答案了 我需要对基本上如下所示的系统进行一些维护 复杂的遗留Python程序 gt 二进制pickle文件 gt 另一个复杂的遗留Python程序 这需要准确弄清楚中间 pickle 文件中的内容 我怀疑文件格式比生成和
  • scipy 的 curve_fit 函数的尺寸问题

    我对 python 中的曲线拟合以及一般的 python 都很陌生 目前 我正在尝试使用 scipy 中的 curve fit 模块来拟合 4 个光谱峰 简而言之 我的文本文件中有两列数据 所以我的第一步是将数据导入到两个数组中 一个包含
  • 使用底图和Python在地图中绘制海洋

    我正在绘制此处提供的 netCDF 文件 https goo gl QyUI4J https goo gl QyUI4J Using the code below the map looks like this 然而 我希望海洋是白色的 更
  • 没有名为 objc 的模块

    我正在尝试将 cocoa python 与 Xcode 一起使用 但它总是会出现错误 Traceback most recent call last File main py line 10 in
  • Python Peeweeexecute_sql() 示例

    我使用 Peewee 模块作为我的项目的 ORM 我看了整个文档 没有明确的 有关如何处理 db execute sql 结果的示例 我跟踪代码 只能发现db execute sql 返回游标 有谁知道如何处理光标 例如迭代它并获取 返回复
  • SyntaxError:多个异常类型必须用括号括起来

    我是初学者 在使用 python 安装 pycaw 进行音频控制后遇到问题 在放置 pycaw 的基本初始化代码时 出现以下错误 Traceback most recent call last File c Users volumeCont
  • python中不规则点之间的坐标列表

    想象一下 我们为 x 和 y 随机选择两个介于 0 到 100 之间的点 例如 95 7 35 6 现在使用简单的 pygame draw line 函数 我们可以轻松地在这些点之间绘制一条没有任何间隙的线 我的问题是 我们如何找到两点之间
  • 如何使用Django模板作为组件?

    我有 5 个模板 index html detail html tag html login html register html and a 基本 html 所有 5 个模板都会扩展基本 html 索引 html 详细信息 html 标签
  • 如何在Python中设置像素的alpha值

    我正在尝试编辑image https drive google com file d 0B8JcwRV HVk0OURrcTFJczhmV2RlUGdMOG0ybldYUVRoamtF view usp sharing以一种将所有白色像素转
  • 如何通过pygit2获取当前签出的Git分支名称?

    这个问题应该与 如何获取Git中当前的分支名称 https stackoverflow com questions 6245570 how to get current branch name in git 获取 git 当前分支 标签名称
  • 对二进制数的字符串表示进行按位运算 python 2.7

    我想对二进制数的两个字符串表示执行按位或 但我不知道如何将字符串转换为原始二进制 a 010110 b 100000 a b 应该产生 110110 然后我想计算 on 位的数量 这应该返回 4 您可以使用内置的将字符串转换为二进制int
  • 尝试输入字符串时出现名称错误[重复]

    这个问题在这里已经有答案了 import pickle import os import time class Person def init self number address self number number self addr
  • jupyter run magic 将参数传递给笔记本

    当您在第一个 jupyter 笔记本 first ipynb 中时 您可以执行第二个 但如何传递参数呢 假设第二个有以下内容 xx 10 您可以从第一个调用第二个 如下所示 run second ipynb xx will print 10

随机推荐