如何publish/subscribe一个python“list of list”作为ROS中的topic

How to publish/subscribe a python “list of list” as topic in ROS

我是ROS和rospy新手,对非简单数据类型作为topic不熟悉

我想构建一个 ROS 节点作为订阅者和发布者:它接收一个主题(两个 float64 的列表),并使用一个函数(比如 my_function)returns一个列表列表float64,然后将这个列表列表发布为主题。

为此,我构建了一个节点如下:

from pymongo import MongoClient
from myfile import my_function
import rospy
import numpy as np

pub = None
sub = None

def callback(req):
    client = MongoClient()
    db = client.block
    lon = np.float64(req.b)
    lat = np.float64(req.a)
    point_list = my_function(lon, lat, db)
    pub.publish(point_list)

def calculator():
    global sub, pub
    rospy.init_node('calculator', anonymous=True)
    pub = rospy.Publisher('output_data', list)
    # Listen
    sub = rospy.Subscriber('input_data', list, callback)
    print "Calculation finished. \n"
    ros.spin()

if __name__ == '__main__':
    try:
        calculator()
    except rospy.ROSInterruptException:
        pass

我清楚地知道订阅者和发布者中的list不是消息数据,但我不知道如何修复它,因为它不是整数也不是整数列表。

您可以通过定义一个新的 ros 来使自己复杂化 输入 msg 或使用默认且易于实现的 std_msgs类型,使用json模块可能会有用,所以你在发布前序列化数据,并在接收后反序列化它......

其余Pub/Sub,主题和处理程序保持不变:)

This post on the ROS forums gives you most of what you need. This is also useful. You can define a new message type FloatList.msg 具有以下规格:

float64[] elements

然后第二个消息类型 FloatArray.msg 定义为:

FloatList[] lists

那么您的函数可能如下所示:

def callback(req):
    client = MongoClient()
    db = client.block
    lon = np.float64(req.b)
    lat = np.float64(req.a)
    point_list = my_function(lon, lat, db)
    float_array = FloatArray()
    for i in range(len(point_list)):
        float_list = FloatList()
        float_list.elements = point_list[i]
        float_array.lists[i] = float_list
    pub.publish(float_array)

然后你可以解压它:

def unpack_callback(float_array_msg):
    for lst in float_array_msg.lists:
        for e in lst.elements:
            print "Here is an element: %f" % e

一般来说,建议您将 ROS 相关问题放在 ROS Forums 上,因为在那里您更有可能得到问题的答案。