ROS:合并两个主题并发布一个

问题描述

我正在尝试从两个不同的主题获取信息,然后将其发布到一个主题上。

主题 1 称为 /objecstInfo,它是自定义消息类型 find_object_2d/DetectionInfo。我需要从这个主题/味精中提取 filePaths

Header header

# All arrays should have the same size
std_msgs/Int32[] ids
std_msgs/Int32[] widths
std_msgs/Int32[] heights
std_msgs/String[] filePaths
std_msgs/Int32[] inliers
std_msgs/Int32[] outliers
# 3x3 homography matrix: [h11,h12,h13,h21,h22,h23,h31,h32,h33] (h31 = dx and h32 = dy,see QTransform)
std_msgs/Float32MultiArray[] homographies

主题 2 被称为 /objectsPose,它是 msg 类型 geometry_msgs/PoseStamped。我需要从这个话题中提取 x 和 y 的姿势。

我要发布此信息的主题名为/objectsTopic。我为此创建了一个名为 ObjectTopic.msg自定义消息,如下所示:

Header header
string objectName
float64 poseX
float64 poseY

objectName我想从主题1发布filePaths,在poseXposeY我想从pose.xpose.y发布主题 2. 我找到了此方法 message_filters,但无法在我的代码中成功实现。事实上,我什至无法发布该主题。当我运行节点并执行 rostopic list 时,主题 /objectsTopic 未列出。

这是我的python脚本:

#!/usr/bin/env python
from __future__ import print_function

import sys
import rospy
import message_filters
from find_object_2d.msg     import ObjectPose
from find_object_2d.msg     import DetectionInfo
from geometry_msgs.msg      import PoseStamped

def listener():
    info_sub = message_filters.Subscriber('/objectsInfo',DetectionInfo)
    pose_sub = message_filters.Subscriber('/objectsPose',PoseStamped)

    ts = message_filters.TimeSynchronizer([info_sub,pose_sub],10)
    ts.registerCallback(callback)
    rospy.spin()

def callback(objectsInfo,objectsPose):
    pub = rospy.Publisher('objectsTopic',ObjectPose,queue_size=10)                       
    msg = ObjectPose()                              
    msg.objectName = objectsInfo.DetectionInfo.filePaths
    msg.poseX = objectsPose.PoseStamped.pose.position.x     
    msg.poseY = objectsPose.PoseStamped.pose.position.y               

    pub.publish(msg)                              
    rospy.sleep(1)

if __name__ == '__main__':
    rospy.init_node('object_topic',anonymous=True)             
    try:
        listener()
    except rospy.ROSInterruptException:
        pass

非常感谢您的帮助!如果有其他方法可以解决此问题,请随时使用 message_filters 提出建议。

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)