Publish protobuf message

asked 2019-03-21 08:13:43 -0500

kk2105 gravatar image

Hi Guys,

I am trying to publish custom protobuf message from a ros node.

My protobuffer message format is as given below :

syntax = "proto2";
package proto_tutorial;

message sample {
  required string name = 1;
  required int32 id = 2;
  required double salary = 3;
}

And my talker.py is as given below:

import rospy
from std_msgs.msg import String
import sys

proto_path = "/home/catkin_ws_proto/test/gen_files/"
sys.path.append(proto_path)
import sample_pb2
from sample_pb2 import sample

def talker():
    pub = rospy.Publisher('chatter', sample, queue_size=10)    # How to define the message type
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

But I am not able to define the message type in the pub = rospy.Publisher('chatter', sample, queue_size=10), could you please help me in clarifying the below doubts.

  1. How can I define the custom message type ?
  2. Is this supported ?
  3. Is conversion is the only solution ?

Thank you, KK

edit retag flag offensive close merge delete

Comments

1

You cannot publish protobuf messages with the standard ROS infrastructure.

gvdhoorn gravatar image gvdhoorn  ( 2019-03-21 08:28:22 -0500 )edit

@gvdhoorn Thanks for the confirmation. I will try to convert the proto buffer msg into std msg then from subscriber end I will convert it back to protobuf. I hope from subscriber end I can use the protobuf message types. Correct me if I am wrong.

kk2105 gravatar image kk2105  ( 2019-03-21 08:33:07 -0500 )edit

That depends on what you mean by "use the protobuf message types".

gvdhoorn gravatar image gvdhoorn  ( 2019-03-21 08:34:21 -0500 )edit

For example as given in the question, the sample protobuf message

kk2105 gravatar image kk2105  ( 2019-03-21 08:37:10 -0500 )edit

You cannot use Protobuf msg types with rospy code, or at least not as the type for Publishers or Subscribers.

gvdhoorn gravatar image gvdhoorn  ( 2019-03-21 09:30:56 -0500 )edit

@gvdhoorn, Thanks again. I will use roscpp to capture/subscribe to the serialized data from rospy publisher. (rospy will be sending serialized data).

kk2105 gravatar image kk2105  ( 2019-03-21 23:26:30 -0500 )edit