Ask Your Question
0

Custom ROS message with unit8[] in python!

asked 2020-01-17 08:16:35 -0600

Farid gravatar image

updated 2020-01-17 08:19:52 -0600

I have a PacketMsg.msg as follows in my package:

uint8[] buf

I would like to decode /topic with this sample python script;

import rospy
from ouster_ros.msg import PacketMsg

def cb(msg):
    rospy.loginfo('len_msg: {}, type_msg: {}'.format(len(msg.buf), type(msg.buf)))
    rospy.loginfo('msg: {}'.format(msg.buf)) 

def main():
    rospy.init_node("my_sub")
    rospy.Subscriber('/topic', PacketMsg,cb)
    rospy.spin()

if __name__ == "__main__":
    main()

This is the output printed in terminal:

[INFO] [1579270050.540915]: len_msg: 49, type_msg: <type 'str'>
[INFO] [1579270050.541343]: msg: ��H?�~�a�B��`����g=��?M��

whereas, $ rostopic echo /topic returns:

buf: [47, 185, 250, 245, 155, 0, 0, 0, 54, 238, 166, ....................... , 23, 154, 0, 0]

Does anybody know how to change such <type 'str'> to a list for further usages?

Cheers,

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-01-18 01:06:01 -0600

Dirk Thomas gravatar image

In Python a str can be iterated and indexed in the same way as a list: msg.buf[i]

Each 8-bit character can be converted into a number using ord().

To get the same result as you see on the console you can do a list comprehension: [ord(c) for c in msg.buf]

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-01-17 08:16:35 -0600

Seen: 37 times

Last updated: Jan 18