Ask Your Question
0

ROS2 how do you publish an int array as a message in python? I get vague "raise TypeError()" when i try

asked 2021-04-30 15:10:27 -0500

rydb gravatar image

updated 2021-04-30 15:11:46 -0500

I want to publish 3 ints as a message but i'm getting this vague error that I'm not sure how to solve this.

This is the relevant bit of my code:

self.publisher = self.create_publisher(
    Int32,
    'wheel_settings',
    10)
#test publishing wheel settings
self.publisher.publish([1, 1, 1])

Error: "raise TypeError()"

How do I fix this? P

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-05-03 12:03:45 -0500

updated 2021-05-03 12:04:10 -0500

alas, Int32 =/= Int32[]

You'll need to define your publisher to publish arrays (in your case, you can use Int32MultiArray.msg).

In python, (without knowing the contents of self.create_publisher()) that could look something like:

from std_msgs.msg import Int32MultiArray

...

self.publisher = self.create_publisher(
    Int32MultiArray,
    'wheel_settings',
    10)
...

msg = Int32MultiArray()
msg.data = [1, 1, 1]
pub.publish(msg)
edit flag offensive delete link more

Comments

Yep, this fixes it. I wish error handling for this was more specific like "error: expected type x, got type y" or something though.

rydb gravatar image rydb  ( 2021-05-04 11:11:09 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2021-04-30 15:10:27 -0500

Seen: 36 times

Last updated: May 03