Precautions when dealing with ROS MultiArray types in Python

I got stuck when I wanted to publish an array in ROS so I'll share it


At first, you can imitate how to write C ++ and do this! I mean ...

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'

Was angry

The correct sentence is below

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)

You can convert it to MultiArray after putting it in List type once.

Recommended Posts

Precautions when dealing with ROS MultiArray types in Python
Precautions when dealing with control structures in Python 2.6
Character encoding when dealing with files in Python 3
gRPC-Methods used when dealing with protocol buffers types in Python CopyFrom, Extend
Japanese output when dealing with python in visual studio
Precautions when using pit in Python
Until dealing with python in Atom
Precautions when using six with Python 2.5
How to not escape Japanese when dealing with json in python
Tips for dealing with binaries in Python
Precautions when solving DP problems with Python
Dealing with "years and months" in Python
[Python] Dealing with multiple call errors in ray.init
[Web development with Python] Precautions when saving cookies
Organize types in Python
Mailbox selection when retrieving Gmail with imaplib in python
Calculate Pose and Transform differences in Python with ROS
Scraping with selenium in Python
Working with LibreOffice in Python
Debugging with pdb in Python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Attention when os.mkdir in Python
Combined with permutations in Python
Error when playing with python
Problem not knowing parameters when dealing with Blender from Python
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
Behavior when listing in Python heapq
CSS parsing with cssutils in Python
Precautions when installing tensorflow with anaconda
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Here's a summary of things that might be useful when dealing with complex numbers in Python
Use rospy with virtualenv in Python3
Precautions when passing def to sorted and groupby functions in Python? ??
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Precautions when creating a Python generator
Password generation in texto with python
Use OpenCV with Python 3 in Window
Precautions when using phantomjs from python
When matplotlib doesn't work with python2.7
Precautions when giving default values to arguments in Python function definitions
When using MeCab with virtualenv python
Get started with Python in Blender
When using regular expressions in Python
When writing a program in Python
Working with DICOM images in Python
[Python] Format when to_csv with pandas
Dealing with key not found error in pacstrap when installing Arch Linux
Dealing with Python error "Attribute Error: module'scipy.misc' has no attribute'imresize'" in deep learning
Addictive point when going through http proxy with basic authentication in python