ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2, TypeError when publishing custom message to Topic

asked 2020-06-11 04:18:43 -0600

anthares gravatar image

updated 2020-06-11 12:39:08 -0600

Hi everyone,

I have defined a custom message:

uint8[] data

The custom message is imported in my Node class with no problems:

from my_shared.msg import MyMessage

In the same Node, I create the publisher with:

self.my_publisher = self.create_publisher(MyMessage, 'topic_in', 200)

and I publish the message with:

self.my_publisher.publish(my_msg)

my_msg is built in the following way:

payload_bitstream = np.fromstring(my_data, np.uint8)
my_msg = payload_bitstream.tolist()

Sadly, I get a TypeError:

File "/opt/ros/eloquent/lib/python3.6/site-packages/rclpy/publisher.py", line 68, in publish
    raise TypeError()
TypeError

Could you help out with this if you know what I am doing wrong pls?

Thanks in advance, G.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-06-11 12:45:40 -0600

Dirk Thomas gravatar image

The variable you pass to self.my_publisher.publish() must be of the same type as the one used to create the publisher, in your case MyMessage. So you need to first create an instance of that class:

my_msg = MyMessage()

and then populate the fields in that message with the desired data:

my_msg.data = payload_bitstream.tolist()

I would assume you don't even need .to_list() in the last line but can assign payload_bitstream directly:

ms_msg.data = payload_bitstream

edit flag offensive delete link more

Comments

Thank you so much! .tolist() is still required. But everything else that you provided was the exact answer and approach.. I mean.. it works: I publish and receive on the subscriber. Thanks so much!!!

anthares gravatar image anthares  ( 2020-06-11 12:53:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-11 04:18:43 -0600

Seen: 1,537 times

Last updated: Jun 11 '20