ROS从python节点发布数组 [英] ROS publishing array from python node

查看:49
本文介绍了ROS从python节点发布数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ros + python的新手,我正尝试从python ros节点发布一维数组.我使用了Int32MultiArray,但我无法理解multiarray中布局的概念.谁能向我解释?还是有其他发布数组的方式?

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 ?

谢谢.

#!/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)

推荐答案

在您的情况下, * MultiArray 消息有点过大.我认为,如果为此创建自己的简单消息类型 IntList ,则简单得多(请参阅

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)

请注意,您不能直接发布列表,而必须实例化 IntList 对象并填充此对象的 data 成员(即使所有消息类型都适用,即使您只想发布一个整数!).

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!).

这篇关于ROS从python节点发布数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆