简体   繁体   中英

ROS2, TypeError when publishing custom message to Topic (python)

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.

The issue is in your assignment of my_msg , which is an instance of the class MyMessage containing attributes defined in the my_shared.msg file, namely my_msg.data which has type of uint8[] . It's correct that you did payload_bitstream.tolist() to get a list of native python ints with uint8 values, but you need to assign it to the data attribute. TL;DR:

my_msg.data = payload_bitstream.tolist()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM