Cyril Anthony
Cyril Anthony

Reputation: 95

ROS publishing array from python node

I'm new to ros+python and i'm trying to publish a 1-D array from python ros node. I used Int32MultiArray but i cant understand the concept of layout in multiarray. Can anyone explain it to me? or is there any other way of publishing an array ?

Thanks.

#!/usr/bin/env python

import roslib
roslib.load_manifest('test_drone')
import numpy
import rospy
import sys
import serial
from std_msgs.msg import String,Int32,Int32MultiArray,MultiArrayLayout,MultiArrayDimension
from rospy.numpy_msg import numpy_msg
from rospy_tutorials.msg import Floats

#port = "dev/ttyS0"
#baud = 115200

#ser = serial.Serial()
#ser.port = port
#ser.baudrate = baud



################################################################################################


def main(args):
   pub=rospy.Publisher('sonar_vals',Int32MultiArray,queue_size = 10)
   rospy.init_node('ca_serial')
   r = rospy.Rate(0.2)
   while not rospy.is_shutdown():
      print "LOOP running"
      a = [3250,2682,6832,2296,8865,7796,6955,8236]
      pub.publish(a)
      r.sleep();



     # try:
         #data_raw = ser.readline()
     # except e:
        # print e
      #sd = data_raw.split(',')   
      #a = numpy.array([sd[0],sd[1],sd[2],sd[3],sd[4],sd[5],sd[6],sd[7],sd[8],sd[9]],dtype=numpy.float32)
      #if sd[0] == 777:
      #   pub.publish(a)
     # else:
       #  print 'Invalid Data'




if __name__ == '__main__':
    import sys, getopt
    main(sys.argv)

Upvotes: 8

Views: 51802

Answers (4)

neurosock
neurosock

Reputation: 576

MultiArray* messages are discouraged for custom applications (use custom messages instead). For quick prototyping and using generic data visualization tools they seem to be OK (see here for a discussion). In ROS2 you can use this to quickly test sending

publisher = node.create_publisher(Float32MultiArray,'topic',10)
data = [0.0, 1.0]
publisher.publish(Float32MultiArray(data=data))

And this to unpack the message on the other side. Messages are already array objects there.

subscription = node.create_subscription(
        Float32MultiArray,'topic',listener_callback,10)
...
def listener_callback(msg):
    rclpy.logging.get_logger().info('I heard: %f %f' % (msg.data[0], msg.data[1]))

Upvotes: 0

Niharranjan Pradhan
Niharranjan Pradhan

Reputation: 131

You can use the following methods for python:

pub = rospy.Publisher('chatter2', Float64MultiArray, queue_size=10)
data_to_send = Float64MultiArray()  # the data to be sent, initialise the array
data_to_send.data = array # assign the array with the value you want to send
pub.publish(data_to_send)

Upvotes: 3

Bill McCormick
Bill McCormick

Reputation: 31

If you do want to use MultiArray, try:

array = []
my_array_for_publishing = Int32MultiArray(data=array)

Upvotes: 3

luator
luator

Reputation: 5037

The *MultiArray messages are a bit overkill in your case. I think it is much simpler if you create your own simple message type IntList for this (see this tutorial on how to create custom messages). The IntList.msg-file looks just like follows:

int32[] data

To publish a list with this message use the following snippet:

a = IntList()
a.data = [3250,2682,6832,2296,8865,7796,6955,8236]
pub.publish(a)

Note that you cannot directly publish the list but have to instantiate an IntList object and fill the data member of this object (this holds for all message types, even if you just want to publish a single integer!).

Upvotes: 10

Related Questions