我是ros + python的新手,我正试图从python ros节点发布一维数组。我使用Int32MultiArray但我无法理解多阵列中布局的概念。有谁可以向我解释一下?或者是否有其他方式发布数组?
感谢。
#!/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)
答案 0 :(得分:2)
*MultiArray
消息在您的情况下有点矫枉过正。我认为如果为此创建自己的简单消息类型IntList
会更简单(参见this tutorial on how to create custom messages)。 IntList.msg
- 文件如下所示:
int32[] data
要发布包含此消息的列表,请使用以下代码段:
a = IntList()
a.data = [3250,2682,6832,2296,8865,7796,6955,8236]
pub.publish(a)
请注意,您无法直接发布列表,但必须实例化IntList
对象并填充此对象的data
成员(这适用于所有消息类型,即使您只是想发布一个单整数!)。
答案 1 :(得分:1)
如果您确实想使用MultiArray
,请尝试:
array = []
my_array_for_publishing = Int32MultiArray(data=array)
答案 2 :(得分:0)
您可以对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)