Ask Your Question
1

publish list of lists with Python

asked 2019-02-19 06:01:37 -0600

pskampas gravatar image

updated 2019-02-19 06:32:07 -0600

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: int_list_1d.msg : int32[] data and another message
int_list_2d.msg : int_list_1d[] data. I use the message for the list of lists (int_list_2d) 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()
edit retag flag offensive close merge delete

Comments

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

Delb gravatar imageDelb ( 2019-02-19 06:17:06 -0600 )edit

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.

Rao gravatar imageRao ( 2019-02-19 06:21:31 -0600 )edit

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

pskampas gravatar imagepskampas ( 2019-02-19 06:37:24 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-02-19 07:35:03 -0600

NEngelhard gravatar image

updated 2019-02-19 09:46:32 -0600

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++

edit flag offensive delete link more

Comments

Thank you! It works!

pskampas gravatar imagepskampas ( 2019-02-19 09:15:43 -0600 )edit
2

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

NEngelhard gravatar imageNEngelhard ( 2019-02-19 09:45:12 -0600 )edit
-1

answered 2019-02-19 07:25:45 -0600

Rao gravatar image

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()
edit flag offensive delete link more

Comments

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

NEngelhard gravatar imageNEngelhard ( 2019-02-20 03:08:03 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-02-19 06:01:37 -0600

Seen: 737 times

Last updated: Feb 19