Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can we actually use Float32MultiArray to publish 2D array using Python?


I am frustrated trying to understand the concept of Float32MultiArray. I have been reading the documentations from Float32MultiArray API , MultiArrayLayout and MultiArrayDimesion but I still don't get how to do it.

Code examples are mostly in C++ (which makes me more confuse that I already am) and many of them conclude to just make their own message file. Some suggest to slice and combine the two arrays into one array then publish it. In the subscriber, the array will be then slice and split up accordingly. My array is a 2*4096 so it is hard for me to do that really. Note that I am a amateur programmer. Please help addressing my question regarding this message type so that the future amateur like me could learn better. My specifications is ROS Indigo, Python 2.7.6 and Ubuntu 14.04. Yeah my tools are old.

My list of questions are as follows:

1) How do we publish and subscribe a Float32MultiArray (or any kind of MultiArray) in Python? What is the standard procedure or steps? From what I have read, I don't understand what is stride from MultiArrayDimesion ? Why do people use stride like this: What does this means[offset + i + dstride1*j] = num?

dstride0 = mat.layout.dim[0].stride
dstride1 = mat.layout.dim[1].stride
offset = mat.layout.data_offset
while not rospy.is_shutdown():
    tmpmat = np.zeros((3,3))
    for i in range(3):
        for j in range(3):
            num = random.randrange(0,10)
  [offset + i + dstride1*j] = num
            tmpmat[i,j] = num
    rospy.loginfo("I'm sending:")
    print tmpmat,"\r\n"

2) Can Float32MultiArray publish 2 array at the same time? I found answer from 2010 post saying that ROS doesn't support 2D message publishing. Is ROS supporting it now in 2019?

3) If I were to make my own message, *can I declare 2 or more array in my message? *Would that message type works? Does ROS support that?

4) Is there any *detail guide or video on using this message type apart from API that has so little explanation ? *I really want to understand about this message type.

5) Any suggestion so that I could publish 2*4096 array of signal amplitude and time (nanosec) for signal processing in my subscriber?

I really hope any expert could address my questions. I think it is really related to ROS rather than my homework. The lack of documentation and examples in python really hinder me from understanding and overcoming this issue. Hope the answers to this question would benefit anyone who looking for the same answers.

Thank you in advance.