Help us understand the problem. What is going on with this article?

ROS MultiArray型 を Python で扱う際の注意

ROS で配列を Publish したかったときに詰まったので共有します


最初はC++ の書き方を真似てこうすればいけるやろ!
ってやると...

import rospy
from std_msgs.msg import Float32MultiArray

def talker():
    pub = rospy.Publisher('/hoge', Float32MultiArray, queue_size=10)

    array=Float32MultiArray()
    array.data.resize(5)
    i=0
    for p in range(5):
        array.data[i]=p
        i+=1

    pub.publish(array)

AttributeError: 'list' object has no attribute 'resize'

と怒られました

正解の文は以下

import rospy
from std_msgs.msg import Float32MultiArray

def talker():
    pub = rospy.Publisher('/hoge', Float32MultiArray, queue_size=10)

    array=[]

    for p in range(5):
        array.append(p)

    array_forPublish = Float32MultiArray(data=array)
    pub.publish(array_forPublish)

一度List型にぶちこんでから MultiArray に変換するといけます

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away