ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Unable to Publish Float64MultiArray in python

asked 2021-06-23 01:26:26 -0500

panserzap gravatar image

updated 2021-06-23 01:30:45 -0500

Hello everyone,

I am trying to publish a Float64MultiArray using python, this is what I have so far.


import rospy
from std_msgs.msg import Float64MultiArray    

TOPIC_NAME = 'waypoints'
NODE_NAME = 'waypoints_publisher'

def post_coordinates():
    pub = rospy.Publisher('waypoints', Float64MultiArray, queue_size=10) # TOPIC
    rospy.init_node('waypoints_publisher', anonymous=True)
    rate = rospy.Rate(1) # 10hz

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

    rospy.loginfo('SEND DATA: \n%s', my_msg)

 if __name__ == '__main__':
    except rospy.ROSInterruptException:

I get the following error when trying to run the code:

 Traceback (most recent call last):
 File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/", line 112, in serialize
struct.error: required argument is not a float

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/", line 882, in publish
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/", line 1066, in publish
    serialize_message(b, self.seq, message)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/", line 152, in serialize_message
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/", line 113, in serialize
    except struct.error as se: self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), 
str(locals().get('_x', self)))))
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/", line 393, in _check_types
    check_type(n, t, getattr(self, n))
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/", line 314, in check_type
    check_type(field_name + '[]', base_type, v)
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/", line 273, in check_type
    raise SerializationError('field %s must be float type' % field_name)
genpy.message.SerializationError: field data[] must be float type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/catkin_ws/devel/lib/robotics_demo/", line 15, in <module>
    exec(compile(, python_script, 'exec'), context)
  File "/home/ubuntu/catkin_ws/src/robotics_demo/scripts/", line 25, in <module>
  File "/home/ubuntu/catkin_ws/src/robotics_demo/scripts/", line 18, in post_coordinates
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/", line 886, in publish
    raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data[] must be float type

I already tried adding this:

d=[[float(d[i][j]) for j in range(len(d))] for i in range(len(d[0]))]

exactly before : = d

the error remains the same. What am I doing wrong?

This is my

#!/usr/bin/env python

import rospy

from ros_tcp_endpoint import TcpServer, RosSubscriber
from std_msgs.msg import Float64MultiArray

def main():
    ros_node_name = rospy.get_param("/TCP_NODE_NAME", 'TCPServer')
    buffer_size = rospy.get_param("/TCP_BUFFER_SIZE", 1024)
    connections = rospy.get_param("/TCP_CONNECTIONS", 10)
    tcp_server = TcpServer(ros_node_name, buffer_size, connections)
    rospy.init_node(ros_node_name, anonymous=True)

    tcp_server ...
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2021-06-23 01:59:29 -0500

mgruhler gravatar image

updated 2021-06-23 02:54:18 -0500

You try to assign a list of lists to is, however, a list of floats only. How you order the single values needs to be specified in the MultiArrayLayout part my_msg.layout.


Not quire sure what the ros_tcp_endpoint expects and if it can handle arbitrary msgs.

If it can handle arbitrary messages, I'd go for something more descriptive for a waypoint, then a FloatMultiArray. You could use e.g. a nav_msgs/Path or a geometry_msgs/PoseArray. Or even create your own Waypoint message, e.g. as a list of geometry_msgs/Point.

If, for some reason, you are limited to use a FloatMultiArray, I'd do something along the lines of (rough sketch, so no guarantee that it works, but you should get the idea)

p1 = [x,y,z] # create all the points until pn = [*p1, ..., *pn]
my_msgs.layout.data_offset =  0 # no padding
dim = []
dim.append(MultiArrayDimension("points", n, 3*n))
dim.append(MultiArrayDimenstion("coords", 3, 1))
my_msgs.layout.dim = dim
edit flag offensive delete link more



Thank you for the guidance. I now understand what I am doing wrong. I have already read the documentation though, but I haven't been able to fully understand it. Could you give me an example either to my specific case or to a similar one?


I believe "Or even create your own Waypoint message, e.g. as a list of geometry_msgs/Point." is the best choice for me. Since it is currently working after following your example I will keep it as it is for now. Thank you for the fast and detailed answer. Wunsche einen schönen Tag!

panserzap gravatar image panserzap  ( 2021-06-23 02:05:13 -0500 )edit

Question Tools

1 follower


Asked: 2021-06-23 01:26:26 -0500

Seen: 3,914 times

Last updated: Jun 23 '21