Robotics StackExchange | Archived questions

publish list of lists with Python

Hello,

I am currently trying for my thesis Project to create topics with ROS where I publish and subscribe a list of lists with python. I have created two messages: intlist1d.msg : int32[] data and another message
intlist2d.msg : intlist1d[] data. I use the message for the list of lists (intlist2d) but, despite the fact that the catkin_make is completed without errors when I execute the roslaunch file for the two nodes I receive the following answer: AttributeError: 'list' object has no attribute 'data'. Can you please help me? I am stuck. Thank you

The code comprises of two files:
pub.py

import rospy
from iot_humans_track.msg import int_array2d
if __name__ == "__main__":
    rospy.init_node('pub', anonymous=True)
    msg_list = int_array2d()
    msg_list.data = [[1,2,3], [1,2]]

    puber = rospy.Publisher('topic', int_array2d, queue_size=10)
    while not rospy.is_shutdown():
        puber.publish(msg_list)

and the sub.py

import rospy
from iot_humans_track.msg import int_array2d

def callback(msg):
    print msg.data

if __name__ == "__main__":
    rospy.init_node('sub', anonymous=True)
    sub_node = rospy.Subscriber('topic', int_array2d, callback, queue_size=10) 
    rospy.spin()

Asked by pskampas on 2019-02-19 07:01:37 UTC

Comments

Can you share your code please ? The CMakeList.txt might help too.

Asked by Delb on 2019-02-19 07:17:06 UTC

Currently ROS can not publish a 2D array. One solution could be that you convert your list to 1D array before publishing and then convert it back upon reception. What is your usage? How are you publishing and receiving the message. Share the code snippet.

Asked by Rao on 2019-02-19 07:21:31 UTC

I have followed the instructions from ROS Wiki. The catkin_make is completed without problems.

Asked by pskampas on 2019-02-19 07:37:24 UTC

Answers

ROS can not publish 2D arrays. Alternatively you can publish a 1D array (use message int32[] data)

import rospy
import numpy as np

from iot_humans_track.msg import int_array1d

if __name__ == "__main__":
rospy.init_node('pub', anonymous=True)

data = np.array([[1,2,3], [4,5,6]])

#Convert to 1d array
data_1d = np.reshape(data,6) 

puber = rospy.Publisher('topic', int_array1d, queue_size=10)
while not rospy.is_shutdown():
    puber.publish(data_1d)

and convert it later as follows:

import rospy
import numpy as np
from iot_humans_track.msg import int_array1d

def callback(msg):

    data_2d = np.reshape(msg.data,(-1,3))
    print data_2d

if __name__ == "__main__":
    rospy.init_node('sub', anonymous=True)
    sub_node = rospy.Subscriber('topic', int_array1d, callback, queue_size=10) 
    rospy.spin()

Asked by Rao on 2019-02-19 08:25:45 UTC

Comments

This is a rather bad idea. ROS has MultiArray message type (e.g. http://docs.ros.org/api/std_msgs/html/msg/ByteMultiArray.html) that could be used for this task. If you just reshape the message, you destroy the information about the individual array sizes.

Asked by NEngelhard on 2019-02-20 04:08:03 UTC

There is an inconsistency in your question. You first say that your messages are int_list_1d.msg and int_list_2d.msg but then you create an int_array2d-message.

msg_list = int_list_2d()
msg_lists.data <- this is a list of int_list_1d-messages and not a list of int32-lists. 

So you'd rather have to go for

msg_list = int_list_2d()
msg_list.data.append(int_list_1d(data=[1,2,3])
msg_list.data.append(int_list_1d(data=[1,2])

"despite the fact that the catkin_make is completed without errors"

Well, python is a language without strict data types, so it's no big surprise that you find type errors at runtime. If you want to find problems like this earlier, use something like C++

Asked by NEngelhard on 2019-02-19 08:35:03 UTC

Comments

Thank you! It works!

Asked by pskampas on 2019-02-19 10:15:43 UTC

glad to hear. then please accept the question so that's clear that it's solved

Asked by NEngelhard on 2019-02-19 10:45:12 UTC