Publishing Multi dimensional array

Hello,

I am trying to publish a multi dimensional array which consists float 64 type data using python. Can someone recommend if I should use float64 multiarray or should I create my custom message?

sample of the array that i am trying to publish.

[[1.2354567, 99.7890, 67.654236], [67.875, 90.6543, 76.5689], [65.3452, 45.873, 67.8956]]

float64 multiarray in std msgs documentation is a bit confusing as well, it would be great if someone could help me out with that.

Hi @nidhi.alpeshmehta ,

Generally speaking, in order to publish your message you should do the following:

1- Setup a Publisher
2- Initialize an empty message of the type you want.
3- Fill the message with your data
4- Publish the message

So in your case, it would be:

pub = rospy.Publisher('my_topic', Float64MultiArray, queue_size=10)
my_msg = Float64MultiArray()  
my_msg.data = [[1.2354567, 99.7890, 67.654236], [67.875, 90.6543, 76.5689], [65.3452, 45.873, 67.8956]]
pub.publish(my_msg)

Sincerely,

Thank you @mouad.abrini.

I tried publishing the way you suggested to publish the multiarray. The code didn’t throw any error until I tried to echo the topic.

In the below image you can see the error, and the data I am trying to publish. I am not able to understand the error message.

It would be great if you or someone else can help me out understand the error message.

Hi @nidhi.alpeshmehta ,

So I was trying to debug this error and it turned out that the ROSSerializationException is caused by the library somehow understanding the array elements as Strings. So the solution was to force all the array elements to Floats. Here is the code to do so.

#!/usr/bin/env python

import rospy
from std_msgs.msg import Float64MultiArray

rospy.init_node('my_node', anonymous=True)
pub = rospy.Publisher('my_topic', Float64MultiArray, queue_size=10)

r = rospy.Rate(1)
while not rospy.is_shutdown():
    my_msg = Float64MultiArray()
    d=[[1.2354567, 99.7890, 67.654236], [67.875, 90.6543, 76.5689], [65.3452, 45.873, 67.8956]]
    d=[[float(d[i][j]) for j in range(len(d))] for i in range(len(d[0]))]
    my_msg.data = d
    pub.publish(my_msg)
    r.sleep()

Sincerely,

The code is not working. Here is the error I got. Appreciate it if you can help.
[DEBUG] [1684377264.037246]: [/my_topic] failed to receive incoming message : unable to receive data from sender, check sender’s logs for details
[DEBUG] [1684377264.038242]: [/my_topic] failed to receive incoming message : unable to receive data from sender, check sender’s logs for details
[DEBUG] [1684377264.038761]: [/my_topic] failed to receive incoming message: Traceback (most recent call last):
File “/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_base.py”, line 804, in receive_loop
msgs = self.receive_once()
File “/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_base.py”, line 744, in receive_once
self.stat_bytes += recv_buff(sock, b, p.buff_size)
File “/opt/ros/noetic/lib/python3/dist-packages/rospy/impl/tcpros_base.py”, line 111, in recv_buff
raise TransportTerminated(“unable to receive data from sender, check sender’s logs for details”)
rospy.exceptions.TransportTerminated: unable to receive data from sender, check sender’s logs for details

@okkewei
Please post your question as a new post, giving all necessary background.