Robotics StackExchange | Archived questions

Publish protobuf message

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

Asked by kk2105 on 2019-03-21 08:13:43 UTC

Comments

You cannot publish protobuf messages with the standard ROS infrastructure.

Asked by gvdhoorn on 2019-03-21 08:28:22 UTC

@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.

Asked by kk2105 on 2019-03-21 08:33:07 UTC

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

Asked by gvdhoorn on 2019-03-21 08:34:21 UTC

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

Asked by kk2105 on 2019-03-21 08:37:10 UTC

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

Asked by gvdhoorn on 2019-03-21 09:30:56 UTC

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

Asked by kk2105 on 2019-03-21 23:26:30 UTC

Answers